4

I am trying to setup a python virtual environment on a docker image running a docker build

The terminal output is ok when I run docker build .. but when I login into my container, no packages are installer in my virtual environment.

#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 

Here is what I get when I list pakages in my virtual environment:

$ 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. 

Is it the right way to do it? How to make sure packages will be installed?

6
  • you should upgrade pip first: /home/ubuntu/venv/bin/python -m pip install pip --upgrade (as the message suggests). Commented Sep 30, 2020 at 7:08
  • 5
    Do you have a specific need for a virtualenv in the container? If you don't, don't create one - the container environment is isolated from the outside world anyway. Commented Sep 30, 2020 at 7:12
  • my idea is to have a dev environment in a container with the possibility to deploy it easily. Commented Sep 30, 2020 at 7:22
  • Please don't post images with text. See also meta.stackoverflow.com/questions/303812/… Commented Sep 30, 2020 at 8:43
  • 1
    I believe an answer can be found in the thread of this question: stackoverflow.com/questions/48561981/… Commented Sep 30, 2020 at 9:12

2 Answers 2

2

Finally having a virtual environment is not mandatory in a container development environment, I just setup image python environment as desired.

# Dockerfile ... RUN python3.8 get-pip.py RUN pip install -r auto/requirements.txt 

This is not exactly what I was looking for but it does the job.

Sign up to request clarification or add additional context in comments.

1 Comment

a virtualenv in a container is still a good idea in some cases: isolating yourself from OS-level system packages (many parts of modern operating systems are implemented in python, especially ubuntu) and avoiding breaking the OS-level functionality
0

I use simple Dockerfile for dev mode

# 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 . . 

3 Comments

thanks but I cannot start from python:3.6-alpine image
Does this mean the pip is run as root in the container? I always get warning from pip saying it is not agood practice to run pip as root.
@doraemon Yes. By default you have root rights while building an image. This makes sense as it simplifies installing/configuring things. You can safely ignore pip's complaint.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.