The First Step When Dockerizing Your Django App - Use Environment Variables

Dockerizing your Django application can seem like a daunting task. There is a lot to learn, new exciting mistakes to make and you’ll need to get used to new workflows.

You’ll also need to prepare your project to be Docker-ready. You’ll have the best experience if you make your project Docker-ready before you try to cram it into a container. This way you won’t have to fight your application’s setup at the same time as you figure out Docker.

Establish Clear Boundaries

One of the core premises of Docker, is that dockerized payloads operate are in a nice and isolated environment of their own. They don’t have to care about where they run, as long as it’s inside of their own container.

There is a clear separation of concerns. The dockerized workload is responsible for doing its thing, and Docker is there to provide it with the environment it needs to do so.

Establishing a clear boundary between your Django project and the environments where it will run is a great step. You can think of the configuration layer as this boundary, beyond which your Django application does not need to know anything anymore. Everything it needs is within the configuration information. The responsibility of your app to figure out the outside environment ends at that configuration boundary.

You want to have an explicit interface - one true way to configure your application. The easier this is to do, the better. That configuration layer, should make it possible to configure your application dynamically, without making changes to the code.

Your app should know exactly where to look for configuration values and what it needs to know to do a good job. It should also refuse to run if it doesn’t have all the necessary information.

Environment Variables To The Rescue

Think of environment variables, as an interface to configure your application without having to deal with its internals.

Your Django application can check for all the environment variables it needs when it first starts. If it finds all expected values, it can use them to adjust its configuration and begin working.

On the other hand, it’s almost always possible to set environment variables - no matter if you’ll be deploying your application to a VPS, whether you’ll be running it inside of a Docker container or without it.

Your app won’t care where it runs as long as the environment variables it needs are present.

Start By Making Your Application Use Environment Variables

Making your Django project configurable with environment variables, is a great way to prepare it for running inside of Docker.

Check out django-environ - it’s the tool you want to be using.

Go through your settings file, and replace any configuration value which might need to be adjusted between your current environment, and a dockerized environment to use VARIABLE_NAME = env('VARIABLE_NAME').

You can set default values, but keep in mind that default values can turn out to be error-prone. If the default value does not apply to your new environment, you might miss to set it properly, and spend time debugging a weird avoidable error.

If you instead configure your Django project to expect every single important variable which can change between environments, you’ll make it easier for yourself to figure out missing values faster. After all, django-environ will raise an ImproperlyConfigured exception if it notices that an expected value is missing.

Running Locally With Environment Variables

Once you have started relying on environment variables to configure your application, you can try to set them for your development environment. Look at the postactivate script of your virtualenv, and add a new environment variable for each of your setting variables:

export VARIABLE_NAME=the_value_you_need

Deactivate your virtualenv session, and activate it again to make the changes apply, or source path/to/your/postactivate to load the new variables.

Try running your development server now - does everything work as expected? If so, congratulations! You are now using environment variables to configure your application.

Next Steps

Using environment variables, is a great way to prepare for dockerizing your Django project. By doing so, you are making sure that your app can be configured to run in a new environment, without making any internal changes to it.

Now, once you start dockerizing it, you’ll have an easier time fixing configuration errors and adjusting it to a running inside of a container. You won’t have to get to know Docker at the same time as adjusting your application to be ready to fit inside of a Docker image. I hope that this article has helped you to make the first step towards using Docker for your Django app.

If you want to learn more about building Docker images, and configuring your dockerized app, check out this in-depth guide to get a complete overview of Docker ARG, ENV, env_file and docker-compose .env files.