Work with multiple services using Docker Compose

Work with multiple services using Docker Compose

Setting up multiple containers using docker run command can be a tedious job. And it become more tough when you are in a dev environment and you have to build the image each time with source code changes.

đź’­ Introduction to Docker Compose

Docker Compose is a tool that allows you to define and run multi-container Docker applications. It uses a YAML file to define the services, networks, and volumes required for your application, making it easy to manage complex, multi-container applications.

Docker Compose makes it easy to start and stop your multi-container application using a single command. It also allows you to scale your application by increasing or decreasing the number of instances of a particular service.

Docker Compose is particularly useful for development and testing environments, as it allows developers to easily manage and test complex, multi-container applications on their local machines. It can also be used in production environments to manage complex, multi-container applications across different hosts and environments.

Overall, Docker Compose is a powerful tool that simplifies the management of multi-container Docker applications, making it easier to build, test, and deploy complex applications.

What is Docker Compose used for?

With Docker Compose, you can define all the services required for your application in a single file, making it easy to manage and deploy your application across different environments. Docker Compose allows you to define multiple containers, each with their own configuration options such as environment variables, ports, volumes, and network connections.

How to install Docker Compose

Docker desktop already have docker compose but for linux you can use https://docs.docker.com/compose/install/

Crafting Your Docker Compose Adventure

So, you’ve decided to embark on a Docker Compose journey, and we’re here to make the ride smooth and enjoyable! Let’s dive into creating a Docker Compose file together.

    version: '3'
    
    services:
      db:
        image: mysql:latest
        restart: always
        environment:
          MYSQL_ROOT_PASSWORD: example
        networks:
          - backend
      server:
        build:
          context: .
          dockerfile: Dockerfile.server
        ports:
          - "80:80"
        depends_on:
          - db
        environment:
          DB_HOST: db
          DB_USERNAME: root
          DB_PASSWORD: example
        networks:
          - backend
    
    networks:
      backend:
    

Let’s Break It Down

In this adventure, we’re creating two service companions: db and server.

The DB Service:

  • Our trusty db service conjures a MySQL container using the latest image.
  • It’s designed to restart automatically, ensuring it never misses a beat.
  • We’ve set the MYSQL_ROOT_PASSWORD to “example” for security.
  • This service joins the “backend” network, our behind-the-scenes bridge.

The Server Service:

  • Meet our dynamic server service! Every time you run this Docker Compose tale, it builds a container from the specified context and Dockerfile (Dockerfile.server).
  • The container’s port 80 is mapped to the host’s port 80, making sure it’s easily accessible.
  • The depends_on spell ensures that the db service is up and ready before our server takes the stage.
  • Environment variables like DB_HOST, DB_USERNAME, and DB_PASSWORD are set for smooth communication with the db.
  • Just like the db, our server dances on the “backend” network.

Network Magic:

  • The “backend” network connects our services. It’s like the secret passage allowing the server to chat with the db.
  • When the server wants to talk to the db, it says “db.backend.” Magic, right?

This is our Dockerfile.server

    FROM python:3.10-alpine
    
    WORKDIR /app
    
    COPY requirements.txt .
    
    RUN pip install --no-cache-dir -r requirements.txt
    
    COPY . .
    
    CMD ["python", "server.py"]
    

This file conjures up a Python environment in the server container and runs our server.py script.

Running a Docker Compose file

To unleash this spectacle, run:

    docker-compose up
    

But hey, if you just want to prepare the stage (build the image), use:

    docker-compose build server
    

Most used Docker Compose commands and options

Here are some of the most commonly used Docker Compose commands and options:

Commands:

  1. docker-compose up:
    • Usage: docker-compose up [options] [SERVICE...]
    • Description: Builds, (re)creates, starts, and attaches to containers for a service.
  2. docker-compose down:
    • Usage: docker-compose down [options]
    • Description: Stops and removes containers, networks, volumes, and images created by up.
  3. docker-compose ps:
    • Usage: docker-compose ps [options] [SERVICE...]
    • Description: Lists containers.
  4. docker-compose logs:
    • Usage: docker-compose logs [options] [SERVICE...]
    • Description: View output from containers.
  5. docker-compose exec:
    • Usage: docker-compose exec [options] SERVICE COMMAND [ARGS...]
    • Description: Run a command in a running service container.
  6. docker-compose build:
    • Usage: docker-compose build [options] [SERVICE...]
    • Description: Builds or rebuilds services.
  7. docker-compose pull:
    • Usage: docker-compose pull [options] [SERVICE...]
    • Description: Pulls images for services.
  8. docker-compose restart:
    • Usage: docker-compose restart [options] [SERVICE...]
    • Description: Restarts services.

Options:

  1. -f, --file FILE:
    • Description: Specify an alternate compose file. Default is docker-compose.yml.
  2. -p, --project-name NAME:
    • Description: Specify an alternate project name.
  3. -d, --detach:
    • Description: Run containers in the background.
  4. -v, --volumes:
    • Description: Mount host volumes into services.
  5. --build:
    • Description: Build images before starting containers.
  6. --force-recreate:
    • Description: Recreate containers even if their configuration and image haven’t changed.
  7. --no-deps:
    • Description: Don’t start linked services.
  8. -e, --env:
    • Description: Set environment variables.
  9. --scale SERVICE=NUM:
    • Description: Set the number of containers to run for a service.
  10. --remove-orphans:
    • Description: Remove containers for services not defined in the Compose file.
  11. --force-recreate:
    • Description: Recreate containers even if their configuration and image haven’t changed.
  12. -h, --help:
    • Description: Display help.

These commands and options provide a solid foundation for working with Docker Compose, allowing you to manage, scale, and control your multi-container applications efficiently. Always refer to the official documentation for the most up-to-date information and additional options.

Conclusion

Our journey merely scratched the surface of Docker Compose’s magic. From orchestrating multi-container symphonies to ensuring seamless communication, Docker Compose proves a valuable ally in the realm of containerization.

As you continue your Docker Compose adventure, explore its nuances, experiment with different services, and craft your own orchestration symphony. May your containers be ever resilient, your networks ever connected, and your deployments ever enchanting.