我正在嘗試在運行docker build的 docker 映像上設置 python 虛擬環境當我運行docker build ..時,終端輸出正常,但是當我登錄容器時,我的虛擬環境中沒有安裝程序包。#DockerfileRUN curl https://bootstrap.pypa.io/get-pip.py -o get-pip.pyRUN python3.8 get-pip.pyRUN pip install virtualenvRUN virtualenv venvRUN /home/ubuntu/venv/bin/pip install -r auto/requirements.txt...# Docker build commanddocker build --no-cache -t auto-server:1.0 .# Terminal outputStep 27/27 : RUN /home/ubuntu/venv/bin/pip install -r auto/requirements.txt ---> Running in d27dbb9a4c97Collecting 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 bashroot@9c1f914d1b7b:/home/ubuntu# source venv/bin/activate(venv) root@9c1f914d1b7b:/home/ubuntu# pip listPackage Version---------- -------pip 20.2.2setuptools 49.6.0wheel 0.35.1WARNING: 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.這是正確的做法嗎?如何確保軟件包將被安裝?
2 回答

白板的微信
TA貢獻1883條經驗 獲得超3個贊
最后,在容器開發環境中,虛擬環境不是必需的,我只是根據需要設置圖像Python環境。
# Dockerfile
...
RUN python3.8 get-pip.py
RUN pip install -r auto/requirements.txt
這并不完全是我想要的,但它可以完成工作。

DIEA
TA貢獻1820條經驗 獲得超2個贊
我使用簡單的 Dockerfile 進行開發模式
# pull official base image
FROM python:3.6-alpine
# set work directory
WORKDIR /usr/src/app
# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# install psycopg2 dependencies
RUN apk update \
&& apk add postgresql-dev gcc python3-dev
# install dependencies
RUN pip install --upgrade pip
COPY ./requirements.txt .
RUN pip install -r requirements.txt
# copy project
COPY . .
添加回答
舉報
0/150
提交
取消