Docker series (part 3 of 4): Docker Compose
Do understand that so far we have built and managed only a single container, but typically most of the applications contain more than one container to serve its purpose. However, building and running multiple containers using multiple Dockerfiles is quite a cumbersome process.
This is where a tool like Docker Compose comes in handy. It helps you to manage multiple containers with ease. The best part is Docker Compose comes as a package along with Docker Desktop and there is no need to install it separately.
In this post, we are going to dive deep to understand how Docker Compose works and how it manages multiple containers effortlessly.
Table of Contents
- What is Docker Compose?
- What is a Compose file?
- How does Docker Compose work?
- What are the benefits of using Docker Compose?
What is Docker Compose?
A Docker Compose is a tool that helps you to define and run multi-container applications. Typically, an application will constitute more than one architecture components. For example, a database and an application server. To manage such multiple containers effortlessly, Docker Compose is used extensively by developers across the globe.
A Docker Compose tool comes with a compose file that can be used to define the application stack and how each container interacts with each other. This Compose file is a text file that can be created using your regular text editor. You can configure as many containers as you want in the Compose file and can define how to build them and where to store the data. And the best part is you can start or stop your entire application using a single command in Docker Compose.
Typically a developer who wishes to use Docker Compose to manage multiple containers have to go through these three steps:
If you recall from our previous post, a Dockerfile is another text file like a Compose file that contains instructions on how to build an image. This Dockerfile is converted into an image and then into a container, which is nothing but the running instance of an image. A Compose file is used to connect all these created containers and run them together as a complete application instead of running each container individually. To get a complete understanding of how Docker Compose works, we need to understand a bit more about a Compose file.
What is a Compose file?
A Compose file or a YAML file is a text file where all the services details along with networking and volume details are mentioned. The default name of the YAML file is docker-compose.yml. A YAML file is both human-readable and extremely easy to create. It is also versatile and allows you to use both .yml and .yaml formats.
A YAML file essentially consists of 4 parts:
- Version – This is nothing but the syntax version of the compose file. In case a version is not specified, version 1 will be considered as the default version.
- Services – This is the meat of the YAML file where you can find the container details that you will start or stop using Docker Compose.
- Networks – In this part of the compose file, you can define the default network of your application or connect it to an external network or even define any app-specific network.
- Volumes – The volumes section mounts the source directories on the host machine. In case a matching path exists, it will be overwritten by the mounted path.
Now that you have understood what a Compose file is, let us use to it explain how a Docker Compose works.
How Docker Compose works?
As a first step, build a YAML file that comprises of more than one service. The below screenshot is a YAML file depicting two services ‘helloworld’ and ‘database’ that we have created.
'3' here states the version of the syntax that we have used to build the compose file. The latest version is 3 and is also the recommended version.
The image under each service is nothing but the Docker Image that is retrieved from the Docker repository.
The restart instruction is used to instruct the Docker that the container should automatically restart in case of a crash. Similarly, the container_name is an optional instruction that replaces the randomly generated container name by the system.
Volumes denote the path of the shared directory between the container and the host while ports help us to access the server from outside the container or in other words from our computer.
The next step is to start both the containers together in the Docker Compose. Use the below command to do that -
You can always verify whether both the container are up and running using the below command -
The output of this command will display that the status of both the containers as up if the compose file has been defined correctly.
To bring down both the containers, you can use the below command –
What are the benefits of using Docker Compose?
1) Preserve volume data when containers are created
All the volumes of the services are stored in Compose. Whenever the compose file runs, it copies all the volumes from the old containers to the new containers when it finds any containers from a previous run. Volumes also ensure that no data is lost that was created previously.
2) Enhanced speed due to caching
Docker Compose caches the configurations of a container while creating it. This cached memory is used when restarting existing services that have not changed. This means an enhanced speed of making changes to the environment.
3) Support for variables
Docker Compose supports variables that help in customizing the composition for different environments. You can also define a block of text that can be reused in the compose file with the help of extension fields. This is often used when you need to set the same environment objects for multiple services.
Docker Compose has become an integral part of many applications thanks to its usefulness in managing multiple containers. Though there are various alternatives to Docker Compose, it is still preferred by many developers due to its simplicity and user-friendliness.
The next article, which will also be the last in the series, will explain in detail about Docker Swarm; another container orchestration tool with a different set of features and methods to manage multiple containers.