Edited 1 week ago by ExtremeHow Editorial Team
DockerContainersApplication DeploymentDocker ComposeOrchestrationConfigurationScriptingDevOpsCommand LineMulti-Container
This content is available in 7 different language
Docker Compose is a tool used to define and run multi-container Docker applications. With Docker Compose, you can use a YAML file to configure your application's services and then you can use a single command to create and start all the services from your configuration. This is incredibly useful for working with complex applications containing many linked services. The purpose of this guide is to explain using Docker Compose on Linux in a very detailed way.
Before diving into Docker Compose, it's important to understand what Docker is. Docker is a platform that enables developers and system administrators to package applications into containers — standardized executable components that combine application source code with the operating system libraries and dependencies needed to run that code in any environment.
Docker Compose takes this a step further by allowing you to configure and start multiple Docker containers as a single service. This means you can manage all the dependencies of your application using a simple file called docker-compose.yml
. This file contains information about which image to run, which ports to expose, volume mappings, and more.
The first step in using Docker Compose is to make sure Docker is installed and running on your Linux machine. Most modern Linux distributions come with Docker in their official repositories, but you can also install it from Docker's official repository to get the latest version.
# Update the apt package index and install packages to allow apt to use a repository over HTTPS
sudo apt-get update -y
sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
software-properties-common -y
# Add Docker's official GPG key & Verify
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
# Set up the stable repository
sudo add-apt-repository \
"deb [arch=$(dpkg --print-architecture)] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"
# Install Docker CE
sudo apt-get update -y
sudo apt-get install docker-ce docker-ce-cli containerd.io -y
# Verify the Docker installation
sudo docker run hello-world
Once Docker is installed, the next step is to install Docker Compose. Unlike Docker, Compose is a separate tool. You can download Docker Compose from the GitHub release page.
# Download the current stable release of Docker Compose
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
# Apply executable permissions to the binary
sudo chmod +x /usr/local/bin/docker-compose
# Verify that Docker Compose is installed correctly
docker-compose --version
docker-compose.yml
file is the core of using Docker Compose. This is where you define all of your services (essentially, your application's containers) and how they interact with each other. The structure of docker-compose.yml
file is fairly straightforward. It typically starts by defining the version of Compose being used, followed by a list of services.
Here's a basic example of a docker-compose.yml
file:
version: '3.8'
services:
web:
image: nginx:latest
ports:
- "80:80"
database:
image: mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: example
In the example above, the compose file is using version 3.8. It defines two services, web
and database
. web
service uses the Nginx image and maps port 80 of the container to port 80 of the host. database
service uses the MySQL image with the root password specified.
Once you have configured your docker-compose.yml
file, your multi-service application can be run with a simple command. Navigate to the directory containing docker-compose.yml
file and run:
docker-compose up
docker-compose up
command searches for docker-compose.yml
file in the current directory, creates the required images if they are not available locally, and starts the defined services.
If you want to run the application in the background (isolated mode), you can use the -d
option:
docker-compose up -d
Docker Compose provides several commands to help you manage your environment:
docker-compose ps
: Displays the status of the services defined in the compose file.docker-compose start
: Starts an existing container for a service.docker-compose stop
: Stops containers from running, without removing them.docker-compose down
: Stops and removes all containers defined in the docker-compose.yml
file.docker-compose logs
: Retrieves logs for a service.Docker Compose also allows you to define volumes and networks in your docker-compose.yml
file. Volumes are used to persist data and networks help in communication between services.
To define a volume, add the following under the Services section:
services:
database:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
volumes:
db_data:
This configuration creates a named volume db_data
and mounts it at /var/lib/mysql
inside the database service container, which stores MySQL data.
The network can also be customized:
services:
frontend:
image: node:14
networks:
- frontend-network
backend:
image: node:14
networks:
- backend-network
networks:
frontend-network:
backend-network:
A powerful feature of Docker Compose is the ability to scale services. This is especially useful for simulating more realistic conditions in a development environment. You can scale a service with --scale
option.
docker-compose up --scale web=3
The above command will start three instances of web
service.
You can execute arbitrary commands inside your running services using docker-compose exec
:
docker-compose exec web bash
This command opens a bash shell inside the running web
service container.
By default, Docker Compose creates a default network for your services. However, you can bind all services to the same network by defining a network and specifying it in each service:
version: '3.8'
services:
web:
image: nginx:latest
networks:
- custom-network
database:
image: mysql:5.7
networks:
- custom-network
networks:
custom-network:
web
and database
services are both connected to custom-network
, allowing them to communicate independently.
Docker Compose is a robust tool that simplifies the process of managing multi-container Docker applications. By defining services in docker-compose.yml
file, you can quickly deploy and manage your application's stack. The key to effective use of Docker Compose is to understand the configuration options of the docker-compose.yml
file, including services, volumes, and networks.
This comprehensive guide provides an overview of how to set up Docker and Docker Compose on Linux, create a Compose file, and manage your application. With this knowledge, you can effectively leverage Docker Compose to handle complex Linux-based environments. Practice with your own applications to become more familiar and customize Docker Compose configurations to meet specific needs.
If you find anything wrong with the article content, you can