如何在容器中安装pip包

我正在尝试在运行docker build的 docker 映像上设置 python 虚拟环境


当我运行docker build ..时,终端输出正常,但是当我登录容器时,我的虚拟环境中没有安装程序包。


#Dockerfile


RUN curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py

RUN python3.8 get-pip.py

RUN pip install virtualenv

RUN virtualenv venv

RUN /home/ubuntu/venv/bin/pip install -r auto/requirements.txt

...

# Docker build command

docker build --no-cache -t auto-server:1.0 .

# Terminal output


Step 27/27 : RUN /home/ubuntu/venv/bin/pip install -r auto/requirements.txt

 ---> Running in d27dbb9a4c97

Collecting asgiref==3.2.10

  Downloading asgiref-3.2.10-py3-none-any.whl (19 kB)

Collecting beautifulsoup4==4.9.1

  Downloading beautifulsoup4-4.9.1-py3-none-any.whl (115 kB)

Collecting Django==3.1.1

  Downloading Django-3.1.1-py3-none-any.whl (7.8 MB)


...


Successfully installed Django-3.1.1 asgiref-3.2.10 beautifulsoup4-4.9.1 fake-useragent-0.1.11 joblib-0.16.0 numpy-1.19.2 pandas-1.1.2 python-dateutil-2.8.1 pytz-2020.1 scikit-learn-0.23.2 scipy-1.5.2 six-1.15.0 sklearn-0.0 soupsieve-2.0.1 sqlparse-0.3.1 threadpoolctl-2.1.0

以下是我在虚拟环境中列出包时得到的结果:


$ docker exec -ti auto-server bash

root@9c1f914d1b7b:/home/ubuntu# source venv/bin/activate

(venv) root@9c1f914d1b7b:/home/ubuntu# pip list

Package    Version

---------- -------

pip        20.2.2

setuptools 49.6.0

wheel      0.35.1

WARNING: You are using pip version 20.2.2; however, version 20.2.3 is available.

You should consider upgrading via the '/home/ubuntu/venv/bin/python -m pip install --upgrade pip' command.


这是正确的做法吗?如何确保软件包将被安装?


蝴蝶刀刀
浏览 122回答 2
2回答

白板的微信

最后,在容器开发环境中,虚拟环境不是必需的,我只是根据需要设置图像Python环境。# Dockerfile...RUN python3.8 get-pip.pyRUN pip install -r auto/requirements.txt这并不完全是我想要的,但它可以完成工作。

DIEA

我使用简单的 Dockerfile 进行开发模式# pull official base imageFROM python:3.6-alpine# set work directoryWORKDIR /usr/src/app# set environment variablesENV PYTHONDONTWRITEBYTECODE 1ENV PYTHONUNBUFFERED 1# install psycopg2 dependenciesRUN apk update \    && apk add postgresql-dev gcc python3-dev# install dependenciesRUN pip install --upgrade pipCOPY ./requirements.txt .RUN pip install -r requirements.txt# copy projectCOPY . .
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python