Series on Docker in Practice & Production - Using Docker Compose to Start Multiple Services

Series on Docker in Practice & Production - Using Docker Compose to Start Multiple Services

Daily short news for you
  • For a long time, I have been thinking about how to increase brand presence, as well as users for the blog. After much contemplation, it seems the only way is to share on social media or hope they seek it out, until...

    Wearing this shirt means no more worries about traffic jams, the more crowded it gets, the more fun it is because hundreds of eyes are watching 🤓

    (It really works, you know 🤭)

    » Read more
  • A cycle of developing many projects is quite interesting. Summarized in 3 steps: See something complex -> Simplify it -> Add features until it becomes complex again... -> Back to a new loop.

    Why is that? Let me give you 2 examples to illustrate.

    Markdown was created with the aim of producing a plain text format that is "easy to write, easy to read, and easy to convert into something like HTML." At that time, no one had the patience to sit and write while also adding formatting for how the text displayed on the web. Yet now, people are "stuffing" or creating variations based on markdown to add so many new formats that… they can’t even remember all the syntax.

    React is also an example. Since the time of PHP, there has been a desire to create something that clearly separates the user interface from the core logic processing of applications into two distinct parts for better readability and writing. The result is that UI/UX libraries have developed very robustly, providing excellent user interaction, while the application logic resides on a separate server. The duo of Front-end and Back-end emerged from this, with the indispensable REST API waiter. Yet now, React doesn’t look much different from PHP, leading to Vue, Svelte... all converging back to a single point.

    However, the loop is not bad; on the contrary, this loop is more about evolution than "regression." Sometimes, it creates something good from something old, and people rely on that goodness to continue the loop. In other words, it’s about distilling the essence little by little 😁

    » Read more
  • Alongside the official projects, I occasionally see "side" projects aimed at optimizing or improving the language in some aspects. For example, nature-lang/nature is a project focused on enhancing Go, introducing some changes to make using Go more user-friendly.

    Looking back, it resembles JavaScript quite a bit 😆

    » Read more

Problem

docker run only allows starting one container at a time, which can be challenging and complex when your application needs to start multiple containers simultaneously. Additionally, manually executing individual commands can be error-prone. Imagine having a stack with multiple images - should you run docker run n times?

Docker Compose was created to address this problem. It stores a set of instructions on how to run multiple containers in a .yml/.yaml file (docker-compose.yml). With just one command, you can start all the containers together.

Docker Compose

Docker Compose used to be a plugin, but it has recently been integrated into Docker for Windows and macOS. However, it is still not available on Linux. If you are using Linux, you need to install Docker Compose separately.

Refer to this link for various ways to install Docker Compose.

For Linux users, simply use the following commands:

$ sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
$ sudo chmod +x /usr/local/bin/docker-compose
$ sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

# Check the version
$ docker-compose --version
docker-compose version 1.29.2, build 1110ad01

To get started, let's say you need to start 2 services: a Node.js API and a MySQL server. Create a docker-compose.yml file:

version: "3.9"
services:  
  node:  
    image: my-node-app
    environment:  
      - MYSQL_HOST: mysql
      - MYSQL_USER: hoaitx
      - MYSQL_PASSWORD: password
    ports:  
      - "3000:3000"
    networks:  
      - my_app
    depends_on:  
      - mysql

  mysql:  
    image: mysql
    environment:  
      - MYSQL_USER=hoaitx
      - MYSQL_PASSWORD=password
    networks:  
      - my_app
    volumes:  
      - ./data:/var/lib/mysql

networks:  
  my_app:  

Then use the docker-compose command to start them.

$ docker-compose up -d

To see the list of running services:

$ docker-compose ps

Note: the above docker-compose commands should be executed in the directory containing the docker-compose.yml file.

Similar to Dockerfile, a YAML file for Docker Compose also consists of a set of commands. Docker Compose continuously adds new commands in subsequent Docker versions, so make sure to check the version in the YAML file with your Docker version before using them.

Components

A YAML file typically contains version and services. The version specifies which version of the YAML file you are using, while services list the services that will be run. It also includes networks that contain the networks used in the file.

Here is an example of a YAML file version 3.9 that runs Node.js and MySQL concurrently:

version: "3.9"
services:  
  node:  
    image: my-node-app
    environment:  
      - MYSQL_HOST: mysql
      - MYSQL_USER: hoaitx
      - MYSQL_PASSWORD: password
    ports:  
      - "3000:3000"
    networks:  
      - my_app
    depends_on:  
      - mysql

  mysql:  
    image: mysql
    environment:  
      - MYSQL_USER=hoaitx
      - MYSQL_PASSWORD=password
    networks:  
      - my_app
    volumes:  
      - ./data:/var/lib/mysql

networks:  
  my_app:  

Tips: YAML files use spaces for indentation, so be careful when building this file. A misplaced line can cause docker-compose to fail.

  • version specifies the version of the YAML file.
  • services contains the services to be run.
  • networks contains network configurations.

For each service, we will have a name, which is not only the service name but also the domain used for services to call each other. In the example above, Node.js calls MySQL using the name "mysql" (MYSQL_HOST: mysql).

  • image specifies the image to be used in the container.
  • environment sets the environment variables.
  • ports maps the ports from inside the container to the host machine.
  • volumes mounts a directory from the host machine to the Docker container.
  • networks declares the networks that the container can join.
  • depends_on sets the dependencies between services, meaning a service will only be initialized once the other service is running.

Apart from the above commands, Docker Compose also supports many other commands. It is not possible to list them all here, so you can find the documentation here. Here are a couple of notable commands:

  • build replaces image. Typically, you need a specific image to start a container (service) using the docker build command. With build, you no longer need to build an image separately, as Docker Compose will build it when starting. See more details here.
  • deploy is a useful command that is only applicable when using Docker Swarm. Generally, deploy is where you gather the instructions on how your services should be run. For example, you can configure the number of replicas, specify running on specific nodes, automatically restart the service if it fails, etc.
  • There are many more commands that you can explore here.

Compose CLI

Knowing the docker-compose commands will help you easily deploy your applications. Below are some basic commands:

To view the help guide:

$ docker-compose --help

Check the Docker Compose version:

$ docker-compose --version
docker-compose version 1.29.2, build 1110ad01

To deploy, navigate to the directory containing the .yml file:

$ docker-compose up -d

To update changes from the .yml file, rerun the deploy command, which will check for changes and update accordingly.

View the list of services:

$ docker-compose ps

Check the logs of the services:

$ docker-compose logs

To clean up:

$ docker-compose down

Conclusion

Docker Compose allows us to run multiple services (containers) simultaneously. It stores the instructions in a .yml/.yaml file commonly named docker-compose.yml.

Docker Compose is suitable for small projects that do not require scaling across nodes. To scale, Docker provides a feature called swarm. I will discuss Docker swarm in more detail in the next article.

Premium
Hello

The secret stack of Blog

As a developer, are you curious about the technology secrets or the technical debts of this blog? All secrets will be revealed in the article below. What are you waiting for, click now!

As a developer, are you curious about the technology secrets or the technical debts of this blog? All secrets will be revealed in the article below. What are you waiting for, click now!

View all

Subscribe to receive new article notifications

or
* The summary newsletter is sent every 1-2 weeks, cancel anytime.

Comments (0)

Leave a comment...