[Django]-Is Docker an alternative for 'virtualenv' while develping Django project?

5👍

Managing Python versions and dependencies with virtualenv (or pipenv) will likely be your method of choice for your app running on localhost.

You can of course run your app exclusively in a Docker container, where you won’t have the need for any separate environments and will manage dependencies via the Dockerfile (and perhaps pip).

I’d suggest the following approach: run an instance of your app on localhost and simultaneously a production version in a Docker container. Also, run your database in a Docker container alongside (both managed via docker-compose.)

For a detailed walk-through on how to set this up (shameless plug), see this post or this GitHub repo

3👍

While in practice, you could, you really shouldn’t. Docker should in most cases be a method of distribution, rather than a way of isolating your development environment. That is to say that your final product — the Django project you produce — could be distributed as a docker image so it’s easily deployed (and scaled via Kubernetes), but distributing an image just to handle the development environment is definitely nonstandard and I wouldn’t recommend it.

Instead, consider Pipenv which neatly combines pip and venv into a human-digestible package. Store all that in your code repository, and setting up a development environment is as simple as

git clone
pipenv install --dev

1👍

Yes docker can be used as an alternative.

virtualenv just sets a fresh python installation in path which can be used to install dependencies particular to your project without polluting global python installation and thus managing versioning as well.

Docker is a step further and gives a complete OS – linux image and OS level isolation. Not just one can have a isolated python installation but can also have other os specific dependencies installed like ffmpeg, lapack, arpack etc. A new developer can spin up a container from docker image having all dependencies (python and OS) installed.

1👍

The usage of virtualenv or docker depends on your requirement or use case.

Docker container encapsulates an entire OS and provides isolation of OS whereas a virtualenv only encapsulates Python dependencies which enables switching between Python versions and dependencies, but you are dependent with your host OS.

Virtualenv can be used in case if you have two different projects on your machine that require two different versions of the same package with python. Typically, the virtualenv provides abstraction from OS’s use of python.

Docker enables portability from one machine to another whereas with virtualenv you will have to do the installation in another place. Docker isolates all of the additional system dependencies too and provides a completely isolated environment. It is containerization platform which is used to package your application and all its dependencies together in the form of containers so to make sure that your application works seamlessly in any environment which can be development or test or production. It helps to ensure that the Dev and Production environment are completely same.

0👍

DOCKERFILE

FROM python:3.9
ENV PYTHONUNBUFFERED=1
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
COPY . /code/

DOCKER-COMPOSE

version: '3.8'
services:
  backend:
    build: .
    command: python manage.py runserver
    volumes:
      - .:/code
    ports:
        - "8000:8000"

Leave a comment