Managing Services with Docker Compose

Tutorial 3 of 5

1. Introduction

1.1 Tutorial's Goal

The purpose of this tutorial is to help you understand how to manage services using Docker Compose. We'll explore how to scale, update, and monitor these services in a practical and easy-to-understand manner.

1.2 Learning Outcomes

By the end of this tutorial, you will be able to:

  • Understand what Docker Compose is and how it manages services.
  • Scale your services using Docker Compose.
  • Update and monitor your services using Docker Compose.

1.3 Prerequisites

This tutorial assumes that you have:

  • Basic understanding of Docker.
  • Docker and Docker Compose installed on your computer.
  • Basic familiarity with YAML syntax.

2. Step-by-Step Guide

2.1 Docker Compose

Docker Compose is a tool for defining and managing multi-container Docker applications. It uses a YAML file to configure the application's services, networks, and volumes.

In this tutorial, we will use a simple web app and a database as our services. Our application will be defined in a docker-compose.yml file.

2.2 Scaling Services

To scale services with Docker Compose, you'll use the scale command followed by the service name and the number of containers you want to run.

docker-compose up --scale service_name=3

2.3 Updating Services

Updating a service in Docker Compose involves changing the service's configuration in the docker-compose.yml file and then running the up command with the --no-deps option to avoid affecting linked services.

docker-compose up -d --no-deps --build service_name

2.4 Monitoring Services

You can monitor the logs of your services by using the logs command followed by the service name.

docker-compose logs service_name

3. Code Examples

Let's look at an example where we have a simple web application and a database as our services.

version: '3'
services:
  web:
    build: .
    ports:
      - "5000:5000"
  db:
    image: postgres
    ports:
      - "5432:5432"

In this docker-compose.yml file, we have defined two services: web and db. The web service is built from the current directory and mapped to port 5000, while the db service uses the postgres image and is mapped to port 5432.

To scale the web service to 3 instances, we run:

docker-compose up --scale web=3

To update the web service, we make the necessary changes in the docker-compose.yml file and then run:

docker-compose up -d --no-deps --build web

To monitor the logs of the db service, we run:

docker-compose logs db

4. Summary

In this tutorial, we have learned how to manage services using Docker Compose. We covered how to scale, update, and monitor services. The next step is to practice these concepts to get a better understanding.

5. Practice Exercises

  1. Create a Docker Compose file with three services: a web server, a database, and a worker. Scale the worker service to 3 instances.

  2. Update the database service in your Docker Compose file without affecting the other services.

  3. Monitor the logs of the web server service.

Solutions

  1. Create a docker-compose.yml file with the three services and scale the worker service.
version: '3'
services:
  web:
    build: .
    ports:
      - "5000:5000"
  db:
    image: postgres
    ports:
      - "5432:5432"
  worker:
    build: .
    ports:
      - "6000:6000"

Then, run the command:

docker-compose up --scale worker=3
  1. To update the db service, make the necessary changes in the docker-compose.yml file and then run:
docker-compose up -d --no-deps --build db
  1. To monitor the logs of the web service, run:
docker-compose logs web