DEV Community

Paul Bennett
Paul Bennett

Posted on

Setting up Docker with Pipenv

I have always struggled with Docker as I mainly build front end web apps, where I have used services like Vercel to host my applications. Sometimes for work, I am required to build some scripts that need to run in a container.

And this where sh*t fits the fan generally. But no more!! I think I have finally cracked it, hello docker-compose I have always seen these files but never really used them.

I'll give you a rundown of how I am using my Dockerfile and docker-compose together to get python running with pipenv.

# For more information, please refer to
FROM python:3.9-slim


# Keeps Python from generating .pyc files in the container

# Turns off buffering for easier container logging

# Install & use pipenv
COPY Pipfile Pipfile.lock ./
RUN python -m pip install --upgrade pip
RUN pip install pipenv && pipenv install --dev --system --deploy

COPY . /app

# Creates a non-root user and adds permission to access the /app folder
RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /app
USER appuser

# During debugging, this entry point will be overridden. For more information, please refer to
CMD ["python", ""]
Enter fullscreen mode Exit fullscreen mode

This is my Docker file for running Python with Pipenv works well enough. This is generally the boilerplate from VSC when you add a Docker file to the workspace. I have only really replaced the requirements.txt and pip stuff with the below

COPY Pipfile Pipfile.lock ./
RUN python -m pip install --upgrade pip
RUN pip install pipenv && pipenv install --dev --system --deploy
Enter fullscreen mode Exit fullscreen mode

Which in turns copies my Pipfile & Pipfile.lock and then installs my dependencies.

If you have a file named .env in your project, it’s only used to put values into the docker-compose.yml file which is in the same folder. Those are used with Docker Compose and Docker Stack. It has nothing to do with ENV, ARG, or anything Docker-specific.

The key-value pairs, in your .env file are used to substitute dollar-notation variables in the docker-compose.yml file. It’s kind of a pre-processing step, and the resulting temporary file is used. This is a nice way to avoid hard-coding values. You can also use this to set the values for environment variables, by substituting the string, but that does not happen automatically.

Here is an example docker-compose.yml file, relying on values provided from a .env file:

version: '3'

    image: linuxserver/plex
        - env_var_name=${VARIABLE_NAME} # here it is
Enter fullscreen mode Exit fullscreen mode

Hint: When working with an .env file, you can debug your docker-compose.yml files quite easily. Just type docker-compose config. This way you’ll see how the docker-compose.yml file content looks after the substitution step has been performed without running anything else.

Once you're done just run docker-compose -f docker-compose.yaml up this will run a container with your image and all the specified env variables. Like anything it's easy when you understand it.

Top comments (1)

jacobitosuperstar profile image
Jacobo Mateo Bedoya Oquendo

Isn't the norm to use virtual environments inside the container too??. I can make my container run like this with no issues, the thing is how to do it while using a virtual environment?