Spring Sale - up to 36% OFF

How to Use Docker Compose for Multi-Container Applications

How to Use Docker Compose for Multi-Container Applications
Published on Apr 9, 2025 Updated on Apr 9, 2025

Modern-day applications require more than just one container to implement the microservices architecture. Using different containers to deploy application services is good because it makes it easy to modify and scale applications.

Docker is good at facilitating microservices as it offers various container solutions such as Docker Desktop, Docker Swarm, and mostly Docker Compose. After creating containers using Docker Desktop you can use Docker Compose to configure and deploy multi-container applications at the same time. In this article, you will learn what Docker Compose is and how to use it to manage multi-container applications.

#Prerequisites

You need to have basic knowledge of how Docker works and make sure you have installed Docker Desktop before starting this tutorial.

Ready to supercharge your Docker infrastructure? Scale effortlessly and enjoy flexible storage with Cherry Servers bare metal or virtual servers. Eliminate infrastructure headaches with free 24/7 technical support, pay-as-you-go pricing, and global availability.

#What is Docker Compose?

Manually managing multiple containers is time-consuming and error-prone. It is easy to make a misconfiguration error when trying to write multiple container configuration files at the same time. There is a lot that needs to be done to maintain multiple containers running simultaneously. Imagine being able to start and configure multiple containers as effortlessly as starting a single one. How much simpler and more convenient would that be?

That is what Docker Compose does: it eliminates all the issues and challenges that come with the process of managing many containers with multiple and different configuration files. Docker compose is a task-efficient tool that configures and administers multiple containers and services at the same time.

Docker Compose uses a single configuration file called docker-compose.yml. This file acts as a single entry configuration definition point for multiple containers that are managed by Docker Compose. Using this file enables Docker Compose to create network components and manage storage volumes for the specified containers. This automatic process makes it easy and simple to manage concurrent running applications.

Docker Compose is not an alternative or a competitor to Docker Desktop; rather, it is an expansion of solutions offered by Docker. Before Docker Compose version 2 was released, Docker compose was downloaded as a standalone binary. Currently, Docker Compose comes preinstalled with Docker Desktop.

Nowadays, applications are usually connected to multiple services, such as databases, user authentication, and payment processing. All of these services have to be deployed and hosted in different containers. Docker Compose makes it possible to configure and specify details for different containers such as:

  • Storage volumes
  • Port bindings
  • Environment variables

#How to check if Docker Compose is installed

Docker Compose does not need an installation process if you have already installed Docker Desktop on your Linux machine. Every feature that you want to use is bundled in Docker desktop and ready to be used. To confirm if Docker Compose is available on your machine, use the following command:

 docker compose version

#How to use Docker Compose for multi-container applications

The process of managing multiple containers using Docker Compose starts with creating a docker-compose.yml file. The docker-compose.yml file is stored in your application’s project root structure. Since it is stored in the application’s project root structure, it will be able to fetch data from the Dockerfile about the image you are building and use it whenever needed.

Below is an example of a docker-compose.yml file that creates a container using the image built by the Dockerfile for the web service. The second service creates and facilitates a Postgres database.

version: '3.8'
services:
  web:
    build: .
    ports:
      - "8000:5000"

  db:
    image: postgres:13
    environment:
      POSTGRES_USER: user
      POSTGRES_PASSWORD: password
      POSTGRES_DB: mydatabase
    volumes:
      - db_data:/var/lib/postgresql/data
    networks:
      - app-network

networks:
  app-network:

volumes:
  db_data:

Below is an explanation of the components found in a docker-compose.yml configuration file:

1. Version: The version key specifies the format used by the docker-compose.yml. The format version determines the features, syntax, and behavior used in the Compose file. Different versions introduce new capabilities and changes in how services, networks, and volumes are defined and interact.

2. Service components: To be able to manage different containers at the same time while making sure that every service meets its requirements and goals, the docker-compose file requires the following specifications:

  • Image: Each service will use its own image. This image will be used to build the container for the specified service. A service can also use a container built in Dockerfile by adding the build: . key-pair value. The period tells Docker Compose where Dockerfile is located, which is the root directory.
  • environment: Sets environment variables required by the service container, such as the root password, database name, and user credentials.
  • ports: This flag enables you to add port numbers that enable your service to communicate with other components, inside and outside networks.
  • Volumes: Mounts the storage directory on the host to a directory inside the container so that the service serves the content from this directory.
  • Networks: Enables the service to connect to a network and communicate with other services connected to the same network.

#How to start multi-applications using Docker Compose

After creating the docker-compose.yml, go ahead and start the containers using the following command:

docker compose up

The above command will do the following:

  • Pull images
  • Create containers
  • Set up networks and volumes
  • Start services

You will get the following output that shows that the services we created using the previous command are now running:

Compose up command output

#Managing application services using Docker Compose

After starting the services, you need to do the following tasks to manage running services.

**1. Viewing logs: **Use the following command to get the root cause of an issue that is causing problems when the service is running. The command will give a comprehensive list of all services.

docker compose logs

You can also view the logs of a specific service by appending the service name:

docker compose logs web

The image below shows logs for the database service:

db service logs

The image below shows logs for the web service:

Web service logs

#Scaling services

When network traffic increases and decreases the number of service instances has to change. The --scale flag allows you to increase the number of instances or replicas a service can have. For example, the command below increases the web service instance by 3 replicas. Increasing the number of service instances helps the service to handle more workload or traffic.

docker compose up --scale web=3 -d

The output below shows that the web-3 service has been scaled by 3 replicas.

Scaling services

#Restarting and stopping services

Use the following command to restart a service. You might use docker compose restart if you need to apply changes to a container that doesn’t require rebuilding the image or if you want to clear any temporary issues without completely stopping and starting the containers.

docker compose restart

If a service is causing serious issues or is no longer needed, use the following command to stop the service. This command is useful when you want to completely shut down the application and clean up the resources.

docker compose down

#Conclusion

In this article, you have learned what Docker Compose is in detail and how to use Docker Compose basic commands that manage multiple running services. Docker Compose does a good job of providing many services with sufficient resources whenever needed. One of the most crucial aspects of running multiple services or containers concurrently is network configuration.

A network is essential for ensuring that services are able to share resources and data. That is why it is important to ensure that the network being used by services controlled by Docker Compose is secure and stable. Implementing networking solutions provided by Cherry Servers can boost your network processes. Cherry Servers provides:

All of the above networking solutions are important for Docker Compose to deploy and host services.

Cloud VPS Hosting

Starting at just $3.24 / month, get virtual servers with top-tier performance.

Share this article

Related Articles

Published on Aug 8, 2024 Updated on Oct 25, 2024

How to Push Docker Image to Docker Hub

Learn how to create a Docker hub repository and push an image to the Docker hub using the `docker push command` in this detailed tutorial.

Read More
Published on Sep 4, 2024 Updated on Nov 7, 2024

How to List Docker Images: A Complete Guide to Managing All Images

Learn how to list Docker images, analyze and filter them efficiently. This complete guide covers commands, flags, and tips for managing all your Docker images effectively.

Read More
Published on Dec 19, 2023 Updated on Jan 24, 2025

Docker Commands Cheat Sheet [With Examples]

This Docker commands cheat sheet will cover all the basic Docker commands you need to know to streamline your development workflow.

Read More
We use cookies to ensure seamless user experience for our website. Required cookies - technical, functional and analytical - are set automatically. Please accept the use of targeted cookies to ensure the best marketing experience for your user journey. You may revoke your consent at any time through our Cookie Policy.
build: 0a3f82ea2.1089