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
  • Here! A question that I've been wondering about for ages, and until yesterday, everything was finally clarified.

    Normally, people use height: 100vh to set the height equal to the viewport of the screen. On computers, there's no issue, and even simulating the size of a smartphone works fine. But when opened on a phone, height: 100vh always exceeds the viewport. Huh!? What's going on???

    The explanation for this is that mobile devices have a different way of calculating the viewport compared to computers. It is often interfered with or affected by the address bar and navigation bar of the platform you are using. Therefore, if you want 100vh on mobile to truly equal the viewport, you need to take an additional step to reset the viewport.

    It's easy, first, you need to create a CSS variable --vh right in the script tag at the top of the page.

    function updateViewportHeight() { const viewportHeight = globalThis.innerHeight; document.documentElement.style.setProperty('--vh', `${viewportHeight * 0.01}px`); } updateViewportHeight(); window.addEventListener('resize', updateViewportHeight);

    Then instead of using height: 100vh, change it to height: calc(var(--vh, 1vh) * 100). And that's it.

    » Read more
  • Nowadays, how much memory will 1 million (1M) concurrent tasks consume? That is the question posed by hez2010, and he decided to find the answer by experimenting with a simple program in various programming languages: How Much Memory Do You Need in 2024 to Run 1 Million Concurrent Tasks?

    To summarize, Rust remains unbeatable, but the second position surprised me 😳

    » Read more
  • Is something coming? 😱🥶

    » 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.
Author

Hello, my name is Hoai - a developer who tells stories through writing ✍️ and creating products 🚀. With many years of programming experience, I have contributed to various products that bring value to users at my workplace as well as to myself. My hobbies include reading, writing, and researching... I created this blog with the mission of delivering quality articles to the readers of 2coffee.dev.Follow me through these channels LinkedIn, Facebook, Instagram, Telegram.

Did you find this article helpful?
NoYes

Comments (0)

Leave a comment...