DEV Community

Jaako
Jaako

Posted on

Virtualenv(pipenv) in Docker?

Is it necessary to use Virtualenv in Docker?

I am currently developing a fullstack project using Django Restframework and React.

I have already made a working Dockerfile and a docker-compose.yml for both React and Django but none of my colleagues are using Docker instead they are only using pipenv.

I am thinking of implementing pipenv inside a Docker but is it necessary? and also which one is the best?

Thank you...

Top comments (5)

Collapse
 
nicolaerario profile image
Nicola Erario • Edited

Docker is already a 'virtualization', so why use virtualenvs in it?
locally I use poetry (similar to pipenv but better imho): normally I generate a requirements.txt (with hashes) with poetry, pass it into container and then run pip install -r requirements.txt
EDIT: otherwise, if I remember well, you can pass to the container yaml also the pipfile.lock and then install dependencies, but, as said, I don't use pipenv

Collapse
 
jaakofalltrade profile image
Jaako

Docker is already a 'virtualization', so why use virtualenvs in it?

That is also what I am thinking, I am already passing requirements.txt to the container and pip installing it. But the thing is that I am the one who manages the package, I am the one who adds and removes packages from the requirements.txt.

Anyway thank you for your recommendation, I really appreciate it.

Collapse
 
smeyer profile image
Sven

I would say you don't need a virtual env in your docker container.
What you can do is use the pipenv arguments --deploy and --system which should install the pipenv requirements into your docker container's base python.

Collapse
 
jaakofalltrade profile image
Jaako

I will look into that suggestion, thank you I really appreciate it. Also nice picture I love that movie.

Collapse
 
paszavonpomiot profile image
PaszaVonPomiot • Edited

There is a case with docker multistage build where you install packages in compile-image and then copy python virtual environment to builder-image in order to make final image smaller. I have venv example only.

FROM python:alpine AS compile-image
ARG VENV=/opt/venv
COPY requirements.txt .
RUN apk add -U build-base linux-headers ca-certificates python3-dev libffi-dev libressl-dev libxslt-dev && \
    python -m venv $VENV && \
    source $VENV/bin/activate && pip install -r requirements.txt

FROM python:alpine AS build-image
ARG WORKDIR=/opt/django
WORKDIR $WORKDIR
ENV PATH="$WORKDIR/venv/bin:$PATH"
COPY --from=compile-image /opt/venv venv
Enter fullscreen mode Exit fullscreen mode

EDIT: Found pipenv example - tech.zarmory.com/2018/09/docker-mu...