web-dev-qa-db-fra.com

Utilisez Python installé avec apk dans Alpine Linux

Je voudrais installer des packages Python dans Alpine Linux en utilisant apk. J'utilise numpy comme exemple dans ce qui suit.

Dockerfile

FROM python:3-Alpine
RUN apk add --update py3-numpy

Je crée mon image Docker

$ docker build -t python-numpy .
Sending build context to Docker daemon  2.048kB
Step 1/2 : FROM python:3-Alpine
 ---> 930a7e894675
Step 2/2 : RUN apk add --update py3-numpy
 ---> Running in b30470090cde
fetch http://dl-cdn.alpinelinux.org/Alpine/v3.10/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/Alpine/v3.10/community/x86_64/APKINDEX.tar.gz
(1/6) Installing libgcc (8.3.0-r0)
(2/6) Installing libquadmath (8.3.0-r0)
(3/6) Installing libgfortran (8.3.0-r0)
(4/6) Installing openblas (0.3.6-r0)
(5/6) Installing python3 (3.7.3-r0)
(6/6) Installing py3-numpy (1.16.4-r1)
Executing busybox-1.30.1-r2.trigger
OK: 113 MiB in 41 packages
Removing intermediate container b30470090cde
 ---> 5a82ffa67522
Successfully built 5a82ffa67522
Successfully tagged python-numpy:latest

l'exécuter et essayer d'importer le package en python

$ docker run -it --rm python-numpy python -c "import numpy"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'numpy'

Cependant, il n'est pas trouvé. Fonctionnement pip install numpy après apk add --update py3-numpy ne considère pas le paquet apk py3-numpy et télécharge une autre version:

Collecting numpy
  Downloading https://files.pythonhosted.org/packages/da/32/1b8f2bb5fb50e4db68543eb85ce37b9fa6660cd05b58bddfafafa7ed62da/numpy-1.17.0.Zip (6.5MB)
...

Si je spécifie la même version que de py3-numpy (voir la sortie de docker build) dans pip install numpy==1.16.4-r1, cela mène à

Collecting numpy==1.16.4-r1
  ERROR: Could not find a version that satisfies the requirement numpy==1.16.4-r1 (from versions: 1.3.0, 1.4.1, 1.5.0, 1.5.1, 1.6.0, 1.6.1, 1.6.2, 1.7.0, 1.7.1, 1.7.2, 1.8.0, 1.8.1, 1.8.2, 1.9.0, 1.9.1, 1.9.2, 1.9.3, 1.10.0.post2, 1.10.1, 1.10.2, 1.10.4, 1.11.0b3, 1.11.0rc1, 1.11.0rc2, 1.11.0, 1.11.1rc1, 1.11.1, 1.11.2rc1, 1.11.2, 1.11.3, 1.12.0b1, 1.12.0rc1, 1.12.0rc2, 1.12.0, 1.12.1rc1, 1.12.1, 1.13.0rc1, 1.13.0rc2, 1.13.0, 1.13.1, 1.13.3, 1.14.0rc1, 1.14.0, 1.14.1, 1.14.2, 1.14.3, 1.14.4, 1.14.5, 1.14.6, 1.15.0rc1, 1.15.0rc2, 1.15.0, 1.15.1, 1.15.2, 1.15.3, 1.15.4, 1.16.0rc1, 1.16.0rc2, 1.16.0, 1.16.1, 1.16.2, 1.16.3, 1.16.4, 1.17.0rc1, 1.17.0rc2, 1.17.0)
ERROR: No matching distribution found for numpy==1.16.4-r1

Qu'est-ce que je rate?

2
maiermic

L'installer en tant que package python fonctionnera correctement car l'image de base est python, je vous recommanderai donc de l'installer en tant que package python. Comme l'installation avec le gestionnaire de packages Alpine, elle n'existe pas dans les packages python ni dans les packages Alpine. Donc, ci-dessous fonctionnera bien.

FROM python:3-Alpine
RUN apk add g++  && \\ 
pip install numpy

Maintenant, lancez le conteneur

docker run -it --rm abc python -c "import numpy"
0
Adiii