docker compose
Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you define a multi-container application in a single file, then spin up your application with a single command. Here's a comprehensive reference for Docker Compose:
Docker Compose Basics:
1. Compose File Format:
Docker Compose files are written in YAML. The default file name is
docker-compose.yml, but you can use a different filename with the-foption.
2. Service Definitions:
Each service in a Compose file is defined under the
serviceskey. A service typically corresponds to a container.
services:
web:
image: nginx:latest3. Build Configuration:
You can specify build configurations for services, allowing you to build an image from a Dockerfile.
services:
web:
build:
context: ./path/to/build-context
dockerfile: Dockerfile.dev4. Container Naming:
By default, Docker Compose generates container names based on the project name and service name. You can customize container names using the
container_namefield.
services:
web:
container_name: my-nginx5. Environment Variables:
Use the
environmentkey to set environment variables for a service.
services:
web:
environment:
- MYSQL_ROOT_PASSWORD=mysecretpassword
- MYSQL_DATABASE=mydatabase6. Volumes:
Define volumes under the
volumeskey to persist data.
services:
web:
volumes:
- /path/on/host:/path/in/container7. Ports:
Use the
portskey to map ports from the container to the host.
services:
web:
ports:
- "8080:80"8. Networks:
Create custom networks for your services.
services:
web:
networks:
- frontend
networks:
frontend:9. Depends On:
Specify dependencies between services.
services:
web:
depends_on:
- db10. Command Override:
Override the default command for a service.
services:
web:
command: ["nginx", "-g", "daemon off;"]11. Links:
Define links between services. Note: The use of links is discouraged in favor of user-defined networks.
services:
web:
links:
- db12. Logging:
Control logging options.
services:
web:
logging:
driver: "json-file"
options:
max-size: "200k"
max-file: "10"13. Scale Services:
Use the
scalecommand to create multiple instances of a service.
docker-compose up --scale web=3Example Docker Compose File:
version: "3"
services:
web:
image: nginx:latest
ports:
- "8080:80"
db:
image: postgres:latest
environment:
POSTGRES_PASSWORD: exampleDocker Compose Commands:
1. docker-compose up:
Build and start services defined in the Compose file.
docker-compose up2. docker-compose down:
Stop and remove containers, networks, and volumes.
docker-compose down3. docker-compose build:
Build services defined in the Compose file.
docker-compose build4. docker-compose ps:
List running containers.
docker-compose ps5. docker-compose logs:
View output from containers.
docker-compose logs6. docker-compose exec:
Execute commands in a running container.
docker-compose exec service_name command7. docker-compose config:
Validate and view the Compose file configuration.
docker-compose config8. docker-compose pause/unpause:
Pause or unpause services.
docker-compose pause
docker-compose unpauseThese are the fundamental concepts and commands for using Docker Compose. Docker Compose simplifies the management of multi-container Docker applications and is especially useful for development, testing, and staging environments.