DEV Community

Cover image for Getting Started with Docker Compose: A Hands-On Guide to Multi-Container Applications
Theodora
Theodora

Posted on

Getting Started with Docker Compose: A Hands-On Guide to Multi-Container Applications

If you’ve been working with Docker, you’ve probably been dealing with single-container applications. However, when your applications become more complex, you may need to run multiple services, such as databases, message queues, or caches. The question is: Do you install everything in a single container, or do you run multiple containers? And if you run multiple containers, how do you connect them together?

In this blog post, we’ll explore how Docker Compose makes managing multi-container applications easy. We’ll work with a simple to-do list app built with Node.js and MySQL to demonstrate how Docker Compose can manage your containers, network, and persistent data, all in a single YAML file.

Table of Contents

Why Use Docker Compose?

One of the key best practices for containers is that each container should do one thing and do it well. When your project grows, running everything inside a single container can quickly become a mess. Imagine running a database, an application server, and a message queue all in one container,it’s not scalable, maintainable, or efficient.

Docker Compose solves this problem by allowing you to define multiple containers and their configurations in a single file, called a docker-compose.yml. This file allows you to:

  • Define all services in one place.
  • Automate network setup for your containers.
  • Persist data across container restarts using volumes.
  • Manage environments with environment variables.
  • Rebuild containers when changes are made using a simple command.

Docker Compose is declarative. You define what you need, and when you run docker-compose up, Docker Compose ensures your environment is built correctly.

Prerequisites

Before you begin, make sure you have the following installed:

  • Docker Desktop: Install it from the official Docker website for your operating system.
  • Git: To clone the application repository.

Lab 1: Setting Up the Application

Let’s begin by setting up the sample application.

1. Clone the Sample Application

First, open your terminal and clone the application repository:

2. Explore the Compose File

Inside the todo-list-app directory, you’ll find a file named docker-compose.yml (also called the Compose file). This YAML file defines all the services that make up your application, their configurations, and how they connect.

3. Overview of the Docker Compose File

The docker-compose.yml file includes the following key sections:

  • services: Defines the individual containers (e.g., app, mysql).
  • networks: Specifies how services communicate.
  • volumes: Persists data, especially for databases, even when containers are stopped.
  • environment variables: Provides configuration data to containers.

Lab 2: Starting the Application

Now, let’s start the application with Docker Compose.

1. Run the Application Using Docker Compose

Run the following command to start the application in detached mode (-d) and build the services:

What happened?

  • Docker pulled the Node.js and MySQL images from Docker Hub.
  • A new network was created for the application to allow communication between the containers.
  • A volume was created to persist MySQL data.
  • Two containers, todo-list-app-app-1 (Node.js app) and todo-list-app-mysql-1 (MySQL), were started.

Lab 3: Accessing the Application

Once everything is up and running, you can access your to-do list app in your browser.

1. Access the Frontend

In your browser, visit:

or on docker desktop, click

2. View the Containers in Docker Desktop

You can also use Docker Desktop to monitor your containers and their configurations. Open Docker Desktop and navigate to the Containers tab to see the todo-list-app-app-1 and todo-list-app-mysql-1 containers.

Tearing Down the Application

When you're done with the application, it’s easy to tear everything down using Docker Compose.

1. Stop the Containers

To stop all running containers and remove them, run:

Expected Output:

2. Remove Volumes (Optional)

By default, volumes are not removed when tearing down the application to allow you to retain your data. If you want to remove the volumes as well, run:

Expected Output:

Conclusion

In this hands-on guide, you learned how to use Docker Compose to define, build, and run a multi-container application. We explored how to:

  • Set up and configure services using docker-compose.yml.
  • Start the app and view it in your browser.
  • Easily tear down the app when done.

Docker Compose makes it easy to work with multiple containers, saving time and effort when managing complex applications. This workflow is essential for modern development, and now you’re equipped to run your own multi-container Docker applications.

Top comments (0)