The structure of a docker-compose.yml
file typically follows a hierarchical format using YAML syntax. Here's a breakdown of the common sections and their respective components:
π Version: This is the version of the Docker Compose file format youβre using. Itβs typically at the top of the file and specifies which features and syntax are available for use.
For example:
version: '3.8'
π Services: This section defines the various services or containers that make up your application. Each service is listed under the services
keyword and has its own configuration.
Here's an example:
services:
web:
image: nginx:latest
ports:
- "8080:80"
database:
image: postgres:latest
environment:
POSTGRES_USER: myuser
POSTGRES_PASSWORD: mypassword
π Networks: This section allows you to define custom networks for your services. Networks enable containers to communicate with each other over isolated networks. You can specify network configurations such as aliases, IP addresses, and external connectivity.
For example:
networks:
my_network:
driver: bridge
π Volumes: The volumes
section allows you to define volumes for persisting data or sharing data between containers. You can specify volume configurations such as source paths, target paths within containers, and read-only settings.
For example:
volumes:
my_volume:
driver: local
driver_opts:
type: 'none'
o: 'bind'
device: '/path/on/host'
π Configs: This section allows you to define configurations for your services using external files. Configurations can include environment variables, file paths, and other settings.
For example:
configs:
my_config:
file: ./config_file.txt
π Secrets: The secrets
section allows you to manage sensitive data securely. Secrets are encrypted and stored on the Docker host, and they can be accessed by services within your Docker Compose setup.
For example:
secrets:
my_secret:
file: ./secret_file.txt
π Extensions: Docker Compose supports extending and overriding configurations using the extends
keyword. This allows you to reuse common configurations across multiple services or environments.
For example:
services:
web:
extends:
file: common-services.yml
service: nginx
π Environment Variables: You can define environment variables for each service using the environment
key. This is useful for passing configuration settings to your containers.
For example:
services:
web:
image: nginx:latest
ports:
- "8080:80"
environment:
ENV_VAR1: value1
ENV_VAR2: value2
π Restart Policies: Docker Compose allows you to specify restart policies for your services, defining how they should behave in case of failures or when restarting. Options include no
, always
, on-failure
, and unless-stopped
.
For example:
services:
web:
image: nginx:latest
restart: always
π Depends On: You can define dependencies between services using the depends_on
key. This ensures that one service starts only after its dependent services are up and running.
For example:
services:
web:
image: nginx:latest
depends_on:
- database
database:
image: postgres:latest
π Logging: Docker Compose allows you to configure logging options for your services. You can specify logging drivers, log paths, and other logging-related settings.
For example:
services:
web:
image: nginx:latest
logging:
driver: "json-file"
options:
max-size: "10m"
max-file: "3"
π Healthchecks: You can define health check configurations for your services using the healthcheck
key. Health checks monitor the status of containers and can be used to determine if a service is healthy or not.
For example:
services:
web:
image: nginx:latest
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost"]
interval: 30s
timeout: 10s
retries: 3
π Build: If you need to build a custom Docker image for a service, you can use the build
configuration to specify the Dockerfile and build context. This is useful when you have custom requirements or need to add dependencies during the build process.
For example:
services:
web:
build:
context: ./myapp
dockerfile: Dockerfile.prod
π Command: You can override the default command for a container using the command
key. This allows you to specify a custom command or arguments when starting the container.
For example:
services:
web:
image: nginx:latest
command: ["nginx", "-g", "daemon off;"]
π Labels: Docker Compose supports adding labels to your services and containers using the labels
key. Labels provide metadata and can be used for organizing and identifying resources.
For example:
services:
web:
image: nginx:latest
labels:
myapp.version: "1.0"
myapp.environment: "production"
π User and Group: You can specify the user and group that a container should run as using the user
key. This allows you to control permissions and access within the container.
For example:
services:
web:
image: nginx:latest
user: "1000:1000"
π Extra Hosts: If your container needs to resolve hostnames to IP addresses outside of DNS, you can specify additional hosts using the extra_hosts
key. This is useful for development or testing environments.
For example:
services:
web:
image: nginx:latest
extra_hosts:
- "host1:192.168.1.1"
- "host2:192.168.1.2"
π Container Names: By default, Docker Compose generates container names based on the service names. However, you can override this behavior and specify custom container names using the container_name
key.
For example:
services:
web:
image: nginx:latest
container_name: my-nginx-container
π Ports Mapping with Protocol: In addition to specifying ports for container-to-host mapping, you can also define the protocol (TCP or UDP) for each port. This is useful when your service requires a specific protocol for communication.
For example:
services:
web:
image: nginx:latest
ports:
- "8080:80/tcp"
- "53:53/udp"
π Resource Limits: You can set resource limits such as CPU shares, memory constraints, and CPU quotas for your containers using the deploy.resources
key. This is particularly useful for managing resource usage and performance.
For example:
services:
web:
image: nginx:latest
deploy:
resources:
limits:
cpus: '0.5'
memory: '512M'
π Container Restart Policy: Docker Compose allows you to specify the restart policy for individual containers using the restart_policy
key. This overrides the global restart policy set in the services
section.
For example:
services:
web:
image: nginx:latest
restart_policy:
condition: on-failure
delay: 5s
max_attempts: 3
This structure provides a comprehensive way to define and configure Docker services, networks, volumes, configurations, and secrets within a single docker-compose.yml
file for your application.