What is Docker Compose: Benefits and Basic Commands

What according to you is the important feature of any web-based application? There are many, but for me, it's high productivity and that’s what Docker Compose does.

Before directly jumping to Docker Compose, let us understand what is Docker and Docker Containers.

Docker is a widely-used container tool that developers and operation teams use to create and automate deploying applications in lightweight containers on VMs. This is done to ensure that applications work efficiently in multiple environments.

Note: Containers are a software package that works with all the dependencies required to run an application.

Now, let's consider a case where you want to use two containers in a single service? What will you do? In order to make it more clear, let's understand this question with an example. 

Suppose you have more than one container in Docker. Let’s say a web server and a database are running in separate containers, which is typical for your data structure. The building, running, and connecting the containers from separate Docker files is difficult and can take a lot of time. This is where Docker Compose comes in. It facilitates multiple services running simultaneously.

Improve Your Earning Potential Further!

DevOps Engineer Masters ProgramExplore Program
Improve Your Earning Potential Further!

What is Docker Compose?

Docker Compose is a tool that assists in defining and sharing multi-container applications. By using Compose, we can define the services in a YAML file, as well as spin them up and tear them down with one single command.

To understand Docker Compose, let’s look at Myntra as an example. Myntra is a fashion e-commerce website similar to Amazon. You visit the Myntra website through your web browser and go through several activities, like logging in to your account, browsing a catalog, checking out, and so on. Behind each of these activities or services are different products, such as an account database, product database, cart database, and others that run behind the scenes.

myntra

Each of these can be considered a microservice. The more microservices you build into your environment, the more valuable it is to have each of these services in their containers. But as a developer, you should be able to jump from one container to another. This is where you can relate this example to Docker, where Docker Compose can connect different containers as a single service.

Docker Compose is used for running multiple containers as a single service. Each of the containers here run in isolation but can interact with each other when required. Docker Compose files are very easy to write in a scripting language called YAML, which is an XML-based language that stands for Yet Another Markup Language. Another great thing about Docker Compose is that users can activate all the services (containers) using a single command.

For example:

If you have an application that requires an NGINX server and Redis database, you can create a Docker Compose file that can run both the containers as a service without the need to start each one separately.

docker-yaml

Benefits of Docker Compose

  • Single host deployment - This means you can run everything on a single piece of hardware
  • Quick and easy configuration - Due to YAML scripts
  • High productivity - Docker Compose reduces the time it takes to perform tasks
  • Security - All the containers are isolated from each other, reducing the threat landscape

Now, you might be thinking that Docker Compose is quite similar to Docker Swarm, but that’s not the case. Here are some of the differences between Docker Compose and Docker Swarm:

docker

Earn the Most Coveted DevOps Certification!

DevOps Engineer Masters ProgramExplore Program
Earn the Most Coveted DevOps Certification!

Basic Commands in Docker Compose

  • Start all services: Docker Compose up
  • Stop all services: Docker Compose down
  • Install Docker Compose using pip: pip install -U Docker-compose
  • Check the version of Docker Compose: Docker-compose-v
  • Run Docker Compose file: Docker-compose up -d
  • List the entire process: Docker ps
  • Scale a service - Docker Compose up -d -scale
  • Use YAML files to configure application services - Docker Compose.yml

These are some of the basic commands that you can use in Docker Compose. If you want to watch a demo on how you can use each of these commands in Docker Compose, click here to watch Simplilearn’s video tutorial for beginners. 

Install Docker Compose

Let us take a deep dive into how to install Docker Compose on Windows, Linux, and macOS, respectively. Before getting into the installation, we have some prerequisites; let us see and define these prerequisites.

Prerequisites

We need to have Docker Engine installed remote or locally in order to be able to install Docker Compose. 

Let us take a look at the required setup for Docker Engine -

  • Docker Compose comes pre-installed on desktop platforms like Docker Desktop for Windows and Mac.
  • Install the Docker Engine for your operating system as indicated on the Get Docker page on Linux systems, then return here for Docker Compose installation instructions on Linux systems.

Installation

Below, we summarize the steps on macOS, Linux, and Windows for installing Docker Compose.

Install Docker Compose on macOS 

For using Docker Compose on macOS, we need to just have Docker Desktop for Mac installed and don’t need to install Docker Compose separately.

Install Docker Compose on Linux

To run Docker Compose on Linux, we need to download the Docker Compose binary using Github’s Compose repository release page

Follow the below instructions to successfully install the Docker Compose -

  • To download the current stable version of the Docker Compose, run the below command -

sudo curl -L "https://github.com/docker/compose/releases/download/<version>/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

  • To the binary, apply for the executable permissions.

sudo chmod +x /usr/local/bin/docker-compose

  • Next, go for installation testing by running the below command.

docker-compose --version

Install Docker Compose on Windows

For using Docker Compose on Windows, we need to just have Docker Desktop for Windows installed and don’t need to install Docker Compose separately.

PRINCE2® Certification Exam Made Easy to Crack

PRINCE2® Foundation & Practitioner CertificationExplore Course
PRINCE2® Certification Exam Made Easy to Crack

Create the Compose file

The next step is to create the Compose file for Docker Compose. 

Follow the below steps to create this file -

  • Create a file called docker-compose.yml at the root of the project.
  • We then proceed by defining and mentioning the version of the schema -

 version: "<version number>"

  •  To run as part of our application, we define the list of containers or services -

 version: "<version number>"

 services:

