Docker Compose is a tool integrated into Docker that simplifies the process of managing multi-container applications.

Docker Compose provides many benefits, including:

  1. Easy to manage container network: Connecting two containers in Docker Compose via Docker network is very simple: define the network and tell the container to connect to the network. In addition, Docker Compose can automatically create and destroy networks when the container itself is created and destroyed.

  2. Container dependencies: If a Docker container needs to rely on another Docker container to start and run (such as a database), Docker Compose can be used to define dependencies for the container and require the dependencies to start and run before the container.

  3. Replicable system Settings: The container’s pre-run Settings can be easily copied to other systems. While it is theoretically possible to do the same thing manually in Bash, it is less flexible and more difficult to adapt to changes.

Install Docker Compose on Linux

Docker Compose is available in the repository of most Linux distributions.

On Ubuntu and Debian based distributions, install Docker Compose using the following command:

sudo apt install docker-compose
Copy the code

On Arch and Manjaro, you can use:

sudo pacman -S docker-compose
Copy the code

On Fedora, you can use the DNF command:

sudo dnf install docker-compose
Copy the code

Installing Docker Compose on CentOS can be a bit trickier.

For other distributions, check the installation documentation for information.

Create our first Docker Compose file

This guide assumes that you are already familiar with and familiar with the Docker CLI tool. If not, check it out.

The Docker Compose configuration information is usually stored in a file named docker-comemess. yml. The default configuration file named docker-compose will be found in the same directory when the docker-compose command is executed. The configuration file is in YAML format.

If you want to learn more about YAML, take a look at this: The basics of YAML that every DevOps engineer must know

Let’s create a YAML file for a NextCloud instance and walk through it together.

First, you need to create an empty directory on your system and create a file called docker-comemage. yml like this:

Then fill the file with the following:

version: '3.5'
services:
  nextcloud_app:
    container_name: nextcloud_app
    image: nextcloud
    restart: unless-stopped
    networks:
      - nextcloud
    ports:
      - 80: 80
    volumes:
      - ./data/app:/var/www/html
    depends_on:
      - nextcloud_mariadb

  nextcloud_mariadb:
    container_name: nextcloud_mariadb
    image: mariadb
    restart: unless-stopped
    networks:
      - nextcloud
    volumes:
      - ./data/mariadb:/var/lib/mysql
    environment:
      MARIADB_ROOT_PASSWORD: 'mariadb'

networks:
  nextcloud:
    name: nextcloud_docker_network
Copy the code

Next, all you need to do is run the following command:

docker-compose up -d
Copy the code

As shown below, the NextCloud container has been started successfully:

For information on how to deploy NextCloud with Docker on Linux, please refer to this introduction.

Open your browser, visit http://localhost, and you should see Nextcloud’s login page, which looks like this:

Learn about the Docker Compose configuration file

Now that you’ve verified that the YAML file actually works, let’s review the file to see what it does.

“Version” label

version: '3.5'
Copy the code

First, the Version tag specifies the version of the Docker Compose file format, because different versions will have different syntax. Usually we will use the latest version, but this is not necessary, and you may need to use a specific version if you have legacy files.

“Services” TAB

services:
  nextcloud_app:
    container_name: nextcloud_app
    image: nextcloud
    restart: unless-stopped
    networks:
      - nextcloud
    ports:
      - 80: 80
    volumes:
      - ./data/app:/var/www/html
    depends_on:
      - nextcloud_mariadb

  nextcloud_mariadb:
    container_name: nextcloud_mariadb
    image: mariadb
    restart: unless-stopped
    networks:
      - nextcloud
    volumes:
      - ./data/mariadb:/var/lib/mysql
    environment:
      MARIADB_ROOT_PASSWORD: 'mariadb'
Copy the code

Next comes the Services tag. It contains a list of all the applications created when Docker-compose up -D is started.

Let’s look at the first container, nextcloud_app. This name is used as an application-specific identifier for other Docker Compose commands. Note that this is not the name of the container itself, as explained later.

What follows defines configuration information about the container itself. The meanings of all relevant fields are listed below. If you already know about Docker, most fields should be self-explanatory:

  • container_name– Defines the name of the container. Equivalent to the –name option.
  • image– Defines the image to be extracted from the container.
  • restart– Defines the container restart policy. Equivalent to –restart.
  • networks– Defines the network to which the container is connected. The network can be new or existing. This value will use the network identifier defined in the “networks” tag, not the actual network name.
  • ports– Defines the host ports that the container can connect to. Equivalent to –publish.
  • volumes– Defines the disk volume of the container. Equivalent to –volume.
  • environment– Defines the container’s environment variables. Equivalent to –env. This option supports two syntax types: yesvariable: value; Or if you’re used to the syntax of the Docker CLI, the other option is probably more familiar- variable=value.
  • depends_on– Specifies the container’s dependencies. The current container is not started until the dependency is started.

“Networks” TAB

networks:
  nextcloud:
    name: nextcloud_docker_network
Copy the code

The Networks TAB is used to define the networks that we list in the networks configuration of the container.

In the Networks configuration, the identifier of the container that uses network in services is first listed, that is, nextCloud.

Next, the name nextcloud_docker_network is defined.

Docker network ls docker network ls

If you want to add the container to an existing network, replace “network_name” with the name of the actual Docker network using the following syntax:

networks:
  nextcloud:
    external: true
    name: network_name
Copy the code

Docker Compose configuration file.

The name of the docker-comemess. yml configuration file is not required, it can be any name, but if you call it another name, you need to specify the parameter in the startup command, such as docker-compose up -f mycomemess. yml.

conclusion

Now you know the basics of Docker Compose and the benefits of using it.

Original link: linuxhandbook.com/docker-comp…