In this guide I show you how to run your Django app in one Docker container and your PostgreSQL database in another and make them talk to each other. We are going to manage and run these two containers using Docker Compose.
👉 If you want to learn how to do this for a Django + MySQL app read this post.
I am assuming you already have a Django project locally that is using a local database and that your project has a
requirements.txt file. You should also install Docker on your machine if you haven’t already.
Let’s dive right in:
The first step is to create a
Dockerfile in the root of your project (this is where you have your
Let’s go through that line by line:
FROM python:3.8is the base image your Docker image will be built upon. This will be pulled from DockerHub.
ENV PYTHONUNBUFFERED 1ensures that the output Django writes to the terminal comes out in real time without being buffered somewhere. This makes your Docker logs useful and complete.
WORKDIR /appcreates a new folder in your container called
appwhich will be your project’s root inside the container. It then sets that as the work directory in which the subsequent commands will be executed in.
COPY requirements.txt /app/requirements.txtcopies your local
requirements.txtfile to the Docker container.
RUN pip install -r requirements.txtwill make sure you have all your project dependencies installed inside the container.
COPY . /appand finally it’s time to copy your project’s content into the Docker container.
Now create a new file in your local project root. This one is called
docker-compose.yml and is used for configuring Docker Compose.
This defines two services: db and web.
db uses an official PostgreSQL image from DockerHub. We then set a bunch of environment variables that are required for PostgreSQL.
./db:/var/lib/postgresql/data/ creates a local
db folder in your project root so that database information can be saved if you destroy the db service.
The web service builds its image using the
Dockerfile we created earlier. It runs the Django server and exposes port 8000 to your machine.
We tell Docker Compose that this service is dependant on the db service.
No we need to update Django database settings to use the new PostgreSQL database:
Now all we have to do is to start everything up. In your project root run:
This should build your images and start the containers. Open a browser and go to
0.0.0.0:8000. You should see your app. Now if you open a new terminal and run a
docker ps you will see the web and db containers are running:
If you want to stop Docker Compose, press Ctrl+C in the first terminal or run
docker-compose stop in the project root folder in another terminal.
manage.py commands in the Django container (web):