Benefits of using Docker for Django projects

Docker. What is it? What’s all the hype about? Should you learn it? Should you use it? What benefits do you get from it compared to a virtual environment? In this article we attempt to answer these questions.

The idea behind Docker is not so different from the idea of a virtual environment. Both aim to provide an isolated environment for your different projects. One project might need Python version 3.6. Another might need the latest version of Python. One project may need Django while the other may not.

You would not, and should not, install all these dependencies globally on your machine. If you do that, you end up with a clashing mess of dependencies and there will be no way of knowing which project uses which version of Django, for example. This is where virtual environments come in handy. They provide us with isolated Python environments where we can install our dependencies regardless of what we have installed on our machine. These environments are independent and do not interfere with one another.

Docker (or Containerization) works the same way. Except that it goes a step further. While virtual environments provide a layer of isolation within the operating system (OS), Docker provides a level of isolation above the OS. Here is a simplified drawing to illustrate this:

Virtual Environments vs. Docker

By running separate containers on top of the OS, Docker provides a bunch of benefits for your Django project. Here are a few:

1. Replicate production environment locally

Let’s say you are working on a Django project on your Windows or Mac. If the project is already deployed, most likely it is running on some sort of Linux machine. This means the Python packages and binaries are slightly different between your production and local environments.

However, if you use Docker, you can make sure that your local development environment runs on the identical OS and Python runtime that you use in your production server.

2. Use any database without the need for local installation and maintenance

As in the picture above, without Docker, you have to install the database your Django project uses locally on your machine. So if your production runs on Postgres, you have to have that locally. Installing databases locally and maintaining them is not really fun (unless you are really into that kind of stuff). Most of the time, you just need a database that works so that you can get on with your development. With Docker, you can just run the database in a separate container. It just works out of the box. There are Docker images for all the major databases. (An image is like a recipe for creating a Docker container.)

3. Get new team members up and running quickly

When new people join the project, you need to make sure that they have the same development environment as you. This used to be a really tiresome process. But gone are the days when everybody had to have the same OS and run a bunch of shell scripts to set up their local development environments such that they were identical. With Docker, not only are you able to work on the project on a machine with a different OS than the server, your teammates can also work on the project even if they have a different OS than you. Imagine how much time and hassle you save by letting Docker take care of the differences rather than manually making sure everybody has the same development environment. A new team member only needs to install Docker on their machine. Then they can just download the code and start working on it right away!

4. Quickly experiment with dependencies

Have you ever tried to experiment with changing the Django version in a project? You need to create a new identical virtual environment as the one you have and in there change the version, fix any problem that comes up, and then update your original virtual environment with the changes. Then everybody in the project needs to update their virtual environments as well. No need for that with Docker. You can experiment all you want and if you are not satisfied, you just go back to your previous setup. All you need to do is to rebuild your Docker container. You can even swap out the database entirely if you like. All without the need to install or setup anything locally.

“Ok! this sounds convincing, how do I get started?”

Dockerizing your Django project is actually very simple. All you need to do is to install Docker and add two files to your project: Dockerfile and docker-compose.yml. I have written a step-by-step guide for dockerizing your Django project.

Of course, you don’t have to use Docker in every single Django project. When I work on small projects that are not going to be deployed, and I am not collaborating with others, I just use the good old virtual environment.

Use Docker if you know that the project is going to be deployed, you are using a database other than SQLite, or you are collaborating with others.

Subscribe via RSS