This guide demonstrates how to define and manage your multi-container Django, Nginx, and PostgreSQL application stack using Docker Compose. Compose simplifies the process of running multi-service applications compared to managing individual docker run
commands.
Before starting this lab, ensure you have completed the steps in the previous guide sections to have your Django application code, Dockerfiles (for Django, Nginx, and potentially Postgres if you built a custom one), and Nginx configuration ready.
sudo
/root privileges. (Needed for installing packages and managing Docker.)pip
(for installing Compose, although newer Docker versions often bundle it). (pip
is Python's package installer, used here if you need to install the standalone docker-compose
command.)python
, nginx
, and postgres
from Docker Hub.)Docker Compose allows you to define and run multi-container applications with a YAML file. Since Docker Engine v2.0, docker compose
(without the hyphen) is often included as a Docker plugin. If you are using an older version or need the docker-compose
(with hyphen) standalone binary, install it via pip.
(This ensures your package list is up-to-date. Python3 pip is needed to install the docker-compose
Python package.)
sudo apt update
sudo apt install -y python3-pip
(This command installs the standalone docker-compose
command-line tool using pip.)
sudo pip3 install docker-compose # installs docker-compose CLI
(Verify that the docker compose
command (or docker-compose
if using the pip install) is available and shows its version.)(Note: On newer Docker releases (generally Docker Engine 20.10+), docker compose
(no hyphen) is built-in and preferred. The pip install
provides the older, hyphenated docker-compose
command, which is still widely used.)
docker compose version # verify installation of the plugin
# or if using the pip install:
# docker-compose --version
docker-compose.yml
A docker-compose.yml
file is a YAML file that defines your application's services, networks, and volumes. Create this file in your project root directory (where your manage.py
, nginx
directory, and Dockerfile
are located).
(This file defines the entire multi-container application stack.)
Create a file called docker-compose.yml
in your project root:
# Specify the Docker Compose file format version
version: "3"
# Define the services (containers) that make up your application
services:
# Django web application service
djangotest:
# Build the image from the Dockerfile in the specified directory (relative to docker-compose.yml)
build: . # Assuming Django's Dockerfile is in the project root
# Explicitly state dependency; postgrestest must be started before djangotest
depends_on:
- postgrestest
# Attach this service to the custom network
networks:
- composenet01
# Always restart the container if it stops, unless explicitly stopped
restart: always
# Nginx reverse proxy service
nginxtest:
# Build the image from the Dockerfile in the specified directory
build: ./nginx # Assuming Nginx's Dockerfile is in ./nginx
# Explicitly state dependency; djangotest must be started before nginxtest
depends_on:
- djangotest
# Map port 80 on the host to port 80 in the container, making Nginx accessible externally
ports:
- "80:80"
# Attach this service to the custom network
networks:
- composenet01
# Always restart the container if it stops
restart: always
# PostgreSQL database service
postgrestest:
# --- Option 1: Build from a custom Dockerfile (as indicated in the original prompt) ---
# build: ./myPostgres03 # Assuming Postgres's Dockerfile is in ./myPostgres03
# --- Option 2: Use the official image (more common) ---
image: postgres:15.4 # Using the official PostgreSQL image from Docker Hub
# Set environment variables inside the container for database configuration
environment:
POSTGRES_USER: postgres # Sets the default user
POSTGRES_PASSWORD: mysecretpassword # Sets the password for the default user
POSTGRES_DB: postgres # Creates a database with this name
# Mount a named volume to the database's data directory for persistence
volumes:
- composevol01:/var/lib/postgresql/data
# Attach this service to the custom network
networks:
- composenet01
# Always restart the container if it stops
restart: always
# Network definitions
networks:
composenet01: # Define a custom bridge network
# Volume definitions
volumes:
composevol01: # Define a named volume for database data
(Note: The original prompt suggested build: ./myPostgres03
. While possible, it's much more common to use the official image: postgres:...
directly in docker-compose.yml
as shown in Option 2 above. I've included both as comments for clarity, but recommend using the image
approach.)
What’s here?
version: "3"
: Specifies the Compose file format version.