apk python 软件包未安装在 Alpine Docker 图片中

apk python packages not installing in Alpine Docker Image

我有一个 dockerfile 如下:

FROM python:3.7.5-alpine3.10

RUN apk update

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

RUN apk add --no-cache cython3

CMD [ "sh", "ls"]

当我进入带有 docker run -it --rm mycontainer /bin/sh 的容器时,cython 似乎没有安装。我错过了什么?

/usr/src/app # which python
/usr/local/bin/python
/usr/src/app # python
Python 3.7.5 (default, Oct 21 2019, 20:13:45) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cython
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'cython'

Alpine 使用此路径 /usr/lib/python3.7/site-packages 安装了 python 包,只需 运行 容器内的命令,您就会看到包已安装。您只需将此路径添加到 python 搜索路径即可。

RUN apk add --no-cache cython3
ENV PYTHONPATH /usr/lib/python3.7/site-packages

PYTHONPATH

Augment the default search path for module files. The format is the same as the shell’s PATH: one or more directory pathnames separated by os.pathsep (e.g. colons on Unix or semicolons on Windows). Non-existent directories are silently ignored.

In addition to normal directories, individual PYTHONPATH entries may refer to zipfiles containing pure Python modules (in either source or compiled form). Extension modules cannot be imported from zipfiles.

The default search path is installation dependent, but generally begins with prefix/lib/pythonversion (see PYTHONHOME above). It is always appended to PYTHONPATH.

An additional directory will be inserted in the search path in front of PYTHONPATH as described above under Interface options. The search path can be manipulated from within a Python program as the variable sys.path.

python envvar PYTHONPATH

更新:

要使用 pip 安装,您需要使用 -m

When called with -m module-name, the given module is located on the Python module path and executed as a script.

python3-cmdline

你可以测试

RUN apk add --no-cache cython3
ENV PYTHONPATH /usr/lib/python3.7/site-packages
RUN python -m pip install requests
RUN python -m pip list
#import test
RUN python -c "import requests"