Docker Compose Introduction

Docker Compose is a tool released by Docker Inc. that enables the definition and management of multi-container applications. It uses YAML files to configure application services and provides a set of commands to manage these services.
With Docker Compose, you can easily define, start, stop, and restart applications composed of multiple Docker containers. You can define information for each container such as images, ports, environment variables, and data volumes, and you can also define dependencies and links between containers.
The workflow of Docker Compose is as follows:

  1. Define application services and configurations using a YAML file.
  2. Start the application using the docker-compose up command; Docker Compose will automatically pull container images and start the containers.
  3. Stop and remove containers using the docker-compose down command.
    In addition to these basic commands, Docker Compose offers other commands such as docker-compose ps for viewing container status and docker-compose logs for viewing container logs. Through these commands, you can easily manage and monitor your Docker container applications.

YAML File Parameters

The following are some common parameters in a Docker Compose YAML file and their purposes:

  • version: Specifies the version of the Docker Compose file format.
  • services: Defines each service and its configuration options.
  • build: Specifies the path to the Dockerfile for building the image.
  • image: Specifies the name and version of the image to use.
  • container_name: Specifies the name of the container.
  • ports: Specifies the mapping relationship between container ports and host ports.
  • volumes: Specifies the mounting relationship between containers and host directories.
  • environment: Specifies environment variables for the container.
  • networks: Specifies the networks used by the container.
  • depends_on: Specifies dependencies between containers.
  • restart: Specifies the restart policy after a container stops.
  • command: Overrides the default command executed when the container starts.
    The above are some common parameters; Docker Compose supports other parameters and options depending on the requirements of your application. When writing Docker Compose YAML files, you can refer to the official Docker documentation for more detailed explanations of parameters and options.

version

In Docker Compose, version is the keyword used to specify the Docker Compose syntax version used by the Compose file. Docker Compose supports different versions of Compose file syntax, and each version has its own specific features and grammar rules.
At the beginning of a Compose file, you need to specify the Compose file syntax version to be used, for example:

version: '3.8'

Here, 3.8 refers to the syntax version of the Compose file. Currently, Docker Compose supports versions including 1, 2, 2.1, 3, 3.1, 3.2, 3.3, 3.4, 3.5, 3.6, 3.7, 3.8, and others. Each version has its own specific grammar rules and features.
It is worth noting that different versions of Compose file syntax have varying levels of support for Docker Engine versions. Therefore, you need to select a suitable Compose file syntax version based on your Docker Engine version. If the Compose file version you use does not support your Docker Engine version, you may encounter issues or errors.
The following table lists common Docker Engine versions and their compatible Docker Compose versions:

Docker Engine Version Minimum Compatible Docker Compose Version
1.10.0 1.6.0
1.12.0 1.8.0
1.13.0 1.10.0
17.04.0-ce 1.13.0
17.06.0-ce 1.14.0
17.09.0-ce 1.16.0
17.12.0-ce 1.18.0
18.06.0-ce 1.22.0
18.09.0-ce 1.23.0
19.03.0-ce 1.24.0
19.03.12 1.25.0
20.10.0 1.27.0
20.10.2 1.28.0
20.10.6 1.29.0
The above table lists some common Docker Engine versions and their compatible Docker Compose versions; actual compatibility may vary due to updates in Docker Engine and Docker Compose. When selecting a Docker Compose version, ensure that your Docker Engine version is compatible with the selected Docker Compose version.

volumes

In Docker Compose, volumes is a mechanism used to manage file sharing between containers and the host. It allows mapping folders or files on the host to folders or files inside the container so that data on the host can be accessed within the container.
In a docker-compose.yml file, you can use the volumes keyword to define volumes. For example:

version: '3'
services:
  web:
    image: nginx
    ports:
      - "8080:80"
    volumes:
      - /path/on/host:/path/on/container

In the above configuration, the directory /path/on/host on the host is mounted to the directory /path/on/container inside the container. This means that when accessing the /path/on/container directory within the container, you are actually accessing the /path/on/host directory on the host. This volume is created when the container starts and deleted when the container stops, allowing data persistence.
Additionally, anonymous volumes can be defined in a docker-compose.yml file. An anonymous volume is a volume where no specific host directory is explicitly specified; Docker automatically assigns it a unique path. For example:

version: '3'
services:
  db:
    image: postgres
    volumes:
      - /var/lib/postgresql/data
      - /etc/postgresql/myconfig.cfg
      - dbdata:/var/lib/postgresql/backup
volumes:
  dbdata:

In the above configuration, an anonymous volume dbdata is defined and mapped to the /var/lib/postgresql/backup directory inside the container. The data for this anonymous volume will be stored in Docker-managed volumes and can be shared between containers.
Using volumes allows better data sharing between containers and the host and facilitates data backup and recovery.

ports

In Docker Compose, ports is a configuration option used to map ports inside the container to ports on the host. ports allows you to specify the port inside the container and the port on the host to which it should be mapped.
The format for ports is as follows:

ports:
  - <host_port>:<container_port>

Where <host_port> represents the port number to be mapped on the host, and <container_port> represents the port number to be mapped inside the container.
For example, the following is an example of a Docker Compose file containing a ports configuration option:

version: '3'
services:
  web:
    image: nginx:latest
    ports:
      - "8080:80"

The ports configuration option in this file maps the 80 port inside the container to the 8080 port on the host. This means that when the container is running, you can access the service it provides via http://localhost:8080.
It should be noted that the ports configuration option only maps ports inside the container to ports on the host; it does not open ports on the host itself. If your firewall or network environment requires certain ports to be opened, ensure that these ports are opened on the host so that external clients can access the services provided by your container.

Docker Compose File Example

The YAML file for Docker Compose is a text file used to define Docker application services, networks, volumes, and their relationships and configurations. Below is an example of a Docker Compose YAML file:

version: "3.9"
services:
  web:
    build: .
    ports:
      - "5000:5000"
    volumes:
      - .:/code
    depends_on:
      - db
  db:
    image: postgres
    environment:
      POSTGRES_PASSWORD: example

In this example, we define two services: web and db. The web service builds an image using the Dockerfile in the current directory and maps the container's port 5000 to the host's port 5000. The web service also maps the current directory to the /code directory inside the container to allow access to application code within the container. The web service also declares a dependency, meaning it depends on the db service.
The db service uses the Postgres image and sets an environment variable POSTGRES_PASSWORD to set the password for the Postgres database when starting the container.
In addition to service definitions, a Docker Compose YAML file can include other sections such as networks (networks) and volumes (volumes), as well as other configuration options like environment (environment variables) and deploy (deployment).