Docker - Pip 从目录安装

Docker - Pip install From Directory

我有文件夹 xyz,其中包含我需要使用 docker 进行 pip 安装的包。通常以下命令有效

pip install xyz/python

我的docker文件如下

FROM python:3.7

RUN mkdir /app
WORKDIR /app
RUN pip install xyz/python/

COPY . .

CMD ["./main.py"]

这是 运行 docker-compose up

的最终结果
Building test_py
Step 1/6 : FROM python:3.7
 ---> 7c891de3e220
Step 2/6 : RUN mkdir /app
 ---> Using cache
 ---> dda26c8c800e
Step 3/6 : WORKDIR /app
 ---> Using cache
 ---> 494a714d91ef
Step 4/6 : RUN pip install xyz/python/
 ---> Running in 099f6997979a
ERROR: Invalid requirement: 'xyz/python/'
Hint: It looks like a path. File 'xyz/python/' does not exist.
WARNING: You are using pip version 22.0.4; however, version 22.1.1 is available.
You should consider upgrading via the '/usr/local/bin/python -m pip install --upgrade pip' command.
ERROR: Service 'test_py' failed to build: The command '/bin/sh -c pip install xyz/python/' returned a non-zero code: 1

如评论中所述:

  • 你应该把COPY . .放在RUN pip install xyz/python
    之前 (否则 xyz 文件夹在图像构建期间在 docker 上下文中不可用)

  • RUN mkdir /appcan/should也被删除,因为WORKDIR /app本身就是相当于[=23的Dockerfile =]mkdir -p /app && cd /app.