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?

blondelg
  • 916
  • 1
  • 8
  • 25
  • you should upgrade pip first: `/home/ubuntu/venv/bin/python -m pip install pip --upgrade` (as the message suggests). – hiro protagonist Sep 30 '20 at 07:08
  • 4
    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. – AKX Sep 30 '20 at 07:12
  • my idea is to have a dev environment in a container with the possibility to deploy it easily. – blondelg Sep 30 '20 at 07:22
  • Please don't post images with text. See also https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors – tripleee Sep 30 '20 at 08:43
  • 1
    I believe an answer can be found in the thread of this question: https://stackoverflow.com/questions/48561981/activate-python-virtualenv-in-dockerfile – Attila Viniczai Sep 30 '20 at 09:12
  • thanks, this confirms that virtual environment in a container does not make sens – blondelg Sep 30 '20 at 09:14

2 Answers2

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.

blondelg
  • 916
  • 1
  • 8
  • 25
  • 3
    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 – anthony sottile Sep 30 '20 at 16:18
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 . .
id1
  • 221
  • 2
  • 6
  • thanks but I cannot start from python:3.6-alpine image – blondelg Sep 30 '20 at 07:18
  • 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 Jan 02 '22 at 03:22
  • @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. – András Aszódi Jun 23 '23 at 09:37