Docker Compose
How to Use Docker Compose
Docker Compose is a tool for defining and running multi-container Docker applications. It allows you to define all your application's services (containers), networks, and volumes in a single docker-compose.yml file, making it easy to manage your entire application stack with a single command.
As a backend developer, Docker Compose is extremely useful when dealing with multiple components simultaneously, such as databases, caches, web servers, and API servers.
Why Use Docker Compose?
- Single Definition for Application Stack: With just one docker-compose.yml file, you can clearly define all services (containers), networks, and volumes that constitute your application.
- Easy Environment Sharing: It allows for consistent sharing and deployment of development environments among team members. By simply sharing the docker-compose.yml file, anyone can easily set up the identical environment.
- Simplified Start/Stop: Without complex commands, a single docker compose up command starts all defined services in their specified order, and docker compose down cleanly stops and removes the entire stack.
- Service Interconnectivity: Compose automatically sets up networking between defined services, allowing them to communicate with each other using their service names (e.g., an application container can access the database container using the hostname db).
- Optimized for Development Environments: It's particularly powerful for quickly setting up and testing complex application environments, especially in development and testing, rather than production.
Steps to Use Docker Compose
Here are the typical steps to use Docker Compose:
1. Verify Docker Compose Installation
Most modern Docker Desktop installations include Docker Compose. You can verify its installation by running the following command in your terminal:
docker compose version
# or docker-compose version (for older versions)
If it's not installed, you'll need to refer to the official Docker documentation for installation instructions.
2. Create a docker-compose.yml File
Create a file named docker-compose.yml (or docker-compose.yaml) in your project's root directory. This file is written in YAML format and defines your application's services, networks, and volumes.
Example: Simple Web Application (Python Flask + PostgreSQL)
docker-compose.yml file content:
version: '3.8' # Docker Compose file format version
services:
web: # Web application service
build: . # Build image using the Dockerfile in the current directory
ports:
- "5000:5000" # Map host port 5000 to container port 5000
volumes:
- .:/app # Bind mount the current directory to the /app directory in the container
environment: # Set environment variables
- FLASK_APP=app.py
- DATABASE_URL=postgresql://user:password@db:5432/mydatabase
depends_on: # db service must start before this service
- db
db: # PostgreSQL database service
image: postgres:13 # Use PostgreSQL 13 image
environment: # Database environment variables
- POSTGRES_DB=mydatabase
- POSTGRES_USER=user
- POSTGRES_PASSWORD=password
volumes:
- db_data:/var/lib/postgresql/data # Use a Named Volume named db_data for data storage
volumes: # Define volumes
db_data: # Create a Named Volume named db_data
In this example, two services (web and db) are defined:
- The web service builds the web application using the Dockerfile in the current directory, maps host port 5000, and mounts the current code into the container. It depends on the db service.
- The db service uses the PostgreSQL 13 image, sets necessary environment variables, and ensures data persistence using a volume named db_data.
3. Create a Dockerfile (if necessary)
Since the web service in the docker-compose.yml example uses build: ., you'll need a Dockerfile in the same directory to build your web application.
Example: Dockerfile (for the web application)
# Dockerfile (located in the same directory)
FROM python:3.9-slim-buster
WORKDIR /app
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["flask", "run", "--host=0.0.0.0"]
4. Run the Application
Navigate to the directory containing your docker-compose.yml file and run the following command:
docker compose up
# Or for background execution: docker compose up -d
- docker compose up: Builds and starts all services defined in the docker-compose.yml file. Adding the -d option runs all containers in detached mode (background).
- Compose automatically determines the correct startup order, considering depends_on (e.g., db will start before web).
- The first time you run this, it might take some time as images may need to be downloaded and built.
5. Stop and Remove the Application
To stop your application and remove associated containers and networks, use the following command:
docker compose down
- docker compose down: Stops and removes all containers and default networks started by docker compose up.
- Caution: By default, this command does not remove volumes. If you want to completely remove volumes as well, add the -v (or --volumes) option:
Bash
docker compose down -v
6. Useful Docker Compose Commands
- docker compose ps: Shows a list of services currently running within your Compose project.
- docker compose logs [service_name]: Displays the logs for a specific service (e.g., docker compose logs web).
- docker compose logs -f: Streams logs from all services in real-time.
- docker compose restart [service_name]: Restarts a specific service.
- docker compose build [service_name]: Rebuilds the image for a specific service (when changes have been made).
- docker compose exec [service_name] [command]: Executes a command inside a running container of a specific service (e.g., docker compose exec web /bin/bash).