- Docker Compose is a tool that allows you to define and manage multi-container docker applications with a single command.
- Docker Compose uses a YAML file to configure the application's services, networks, and volumes, enabling you to define and manage the entire stack in one place.
Example: Create a docker-compose.yml file and defines the services, networks, and volumes your application requires.
version: '3'
services:
web:
image: nginx
ports:
- "80:80"
mydb:
image: postgres
environment:
POSTGRES_PASSWORD: test123
Here,- version 3 is docker-compose file version so use all commands in this file as per version 3- services are containers
- Create and run services (services' containers) defined in the docker-compose.yml file
docker-compose up
Create and run services in the background
docker-compose up -d
You can specify an alternate docker compose file rather than default docker- compose.yml with 'f' option
- Stop and remove resources like containers, networks, and volumes created by docker-compose up command.
docker-compose down
- Stop the running services without removing them
Stop specific service
docker-compose stop mydb
- Force stop the running services
- Start the stopped services
- Restart the services
Restart specific service
docker-compose restart mydb
- Remove stopped services
docker-compose rm
- Restart services
- Pull service images
- Push service images
docker-compose push
- Display the logs from services.
docker-compose logs
You can use -f option to follow the logs in real-time
docker-compose logs -f
- Scale the "mydb" service to 3 instances
docker-compose up --scale mydb=3
- Execute a command inside a running service
docker-compose exec web ls /
- List images
- List running services
- Pause/Unpause services
docker-compose unpause
- Validate and view YAML file
- List all docker-compose commands
docker-compose --help
- Build or rebuild the Docker images defined in a docker-compose.yml file
docker-compose build
Please note docker skips building pre-built images like postgre etc if its there in your docker-compose file. So you can't build an image using above docker-compose.yml file in which we are using pre-built images nginx and postgre
Example: Building a docker image according to the Dockerfile.
mkdir my_workspacecd my_workspace
Step1: Create index.html</html><body>Hello, Docker Compose!</body></html>
Step2: Create DockerfileFROM nginx:alpineCOPY index.html /usr/share/nginx/html/index.html
Step3: Create docker-compose.yml fileversion: '3'services:web:build: .ports:- "8000:80"
👉 Here we are saying that docker should build an image from the Dockerfile located in the current directory (.)
Step4: Building a docker imagedocker-compose build
Step5: After building the image, you can start the container withdocker-compose up
you can access container with URL http://localhost:8000 in your web browser.
- Install docker-compose tool
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-composesudo docker-compose --version
Docker Compose tool simplifies working with multi-container docker applications, making it a powerful tool for both development and production environments.
Whatever things we do with a bunch of docker commands like creating images, containers, volume, network, scaling and stuff those all are simplified with docker-compose
Common Use Cases,
Microservices: Easily manage multiple microservices within a single application.
Local Development: Spin up local development environments that closely mirror production.
Testing: Use Docker Compose to run integration tests in isolated environments.
Local Development: Spin up local development environments that closely mirror production.
Testing: Use Docker Compose to run integration tests in isolated environments.