1

I'm used to using pip to install Python packages into my Django projects' virtual environments.

When I am working with a Divio Docker project locally, this does not work.

Daniele Procida
  • 1,477
  • 10
  • 27

1 Answers1

2

There are two things you need to be aware of when installing Python packages into a Docker project:

  • the package must be installed in the correct environment
  • if you want to use the installed package in the future, it needs to be installed in a more permanent way

The details below describe using a Divio project, but the principle will be similar for other Docker installations.

Installation in the correct environment

To use pip on the command line to install a Python package into a Dockerised project, you need to be using pip inside the Docker environment, that is, inside the container.

It's not enough to be in the directory where you have access to the project's files. In this respect, it's similar to using a virtual environment - you need to have the virtualenv activated. (Otherwise, your package will be installed not in the virtual environment, but on your own host environment.)

To activate a virtual environment, you'd run something like source bin/activate on it.

To install a package within a Divio web container:

# start a bash prompt inside the project
docker-compose run --rm web bash

# install the package in the usual way
pip install rsa

rsa is now installed and available to use.

More permanent installation

So far however, the package will only be installed and available in that particular container. As soon as you exit the bash shell, the container will disappear. The next time you launch a web container, you will not find the rsa package there. That's because the container is launched each time from its image.

In order to have the package remain installed, you will need to include it in the image.

A Divio project includes a requirements.in file, listing Python packages that will be included in the image.

Add a new line containing rsa to the end of that file. Then run:

docker-compose build web

This will rebuild the Docker image. Next time you launch a container with (for example) docker-compose run --rm web bash, it will include that Python package.

(The Divio Developer Handbook has some additional guidance on using pip.)

Note: I am a member of the Divio team. This question is one that we see quite regularly via our support channels.

Daniele Procida
  • 1,477
  • 10
  • 27