Choose Language

Apply โฑ 60 min

Docker Compose Tutorial

What You Will Learn

  • How to use Docker Compose to manage and run multiple Docker containers
  • How to define and run services and applications that belong together in one environment
  • How to use Docker Compose to simplify the process of starting, stopping, and managing containers

Key Concepts

Docker Compose is a tool that allows you to define and run multiple services and applications that belong together in one environment. It simplifies the process of starting, stopping, and managing containers by providing a structured way to contain common Docker commands. Docker Compose uses a YAML file to define the configuration of the services and containers, making it easy to manage and edit. The docker-compose up command is used to start the services and containers, while the docker-compose down command is used to stop and remove them.

Code Examples

version: '3'
services:
  mongodb:
    image: mongo
    ports:
      - "27017:27017"
    environment:
      - MONGO_INITDB_ROOT_USERNAME=admin
      - MONGO_INITDB_ROOT_PASSWORD=supersecret

This code defines a Docker Compose service for a MongoDB container, mapping port 27017 on the host to port 27017 in the container, and setting environment variables for the root username and password.

version: '3'
services:
  myapp:
    build: .
    ports:
      - "3000:3000"
    environment:
      - MONGO_USERNAME=${MONGO_USERNAME}
      - MONGO_PASSWORD=${MONGO_PASSWORD}

This code defines a Docker Compose service for a custom application, building the image from the current directory, mapping port 3000 on the host to port 3000 in the container, and setting environment variables for the MongoDB username and password using variables.

Lesson Summary

In this lesson, we learned how to use Docker Compose to manage and run multiple Docker containers. We started by understanding what Docker Compose is and how it can simplify the process of starting, stopping, and managing containers. We then learned how to define and run services and applications that belong together in one environment using a YAML file. We also learned how to use the docker-compose up and docker-compose down commands to start and stop the services and containers. Additionally, we learned how to use variables and placeholders in Docker Compose to avoid hardcoding sensitive data.

Practice Exercise

Create a Docker Compose file that defines two services: a MongoDB container and a custom application container. The MongoDB container should map port 27017 on the host to port 27017 in the container, and set environment variables for the root username and password. The custom application container should build the image from the current directory, map port 3000 on the host to port 3000 in the container, and set environment variables for the MongoDB username and password using variables.

What Is Next

In the next lesson, we will learn how to push our custom application image to a private Docker repository and reference it in our Docker Compose file. We will also learn how to use Docker Compose to deploy our application to a production environment.