Using Pipenv with Docker

In this tutorial I’ll show you how to use pipenv and Pipfile.lock file in your docker containers.

You can see the source code here: GitHub – jonathanmeier5/django-project-configuration at tutorial/lock-files-with-docker

Create a Dockerfile

Now let’s create a Dockerfile:

Dockerfile

FROM python:3.8

RUN pip install pipenv

ENV PROJECT_DIR /usr/local/src/webapp

WORKDIR ${PROJECT_DIR}

COPY Pipfile Pipfile.lock ${PROJECT_DIR}/

RUN pipenv install --system --deploy

Nothing here is out of the ordinary except for our pipenv install --system --deploy RUN command.

This tells pipenv that rather than create a virtualenv with our installed packages, we should install them directly in the the container’s system python.

Finally, the --deploy flag tells pipenv to blow up if the lock file is out of date. That is, if the requirements specified in the Pipfile no longer align with the hashes specified in the lock file.

test things work

docker build . -t lockfile:local

Now let’s test things work by building the image.

As we can see, our packages are installing.

confirm things are working

docker run -it lockfile:local bash
pip freeze

Now let’s run the image and confirm our packages are present.

As you can see, all our packages installed successfully to the system python distribution in the container.

And that’s it, you can now use pipenv lock files in your docker containers.

Leave a Reply

Your email address will not be published.