Docker Compose: Overview and Configurations
What is Docker Compose?
Docker Compose is a tool for defining and running multi-container Docker applications. With Docker Compose, you can use a YAML file to configure your application’s services, networks, and volumes. Then, with a single command, you can create and start all the services from your configuration.
Uses of Docker Compose
- Simplified Multi-Container Management: Easily manage multiple containers as a single application.
- Reproducible Environments: Define the exact environment in code, ensuring consistent setup across different environments (development, testing, production).
- Service Orchestration: Define dependencies between services, ensuring they start in the correct order.
- Configuration Versioning: Keep your environment configurations in version control along with your application code.
Key Docker Compose Commands
docker-compose up: Create and start containers.docker-compose down: Stop and remove containers, networks, images, and volumes.docker-compose build: Build or rebuild services.docker-compose logs: View output from containers.docker-compose exec: Execute a command in a running container.
Question
What is the difference between
docker-compose upanddocker-compose build? Does up also builds the containers?
Success
Yes it does, if it does not already exists
Docker Compose Configuration File (docker-compose.yml)
The docker-compose.yml file is where you define your application’s services, networks, and volumes. Below are some common configurations you can use:
Services
Defines the services (containers) in your application.
services:
web:
image: nginx:latest
ports:
- "8080:80"
volumes:
- ./webdata:/usr/share/nginx/html
networks:
- webnet
db:
image: postgres:latest
environment:
POSTGRES_PASSWORD: example
volumes:
- dbdata:/var/lib/postgresql/data
networks:
- webnetVolumes
Defines named volumes to persist data.
volumes:
dbdata:Networks
Defines custom networks to enable communication between containers.
networks:
webnet:Common Configuration Options
image
Specifies the Docker image to use for the service.
image: nginx:latestbuild
Specifies the build context and Dockerfile for the service.
build:
context: ./path
dockerfile: Dockerfileports
Maps ports between the host and the container.
ports:
- "8080:80"Note
The mapping is host:container, ie. container exposes port 80 which is mapped to port 8080 on the host machine
volumes
Mounts host directories or named volumes into the container.
volumes:
- ./host_directory:/container_directory
- dbdata:/var/lib/postgresql/dataenvironment
Sets environment variables for the container.
environment:
- POSTGRES_PASSWORD=exampledepends_on
Specifies service dependencies, ensuring they start in the correct order.
depends_on:
- dbnetworks
Connects services to custom networks.
networks:
- webnetcommand
Overrides the default command for the service’s container.
command: ["bundle", "exec", "puma"]Full Example docker-compose.yml
version: '3.8'
services:
web:
build:
context: .
dockerfile: Dockerfile
image: my_user/web:latest
ports:
- "8080:80"
volumes:
- ./webdata:/usr/share/nginx/html
networks:
- webnet
depends_on:
- db
db:
image: postgres:latest
environment:
POSTGRES_PASSWORD: example
volumes:
- dbdata:/var/lib/postgresql/data
networks:
- webnet
volumes:
dbdata:
networks:
webnet:Summary
Docker Compose simplifies the management of multi-container Docker applications by allowing you to define and run your services with a single command. You can configure various aspects of your application, including services, networks, and volumes, in the docker-compose.yml file.