Docker containers can be started using command-line arguments, but as applications become more complex, managing them manually becomes difficult. Docker Compose solves this problem by allowing you to define containers, networks, volumes, and environment variables in a single YAML configuration file.
A Docker Compose file makes application deployment easier, more consistent, and easier to maintain.
Table of Contents
Example Docker Compose File
services:
app:
container_name: my_container
image: ubuntu:latest
ports:
- "8080:80"
- "443:443"
environment:
MYSQL_ROOT_PASSWORD: my_password
MYSQL_DATABASE: my_database
MYSQL_USER: my_user
MYSQL_PASSWORD: my_database_password
volumes:
- ./data:/app/data
command: ["npm", "start"]
restart: always
Understanding the Configuration
container_name
The container name defines how the container will appear in Docker.
container_name: my_container
Without this option, Docker automatically generates a random name.
image
The image specifies which Docker image should be used when creating the container.
image: ubuntu:latest
Docker will automatically download the image if it is not already available on the server.
ports
Port mappings allow external traffic to reach services running inside the container.
ports:
- "8080:80"
- "443:443"
In this example:
- Port 8080 on the server forwards traffic to port 80 inside the container.
- Port 443 on the server forwards traffic to port 443 inside the container.
environment
Environment variables provide configuration values to applications running inside the container.
environment:
MYSQL_ROOT_PASSWORD: my_password
MYSQL_DATABASE: my_database
MYSQL_USER: my_user
MYSQL_PASSWORD: my_database_password
These values are commonly used by databases, web applications, and other services.
volumes
Volumes allow data to persist even if the container is removed and recreated.
volumes:
- ./data:/app/data
In this example:
./datais a directory on the host server./app/datais the directory inside the container.
Any changes made in either location are synchronized.
command
The command option overrides the image’s default startup command.
command: ["npm", "start"]
When the container starts, Docker executes this command.
restart
The restart policy controls what happens if a container stops unexpectedly.
restart: always
Common options include:
| Policy | Description |
|---|---|
| no | Never restart automatically |
| always | Always restart the container |
| unless-stopped | Restart unless manually stopped |
| on-failure | Restart only after failures |
Starting the Application
Save the configuration as:
docker-compose.yml
Then start the container:
docker compose up -d
The -d flag runs the container in the background.
Viewing Running Containers
To display running containers:
docker ps
To view application logs:
docker compose logs
To stop the application:
docker compose down
Example: WordPress with MariaDB
Docker Compose is often used to deploy multiple services together.
services:
db:
image: mariadb:latest
environment:
MYSQL_ROOT_PASSWORD: strongpassword
MYSQL_DATABASE: wordpress
wordpress:
image: wordpress:latest
ports:
- "8080:80"
environment:
WORDPRESS_DB_HOST: db
WORDPRESS_DB_NAME: wordpress
WORDPRESS_DB_PASSWORD: strongpassword
This configuration launches both a WordPress container and a MariaDB database container that automatically communicate with each other.
Summary
Docker Compose allows you to define container configurations in a simple YAML file instead of managing long Docker commands manually. It simplifies application deployment by centralizing configuration such as images, ports, environment variables, storage volumes, and restart policies.
For modern applications that use multiple containers, Docker Compose is one of the most commonly used tools for local development, testing, and small-to-medium production environments.