Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you can use a YAML file to configure your application’s services, networks, and volumes, and then spin up all the services with a single command. This guide covers the basics of Docker Compose, its configuration, and best practices.
Docker Compose allows you to define a multi-container application in a single file (docker-compose.yml). It manages the entire lifecycle of your application, including building, running, and scaling containers. This is especially useful for development, testing, and production environments.
- Simplicity: Define all your services, networks, and volumes in a single YAML file.
- Consistency: Easily reproduce and share environments across different machines and teams.
- Automation: Simplifies the orchestration of multi-container applications, enabling straightforward setup and teardown.
A basic docker-compose.yml file might look like this:
version: '3.8'
services:
web:
image: nginx:latest
ports:
- "80:80"
db:
image: postgres:latest
environment:
POSTGRES_DB: mydb
POSTGRES_USER: user
POSTGRES_PASSWORD: password- version: Specifies the Compose file format version.
- services: Defines the containers (services) that make up the application.
Here are some commonly used Docker Compose commands:
docker-compose upDescription: Starts and runs your services as defined in the docker-compose.yml file. Use the -d flag to run in detached mode.
Example:
docker-compose up -ddocker-compose downDescription: Stops and removes all containers defined in the docker-compose.yml file.
Example:
docker-compose downdocker-compose logsDescription: Shows logs from all services defined in the docker-compose.yml file.
Example:
docker-compose logsdocker-compose psDescription: Lists all running services defined in the docker-compose.yml file.
Example:
docker-compose psdocker-compose buildDescription: Builds or rebuilds services defined in the docker-compose.yml file.
Example:
docker-compose builddocker-compose run <service_name> <command>Description: Runs a one-time command against a service.
Example:
docker-compose run web bashYou can define networks in your docker-compose.yml file to enable communication between services.
version: '3.8'
services:
web:
image: nginx:latest
networks:
- frontend
db:
image: postgres:latest
networks:
- backend
networks:
frontend:
backend:Volumes are defined to persist data and share it between containers.
version: '3.8'
services:
db:
image: postgres:latest
volumes:
- db-data:/var/lib/postgresql/data
volumes:
db-data:Environment variables can be set directly in the docker-compose.yml file.
version: '3.8'
services:
web:
image: nginx:latest
environment:
- NGINX_HOST=example.com
- NGINX_PORT=80You can extend services by defining additional configurations in a separate file.
docker-compose.override.yml:
version: '3.8'
services:
web:
environment:
- NGINX_HOST=override.comCommand to use the override file:
docker-compose up- Use
.envFiles: Store environment variables in a.envfile to keep sensitive information secure. - Leverage
docker-compose.override.yml: Use overrides for development-specific configurations. - Keep
docker-compose.ymlClean: Organize services, networks, and volumes logically for readability and maintenance.
Docker Compose is an indispensable tool for managing multi-container applications. It simplifies the process of setting up, running, and scaling services, making it easier to develop and maintain complex applications. For more detailed information, refer to the Docker Compose Documentation.
Happy Dockerizing!