A Practical Introduction to Docker Compose

Hiten Pratap Singh
TechCret Software
Published in
3 min readApr 30, 2020

In this post, we will learn about the Docker Compose and what things can be achieved using it. Docker Compose is mainly useful in managing multi-container application. In our previous post about “Deployment of Spring Boot application using Docker”, we did manage two containers and that’s just a plain application so we will see how Docker Compose helps us in managing scenarios like this.

Docker Compose

With applications getting larger and larger as time goes by it gets harder to manage them in a simple and reliable way. That is where Docker Compose comes into play. Docker Compose allows us developers to write a YAML configuration file for our application service which then can be started using a single command.

Spring Boot Application (ToDo Application)

We will try to run the application used in the previous blog using the Docker Compose. Source code of the application can be found here:

In our application, there are two services/components:

  1. Backend Application
  2. Database Service (MySQL)

Earlier we did run these components using Docker in traditional way. Now let’s see how Docker Compose file (YAML file) will look like:

docker-compose.yml

This “Docker Compose file” can be found the code repository as well.

Now let’s look at some of the keywords used in the Docker Compose file.

  1. version: version of the Docker Compose being used.
  2. services: Under this all the services will be listed like todo-mysql and todo-app in our project example.
  3. image: It’s used to mention the images name from which container will spin-up from. Image can be in the local system or hosted on some remote repository like in our example images is hosted on GitHub docker repository.
  4. networks: Used to mention the name of the network.

If we observer/focus a little here then we will find out that in the Docker Compose file, all the options are just as same as we had given in docker run command earlier.

Name of the Docker Compose file must be docker-compose.yml and following commands can be used to run/stop it.

  1. docker-compose up: This command is used to run the containers/services mentioned in the Docker Compose file.
  2. docker-compose down: This command is used to destroy all the containers/services started/run from the Docker Compose file.

Command Cheat Sheet

Docker Compose Commands

Docker Compose is a great tool to quickly deploy and scrap containers, the compose file can run seamlessly on any machine installed with docker-compose.

--

--