Django with Docker: Add Postgres

January 24, 2018

In the last post I covered starting a new Django project with Docker, and how to build a custom container image for my project, but what I haven’t covered is how to use a Database and how persist your data since all data stored in the container ephemeral and would only exist during the life span of the container.

Instead of managing the Database inside my container we will use a ready made container, in my case I am going to use Postgres, and to solve data persistence issue we are going to mount Postgres data directory into a named volume.

But what about telling my Django app where to find the Postgres instance! Luckily this part is easily solved with the use of Docker Compose

Compose is a tool for defining and running multi-container Docker applications

We start with creating a new docker-compose.yml file describing what services our application consist of: