Introduction
In this article, we are going to discuss what Docker Compose is, and its benefits. Also, we are going to talk about the Compose file and the basic commands available in Docker Compose.
Overview
Docker Compose is a tool for creating and running multi-container Docker applications by using a YAML file to configure your services. That allows you to run multiple services with a single command.
Life-Cycle
Docker Compose works in all stages of the software life cycle: development, testing, production, staging as well in CI workflows:
- Start, stop, and rebuild services
- View the status of running services
- Stream the log output of running services
- Run a one-off command on a service
Features
Docker Compose has some key features that make it effective:
- Provides multiple isolated environments on a single host (e.g. staging, dev, and production).
- Retains volume data upon container creation.
- Recreate only containers that have changed.
- Supports variables and moves a composition (which typically includes multiple services, networks, and volumes) from one environment to another (e.g. from stating to production)
The Compose File
The Docker Compose File is written in a YAML file, usually named docker-compose.yml
, and has a specific structure consisting of several main elements.
Letβs see a docker-compose.yml
example:
version: '3'
services:
web:
build: .
ports:
- "5000:5000"
volumes:
- "/usercode/:/code"
links:
- "database:backenddb"
database:
image: mysql/mysql-server:5.7
environment:
- "MYSQL_ROOT_PASSWORD=root"
- "MYSQL_USER=testuser"
- "MYSQL_PASSWORD=admin123"
- "MYSQL_DATABASE=backend"
volumes:
- "/usercode/db/init.sql:/docker-entrypoint-initdb.d/init.sql"
Basic Elements
Here's an overview of the basic and advanced elements and their purpose:
version
: specifies the version of the Docker Compose file format being used. However, the behavior of the version property has changed, and it no longer selects an exact schema for validation. Instead, it serves as an informative field to indicate the approximate version of the Compose file format.services
: is where you define the different services (containers) that make up your application. Each service is defined under a unique name and contains its configuration options: the base image, ports to expose, environment variables, volumes, network connections, and more. Please visit the following URL to get more details.networks
: you can define custom networks that your services can connect to. Networks enable communication between different services within the Docker Compose environment.volumes
: is used to define named volumes or bind mounts for persisting data. You can share and persist data between containers or between containers and the host system.
Advanced Elements
Here's an overview of the advanced elements and their purpose:
build
: Specifies the build context andDockerfile
for building a custom image for a service. It allows you to create a Docker image from a local directory or a Git repository.image
: Specifies the pre-built Docker image to use for a service. Instead of building an image, you can directly reference an existing image from a Docker registry.ports
: Maps the ports exposed by the container to the host machine. It defines the container's listening ports and the host machine's ports to bind to.config
: is used to define external configuration options for a service. The source of the config is either a file or external.secrets
: it allows you to manage sensitive data that your services may require (e.g. passwords, API Keys, certificates, etc). This works in conjunction with Docker Secrets, which are encrypted and secure files or in-memory values used to store sensitive data.environment
: Sets environment variables within the container. These variables can be used by the application running inside the container to configure its behavior.depends_on
: Defines the dependencies between services. It specifies the services that a particular service depends on, ensuring the dependent services are started before the dependent ones.restart
: Specifies the restart policy for a service. It determines the conditions under which a container should be restarted, such as always, on failure, or not.Deploy
: The deploy element is used for deploying the Docker Compose file to a swarm cluster. It provides options for scaling services, defining resource constraints, specifying update policies, and more.
You can mix and match these elements to customize your application's behavior and ensure smooth orchestration of your containers. For more details and advanced configuration options, refer to the official Docker Compose documentation: https://docs.docker.com/compose/.
Basic Commands
In this section, we are going to describe useful commands for managing the whole lifecycle of our application using Docker Compose.
docker-compose start
: Starts the containers defined in thedocker-compose.yml
file that is already created but stopped. It does not create any new containers. If there are containers that were previously running, they will be started again.docker-compose stop
: Stops the running containers defined in thedocker-compose.yml
file without removing them. It gracefully stops the containers by sending aSIGTERM
signal and waits for them to shut down. Usedocker-compose down
if you want to remove the containers.docker-compose pause
: Pauses the running containers defined in thedocker-compose.yml
file. The containers are frozen in their current state, and their processes are paused. This command can be useful when you need to temporarily halt the containers' execution.docker-compose unpause
: Resumes the paused containers. It restores their execution and allows them to continue running from where they were paused.docker-compose ps
: Shows the status of the containers defined in thedocker-compose.yml
file. It lists the running containers along with their status, including the container ID, name, command, and port mapping information. It's a helpful command to quickly check the running containers in your Compose setup.docker-compose up
: Builds, (re)creates, and starts the containers defined in thedocker-compose.yml
file. It will create any necessary images and networks, and then start the containers. If the containers are already created and running, it will only display their logs. By default, this command will attach to the containers' output, so you'll see the logs in your terminal.docker-compose down
: Stops and removes the containers, networks, and volumes defined in the Compose file. It gracefully shuts down the containers by sending aSIGTERM
signal and waits for them to stop before removing them. It also removes any networks and volumes that were created.
These are just a few examples of the available options and commands. You can refer to the Docker Compose documentation for a comprehensive list.
Nο»Ώote: the docker-compose
command can still be used for older versions of Compose (V1). However, if you are using Compose V2, it is recommended to use docker compose
instead.
Final Toughts
Great! That's all. We've explored the basics of Docker Compose, including its benefits, the structure of the Compose file, and various commands for managing containers.
Now it's your turn, don't hesitate to take that first step. Start using Docker Compose today and witness firsthand the incredible impact it can have on your containerized projects. Get ready to revolutionize your development workflow and embark on a journey of seamless container orchestration.