Docker Compose and Django

In this post we are going to see how to build a web application using Django and Docker Compose.
Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design.
Docker-Compose is a tool that allow you for defining and running multi-container Docker applications (see my previous post for major details and to see how to install it).
I run this example on an Ubuntu 14.04 machine with Docker version 1.12.1 and Docker-Compose 1.8.0.

Create a directory where put the files needed for this example.

Create a Docker file (named Dockerfile, no extension needed)

This Docker file defines a basic image based on Python 2.7, creates a folder named code, adds the requirements.txt file to the folder and runs a pip install command.

We have to create now the requirements.txt file, with the following content:

These requirements are needed to run the Django web framework and to connect to a Postgresql db.

Create now a docker-compose.yml file that will contain our service definition: a Django web server a Postgresql database.

The command section defines the Django instruction to run the web server (see this link to the full documentation about

To build the Django web service container we can use the docker-compose run command.

After we run this command we can see that the Django project (compossexample folder) has been created.


The file created by the Django-admin are owned by the root user. Use this command to change the permission for those files.

We need now to edit the Django configuration file (composeexample/ to set the Database connection string. Edit the DATABASE section and add this new configuration (the parameters of the connection have been defined in the docker-compose.yml file).

Check this link to see the full Database Django configuration documentation.

To start the two containers we can user the docker-compose up command:

The both services (shipped in two different containers) are running:

To list all the container (with extra information like ID,Status, Names, ecc) you can use the docker ps command:


The Django web application is The Django web server is now running on port 8000.


In case we would like to connect to the running container to perform some Django operation (like create a super user) we can use the Docker excec command.

The id of the container is the one shown by the ps command.

We can now create a super user to login to our Django web application (http://ip_address:8000/admin).



We combine Docker and Docker-Compose to create two containers for our Django web application. The first container contains the Django web server and the second one contains the Postresql database. These containers can be hosted on the same machine in a development environment but can splitted up when the application is deployed to a production environment.