🚀 Think you’ve got what it takes for a career in Data? Find out in just one minute!

Docker Compose: Simplifying Containerized Applications

-
4
 m de lecture
-
docker compose

If you're reading this article, you're probably familiar with this very popular containerisation tool in the DevOps world: Docker. But what exactly is Docker Compose, then? Docker Compose is used to launch applications that need several containers to run, via a YAML file.

It is often necessary for an application to run several containers for different tasks. As a result, it can be complex to run them separately, while having them communicate with each other. That’s where Docker Compose comes in, and will make your task a whole lot easier.

Orchestration and the various possible actions are written into a docker-compose.yml file, the operation of which we will explain in more detail in this article. This single file will contain a multitude of possible actions, from building your Docker images to connections and volumes.

Installing Docker Compose

To start using Docker Compose, you first need to install the tool on your system. It’s easy to install and is available for different platforms.

1. Checking the prerequisites: before installing Docker Compose, make sure you already have Docker installed on your system. Docker Engine is required for it to work properly. Administrator or sudo privileges are required for installation.

2. Install Docker Compose on your platform:

a. On Linux distributions, installation is via the package manager

For Ubuntu or Debian, the commands to run are :

				
					$ sudo apt-get update
$ sudo apt-get install docker-compose-plugin
				
			
      • For RPM-based distributions :
				
					$ sudo yum update
$ sudo yum install docker-compose-plugin
				
			

b. For Windows and macOS, Docker Compose is usually installed automatically with Docker Desktop.

c. You can also install Docker Compose manually by downloading the binaries from the official GitHub repository.

3. Check your installation by running the following command:

				
					$ docker-compose --version
				
			

This command will display the version of Docker Compose installed, confirming that it has been successfully installed.

The docker-compose.yml file

This file contains all the rules and configurations needed to run your containers. The YAML syntax is designed to be humanly comprehensible while being optimised for computers.

Its structure is made up of different elements:

VersionThis parameter is essential, and tells Docker Compose which version to use, in accordance with the version of Docker installed on your system.
ServicesThis section is where you will define the different containers for your application. Each service is an individual container that can be built from an existing Docker image (via a Docker Hub repository for example), or from a custom Dockerfile.
NetworksThese define how the different services communicate with each other. It is possible to create custom networks to isolate services, or use default networks.
VolumesThey allow you to specify the volumes mounted in your containers. They are used to store persistent data or to share files between containers.
Environment variablesYou have the option of defining environment variables for your services. These can be used to configure the behaviour of your applications. You can define them directly in the Docker Compose file, or in a .env file

Here is an example of a Docker Compose file:

In this example, we have 2 services: web and db. The first uses the NGINX image, exposes port 80 and mounts a volume for the HTML files. The second service uses the MySQL image, defines a password and mounts a volume for the database.

By using the Docker Compose file structure, you can create more complex configurations for your multi-container applications.

Application deployment and management

When you’ve finished configuring your services, volumes and networks in Docker Compose, you can deploy and manage your multi-container applications efficiently.

  • Initial deployment

To deploy your application, simply run the following command in the directory where your Docker Compose file is located:

				
					$ docker-compose up
				
			

This command will build the necessary containers and all the objects defined in your Docker Compose file (volumes, networks, etc), and start the services.

  • Container management

Here are some useful commands for managing containers:
    • To stop services and containers
				
					$ docker-compose down
				
			
    • To restart services
				
					$ docker-compose restart
				
			
    • To display information about running containers
				
					$ docker-compose ps
				
			
    • You have the option of scaling a specific service by specifying the number of replicas you want. The following command scales the web service with 3 replicas:
				
					$ docker-compose up --scale web=3
				
			

  • Managing environment variables

As we saw earlier, Docker Compose allows you to specify environment variables directly in the YAML file, or in an .env file. These variables will be useful for configuring the behaviour of your applications, such as sensitive information like passwords, keys, etc.

  • Monitoring

In the event of problems or anomalies, Docker Compose provides tools for examining container log files. The following command is used to obtain them:

				
					$ docker-compose logs <nom-du-service>
				
			

A few tips for using Docker Compose

It is important to follow a few best practices to ensure that your applications are deployed smoothly and efficiently.

  • Use reliable images from trusted sources wherever possible. Unmaintained or obsolete images can present security or compatibility problems.
  • Separate your environments: use different Docker Compose files depending on the environment (test, production, development, for example). This allows you to segment your work and keep your configurations clean and organised.
  • Naming nomenclature: use understandable and meaningful names for your services.
    Isolate networks: this will enhance security and avoid port opening conflicts.
  • Monitor your container resources carefully (CPU, memory, disk space) to ensure that they are running optimally.
  • Make regular back-ups of your volumes to avoid any loss of data in the event of a problem.
  • Update your images regularly to ensure greater stability and avoid potential security problems.

Conclusion

We’ve seen in this article that Docker Compose is a powerful tool for deploying and managing multi-container applications. With a carefully configured YAML file, you can orchestrate your services, volumes and networks efficiently.

Don’t forget to follow best practices to ensure stable and functional applications.

Facebook
Twitter
LinkedIn

DataScientest News

Sign up for our Newsletter to receive our guides, tutorials, events, and the latest news directly in your inbox.

You are not available?

Leave us your e-mail, so that we can send you your new articles when they are published!
icon newsletter

DataNews

Get monthly insider insights from experts directly in your mailbox