4

What is the purpose of virtualenv inside a docker django application? Python and other dependencies are already installed, but at the same time it's necessary to install lots of packages using pip, so it seems that conflict is still unclear.

Could you please explain the concept?

EDIT: Also, for example. I'v created virtualenv inside docker django app and recently installed pip freeze djangorestframework and added it to installed in settings.py but docker-compose up raises error . No module named rest_framework.Checked, everything is correct.Docker/virtualenv conflict ? May it be?

Sebastian Wozny
  • 16,943
  • 7
  • 52
  • 69

2 Answers2

8

Docker and containerization might inspire the illusion that you do not need a virtual environment. distutil's Glpyh makes a very compelling argument against this misconception in this pycon talk.

The same fundamental aspects of virtualenv advantages apply for a container as they do for a non-containerized application, because fundamentally you're still running a linux distribution.

Debian and Red Hat are fantastically complex engineering projects. Integrating billions of lines of C code.For example, you can just apt install libavcodec. Or yum install ffmpeg.

Writing a working build system for one of those things is a PhD thesis. They integrate thousands of Python packages simultaneously into one working environment. They don't always tell you whether their tools use Python or not.

And so, you might want to docker exec some tools inside a container, they might be written in Python, if you sudo pip install your application in there, now it's all broken.

So even in containers, isolate your application code from the system's

Regardless of whether you're using docker or not you should always run you application in a virtual environment.

Now in docker in particular using a virtualenv is a little trickier than it should be. Inside docker each RUN command runs in isolation and no state other than file system changes are kept from line to line. To install to a virutalenv you have to prepend the activation command on every line:

RUN apt-get install -y python-virtualenv
RUN virtualenv /appenv
RUN . /appenv/bin/activate; \
    pip install -r requirements.txt

ENTRYPOINT . /appenv/bin/activate; \
           run-the-app
Sebastian Wozny
  • 16,943
  • 7
  • 52
  • 69
3

A virtualenv is there for isolating the packages to a specific environment. Docker is also there to isolate the settings to a specific environment. So in essence if you use docker there isn't much benefit of using virtualenv too.

Just pip install thing into the docker environment directly it'll do no harm. To pip install the requirements use the dockerfile where you can execute commands.

You can find a pseudo code example below.

FROM /path/to/used/docker/image
RUN pip install -r requirements.txt
Jonathan
  • 8,453
  • 9
  • 51
  • 74
  • what is the command to install something with pip to docker ? can't there be any path conflicts –  Feb 25 '17 at 15:01
  • @Vinand I added an example. There shouldn't be any path conflicts with this, as when you install with pip like that pip installs things globally in the docker environment. – Jonathan Feb 25 '17 at 15:09