Mastering Docker Compose

Learn to create and manage multi-container Docker applications, explore key differences from Docker’s detached mode, and set up a MySQL database service using environment variables, services, and volumes.

Since 2013, Docker has been a game-changer for millions of developers, providing the ability to create independent and isolated environments for launching and deploying applications. If you’re new to Docker, you might want to start with our “What is Docker?” tutorial to grasp the basics.

Now, imagine managing a multi-container application with a single command—that’s the power of Docker Compose. Welcome to a world where setting up, running, and scaling complex applications is not just easier but more efficient.

In this tutorial, we’ll explore Docker Compose in depth. You’ll learn how it simplifies the process of defining, configuring, and running multi-container Docker applications. We’ll show you how it extends beyond running individual containers, even in detached mode, to offer a more robust solution for deploying and managing your applications.

Ready to revolutionize the way you work with containerized applications? Let’s dive into mastering Docker Compose!

Setting Up Docker Compose Locally

Whether you’re using Linux, Mac, or Windows, setting up Docker Compose locally is straightforward. It involves installing Docker Desktop on your system, similar to the Docker setup process. For detailed instructions, check out our guide on Setting up Docker.

Differences Between Docker and Docker Compose

Docker and Docker Compose are both integral tools within the Docker ecosystem, but they serve distinct purposes and operate at different levels of abstraction.

Docker is a platform that enables the development, shipping, and running of applications in containers. It allows you to run applications isolated from each other on a single system. The key components of Docker include:

  • Docker Images: These are templates that contain the application and all dependencies needed to run it.

  • Dockerfile: This is a file created within your project containing a series of instructions and commands. It is used to build a Docker image, specifying what happens when someone creates a container using that image.

  • Docker Containers: Containers are instances of Docker images. They are lightweight, standalone, and executable packages that include everything needed to run software—such as the code, runtime, libraries, and system tools. Each container operates independently, ensuring isolation from other containers.

Docker Compose is a tool specifically designed to manage and run multi-container Docker applications simultaneously. It relies on a YAML file to configure the application’s services, networks, and volumes. Where YAML is a human-readable data format used for configuration files. The key components of Docker Compose include:

  • Docker Compose File: This YAML file, typically named docker-compose.yml, defines the services required to run your application. It includes configurations such as the image to use, environment variables, ports to expose, volumes to mount, and networks to connect to.

  • Multi-Container Management: Docker Compose allows you to define and manage multiple interconnected services, simplifying the orchestration of complex applications that require several containers.

Hands-on with Docker Compose

There are two main commands required to run and stop Docker Compose files:

  • docker-compose up: This command starts and runs the containers defined in your Docker Compose file.
  • docker-compose down: This command stops and removes the containers, networks, and volumes created by docker-compose up.

Before we dive into Docker Compose, let’s clear up a common doubt. However, the key to mastering Docker Compose lies in understanding how to build a Docker Compose file.

Docker Compose vs. Docker Detached Mode

If you’re not familiar with detached mode, it’s an option you can use with the docker run command to run containers in the background, freeing up the terminal for other commands:

docker run --detach (or -d) image_name

While detached mode is useful, Docker Compose is preferred when managing complex, multi-container applications. Docker Compose allows you to define, configure, and run multiple interconnected containers as a single unit using a YAML file, which simplifies management and deployment.

Example:

Instead of running multiple detached containers like this:

docker run -d --name db postgres
docker run -d --name web -p 8000:8000 --link db myapp

You can define a docker-compose.yml file:

version: '3'
services:
db:
image: postgres
web:
image: myapp
ports:
- "8000:8000"
depends_on:
- db

And then simply run:

docker-compose up -d

This launches both services in detached mode, with proper networking and dependency management. It also makes sharing your Docker setup easier, as users with limited Docker knowledge can set up the environment by running a single command.

In Summary:

  • Detached Mode: Useful for running individual containers in the background.
  • Docker Compose: Provides a more robust solution for managing complex, multi-container applications, with benefits like service dependency management, easier scaling, and simplified configuration through a single file.

Creating a Docker Compose File

In this section, we’ll learn how to create a Docker Compose file to set up a MySQL server. We’ll go through the process step by step:

  1. Create a New File: Open your preferred text editor and create a new file named docker-compose.yml in your project directory.

  2. Specify the Version: Start your file by specifying the Docker Compose version. For this project, we’ll use version 3:

    version: '3'
  3. Define the Services: Services are the core components that define your application’s container configuration. Each service typically represents a single container or a set of replicated containers. Under the version, define the MySQL service:

    services:
    db:
    image: mysql:5.7
  4. Set Environment Variables: Environment variables are used to pass configuration settings to containers. For the MySQL server, we’ll set the root password using an environment variable:

    services:
    db:
    image: mysql:5.7
    environment:
    MYSQL_ROOT_PASSWORD: password
  5. Configure Ports: To access MySQL from outside the container, map its port:

    services:
    db:
    image: mysql:5.7
    environment:
    MYSQL_ROOT_PASSWORD: password
    ports:
    - "3306:3306"
  6. Add a Volume for Data Persistence: Volumes in Docker are used to persist data generated by containers. Adding a volume ensures that your data remains even if the container is stopped or removed. Let’s add a volume to our configuration:

    services:
    db:
    image: mysql:5.7
    environment:
    MYSQL_ROOT_PASSWORD: password
    ports:
    - "3306:3306"
    volumes:
    - mysql_data:/var/lib/mysql
    volumes:
    mysql_data:

    Here, mysql_data is the name of our volume, and /var/lib/mysql is the path inside the container where MySQL stores its data.

Your complete docker-compose.yml file should now look like this:

version: '3'
services:
db:
image: mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: password
ports:
- "3306:3306"
volumes:
- mysql_data:/var/lib/mysql
volumes:
mysql_data:

Running the Docker Compose File

Now that we’ve created our Docker Compose file, let’s run it:

  1. Open a terminal and navigate to the directory containing your docker-compose.yml file.

  2. Run the following command to start your MySQL server:

    docker-compose up -d

    The -d flag runs the containers in detached mode, in the background.

  3. To stop the MySQL server and remove the containers, use:

    docker-compose down

Note: This won’t remove the persistent volume we created.

That’s it! You’ve successfully created and run a Docker Compose file for a MySQL server. You can extend this process to more complex setups with multiple services.

Conclusion

Congratulations on completing the Docker Compose tutorial! You’ve just taken a significant step in mastering Docker Compose, a powerful tool for managing multi-container Docker applications. In this tutorial, we’ve explored:

  • The basics of Docker Compose and how it differs from Docker itself.
  • How Docker Compose compares to the default Detached mode.
  • How to create a Docker Compose file for a MySQL database service.
  • The importance of services, environment variables, and volumes in Docker Compose.
  • How to run and manage your Docker Compose setup.

If you found this tutorial useful, be sure to check out more articles on Docker here.

Author

Codecademy Team

'The Codecademy Team, composed of experienced educators and tech experts, is dedicated to making tech skills accessible to all. We empower learners worldwide with expert-reviewed content that develops and enhances the technical skills needed to advance and succeed in their careers.'

Meet the full team