We then proceed to migrate a service one at a time to our already created Compose file.

Define Various Services

To define the app container, we use the following command -

 docker run -dp 3000:3000 \

  -w /app -v "$(pwd):/app" \

  --network todo-app \

  -e MYSQL_HOST=<host_name>\

  -e MYSQL_USER=root \

  -e MYSQL_PASSWORD=secret \

  -e MYSQL_DB=<db_name> \

  node:12-alpine \

  sh -c "yarn install && yarn run dev"

In case we are using PowerShell, we may use the below command -

PS> docker run -dp 3000:3000 `

  -w /app -v "$(pwd):/app" `

  --network todo-app `

  -e MYSQL_HOST=<host_name> `

  -e MYSQL_USER=root `

  -e MYSQL_PASSWORD=secret `

  -e MYSQL_DB=<db_name> \

  node:12-alpine `

  sh -c "yarn install && yarn run dev"

Earn 40% More Than Non-Certified Peers

Lean Six Sigma Expert Masters ProgramEnroll Now!
Earn 40% More Than Non-Certified Peers

Now, follow the below steps to successfully define the various services in Docker Compose -

1. We need to define the image and service entry for the container. We need to pick a custom name for our service, which will become our network alias and will be useful in defining the MySQL service. 

 version: "4.2"

 services:

   app:

     image: node:12-alpine

2. Close to the image definition, one can see the command. Now, we move this into our file. 

Note: There is no need for ordering.

 version: "4.2"

 services:

   app:

     image: node:12-alpine

     command: sh -c "yarn install && yarn run dev"

3. We define the ports for the service to migrate the -p 3000:3000 part of the command. For the same reason, we will be using a short syntax here.

 version: "4.2"

 services:

   app:

     image: node:12-alpine

     command: sh -c "yarn install && yarn run dev"

     ports:

       - 3000:3000

4. Now, we migrate the volume mapping (-v "$(pwd):/app") and a working directory (-w /app) by using the definitions of volumes and working_dir. The volumes also have both short and long syntax.

Note: The advantage of the volume definitions in Docker Compose is that from the current directory, we can use relative paths. 

 version: "4.2"

 services:

   app:

     image: node:12-alpine

     command: sh -c "yarn install && yarn run dev"

     ports:

       - 3000:3000

     working_dir: /app

     volumes:

       - ./:/app

5. Using the environment key, we need to migrate the environment variable definitions.

 version: "4.2"

 services:

   app:

     image: node:12-alpine

     command: sh -c "yarn install && yarn run dev"

     ports:

       - 3000:3000

     working_dir: /app

     volumes:

       - ./:/app

     environment:

       MYSQL_HOST: mysql

       MYSQL_USER: root

       MYSQL_PASSWORD: secret

       MYSQL_DB: <db_name>

Earn the Most Coveted DevOps Certification!

DevOps Engineer Masters ProgramExplore Program
Earn the Most Coveted DevOps Certification!

Run the Application Stack

Now, we need to startup the compose file that we had created.

  • First, make sure there are no other copies of the app running (docker rm -f <ids> and docker ps).
  • Using the docker-compose up command, we need to start the application stack. To run everything in the background, we need to use the -d flag.

 docker-compose up -d

Output:

 Creating network "app_default" with the default driver

 Creating volume "<app_name>-mysql-data" with default driver

 Creating app_app_1   ... done

 Creating app_mysql_1 ... done

  • By implementing the docker-compose logs -f command, we need to examine the logs. Each of the services' logs will be combined into a single stream. If we need to keep an eye on timing concerns, this is really handy. The -f flag "follows" the log, so you'll get real-time results as they're generated.

The output will look similar to the one below -

mysql_1  | 2019-10-03T03:07:16.083639Z 0 [Note] mysqld: ready for connections.

mysql_1  | Version: '5.7.27'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server (GPL)

 app_1    | Connected to mysql db at host mysql

 app_1    | Listening on port 3000

Note: To aid recognize messages, the service name is printed at the start of the line (typically colored). The service name can be included at the end of the log commands to view the logs for a specific service (for instance, docker-compose logs -f app).

  • We must be able to access our app and see it running at this point. We are now left with only one command to go.

App Stack in Docker Dashboard

On the Docker Dashboard, we find a group called app. This is the Docker Compose "project name" that is used to organize the containers together. The project name is the same as the name of the directory where docker-compose.yml is found by default.

Docker_Dashboard_1.

We see the two containers defined in the compose file if we twirl down the app. Because they follow the pattern of project-name> service-name> replica-number>, with more descriptive names. As a result, identifying the MySQL database container and our app container is a breeze.

Docker_Dashboard_2

Learn More About Docker

If you want to master all things Docker, check out Simplilearn’s Docker In-Depth Certification Training Course today. In this comprehensive course, you will get access to four hours of self-paced learning, four end-of-chapter quizzes, and the opportunity to work on hands-on projects. 

About the Author

Sana AfreenSana Afreen

Sana Afreen is a Senior Research Analyst at Simplilearn and works on several latest technologies. She holds a degree in B. Tech Computer Science. She has also achieved certification in Advanced SEO. Sana likes to explore new places for their cultures, traditions, and cuisines.

View More
  • Acknowledgement
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, OPM3 and the PMI ATP seal are the registered marks of the Project Management Institute, Inc.