One of Docker’s selling points is its functionality for working with multiple containers quickly. This makes Docker suitable for building both monolithic applications and microservices.
Docker Compose is the tool Docker uses to achieve cross-container interaction and orchestration.
What Is Docker Compose?
Docker Compose is a simple container orchestration tool built on the Docker engine. It lets you work with multiple Docker containers. There are many reasons to use Docker for virtualization, from its scalability to its built-in version control.
You can use Docker Compose to build full-stack apps with multiple architectural patterns.
Docker Compose is an agnostic tool that powers many containerized microservice applications. With Docker Compose, you still get to use a single host environment for your application. But you avoid the hassles of multiple configurations that can compromise your app’s security and productivity.
Docker Compose doesn’t use Dockerfiles for builds. Instead, you’ll use a docker-compose.yaml file for your app’s configuration settings.
You can configure many different build settings in the YAML file. These include port mapping, environment variables, volumes and networks, and services.
Check Docker Compose Is Installed
The docker-compose program runs on the Docker engine. Docker Compose is part of a macOS or Windows installation of Docker CLI and Docker GUI.
Run this command to confirm it’s installed:
The command returns the docker-compose version, build number, and other related information.
If you don’t already have Docker Compose, you can install it by following the official Docker instructions. The Docker engine is a dependency for Docker Compose. You’ll need to ensure that you have Docker installed before attempting to install Docker Compose.
The Docker Compose YAML File
Docker Compose uses a .yaml file for build specifications. You’ll use the docker-compose file to define your apps’ services, networks, and volume configurations for your app’s build.
After writing the Dockerfile for your app, create a docker-compose file in the root directory of your working directory.
You’ll need to understand YAML files to use `docker-compose properly. Here’s how to specify build configurations in your docker-compose YAML file:
The version key should hold the version of Docker Compose you’re running.
In the services key, you can define the container configurations. The web key defines the service name. The build declaration defines the location of the docker file (the docker file path), and you can map the ports for your application in the ports key.
You can specify additional fields for your database and services in the docker-compose file.
Here’s an example of a Docker Compose file for a simple web application:
In this docker-compose file, the service will run version 3.9 of Docker Compose. It will build docker files in the root directory to run on port 8080 with a Redis database image and environment variables declared.
Docker Compose Commands
You can use many other commands with docker-compose for your container orchestration-related operations.
The build command builds or rebuilds the images in the docker-compose YAML file and creates the containers for your service.
The run command starts up your services as specified in the docker-compose file by creating the containers from the Docker images.
The images command lets you view a list of the images built from your docker-compose file.
The up command is the combination of the build and run commands. This command builds and runs the Docker images and starts the containers.
You can use the ps command to list all containers in the docker-compose file.
The down command stops and cleans up containers and images associated with the docker-compose file.
You’ll find the stop command useful if you only want to stop all containers and services in your docker-compose file.
Docker Promises to Ease Your Containerization Woes
Containerization tools existed before Docker, but Docker is one of the easiest to use.
Docker Compose offers easier container management, so using Docker can be more productive than other competing technologies.