Using Docker instead of Virtual Environment for Django app development

Docker have all the good featured of virtual machine. It helps developer to set up an environment on development machine which is similar to production environment. Please go through official docker site if you want to know more about Docker.

In this article we will see how to develop a hello world Django project and will run it docker container instead of virtual environment.

Installing Docker:

Please follow this guide to install docker on your machine.

We are using Docker version 17.12.1-ce  for this article.

Starting docker container of application:

– Create your Django project. You may start with hello world Django project Or you can clone this github repo.

– Add requirements.txt  file and add django==1.10.0  in it.

– Create a Dockerfile  and add below text in it. Each step is explained using comment above it.

– Now lets create a docker image of our project. Run below command.

– Run a container using this newly created image.

– Now open another terminal and run the below command to see the running containers.

output will be something like this:

– Now go to localhost:8800/helloworld/  in your browser and you can see the template being rendered.

docker django– In the previous terminal where container is running, you can see the request hits.

docker django


Now if you change something in your code and refresh the browser, you won’t see the changes. To see the changes as soon as you update the code (just like running python server on host machine instead of docker container), we need to mount our current directory when running the container.

Stop the running container.

As soon as container is stopped, it is also removed. This is because of --rm flag used in docker run command, which tells docker to remove the container as soon as it is stopped.

Now re-run the container with below command where we are mounting the pwd  on host machine to /usr/src/app  directory of docker container.

You can provide absolute path as well instead of "$(pwd)" .

Now if you change your response text in  file, it will be reflected immediately in the browser.

You can see in the terminal output of docker container that python server is reloaded as you change and save some file.

docker django


Now you can develop your application without worrying about virtual environments.

Everytime you are going to develop an Django application, follow the above steps to start the application in container.
Summary of steps:
– Create project.
– Create Docker file.
– Create Image.
– Run container by mounting the pwd.


Complete code is available on Github.

Docker or Virtualenv or Both?

Whether one should completely replace virtual environment with Docker or not or use both, is debatable? Best approach would be to try both yourself.

Virtual Environment – A pocket guide.

Few good articles comparing virtual environment and docker:

Python: If you have Docker, do you need virtualenv?
Docker and Virtualenv? A clean way to locally install python dependencies with pip in Docker
Does virtualenv serve a purpose (in production) when using docker?
VirtualEnv lives.


Next Step:

In the next part of this article we will see how to use docker-compose  to run multiple container and how to use MySQL database in your dockerized Django application.


(Visited 659 times, 1 visits today)

You must read this :

Leave a Reply

Your email address will not be published. Required fields are marked *