The docker-compose file

The docker-compose tool orchestrates containers using YAML, which is a Yet Another Markup Language called the docker-compose file. YAML is a human-friendly data serialization format. Docker began its journey as a container enablement tool, and it is growing by leaps and bounds as an ecosystem to automate and accelerate most of the tasks such as container provisioning, networking, storage, management, orchestration, security, governance, and persistence. Consequently, the docker-compose file format and its version are revised multiple times to keep up with the Docker platform. At the time of writing this edition, the latest version of the docker-compose file is version 3. The following table lists the docker-compose file and the Docker Engine version compatibility matrix:

Docker Compose file format

Docker Engine

Remarks

3, 3.1

1.13.0+

Provides support for docker stack deploy and docker secrets

2.1

1.12.0+

Introduced a few new parameters

2

1.10.0+

Introduced support for named volumes and networks

1

1.9.0+

Will be deprecated in the future compose releases

The docker-compose tool by default uses a file named as docker-compose.yml or docker-compose.yaml to orchestrate containers. This default file can be modified using the -f option of the docker-compose tool. The following is the format of the docker-compose file:

version: "<version>" 
services:
<service>:
<key>: <value>
<key>:
- <value>
- <value>
networks:
<network>:
<key>: <value>

volumes:
<volume>:
<key>: <value>

Here, the options used are as follows:

  • <version>: This is the version of the docker-compose file. Refer to the preceding version table.
  • <service>: This is the name of the service. You can have more than one service definition in a single docker-compose file. The service name should be followed by one or more keys. However, all the services must either have an image or a build key, followed by any number of optional keys. Except for the image and build keys, the rest of the keys can be directly mapped to the options in the docker run subcommand. The value can be either a single value or multiple values. All the <service> definitions must be grouped under the top-level services key.
  • <network>: This is the name of the networks that are used by the services. All the <network> definitions must be grouped under the top-level networks key.
  • <volume>: This is the name of the volume that is used by the services. All the <volume> definitions must be grouped under the top-level volume key.

Here, we are listing a few keys supported in the docker-compose file version 3. Refer to https://docs.docker.com/compose/compose-file for all the keys supported by docker-compose.

  • image: This is the tag or image ID.
  • build: This is the path to a directory containing a Dockerfile.
  • command: This key overrides the default command.
  • deploy: This key has many subkeys and is used to specify deployment configuration. This is used only in the docker swarm mode.
  • depends_on: This is used to specify the dependencies between services. It can be further extended to chain services based on their conditions.
  • cap_add: This adds a capability to the container.
  • cap_drop: This drops a capability of the container.
  • dns: This sets custom DNS servers.
  • dns_search: This sets custom DNS search servers.
  • entrypoint: This key overrides the default entrypoint.
  • env_file: This key lets you add environment variables through files.
  • environment: This adds environment variables and uses either an array or a dictionary.
  • expose: This key exposes ports without publishing them to the host machine.
  • extends: This extends another service defined in the same or a different configuration file.
  • extra_hosts: This enables you to add additional hosts to /etc/hosts inside the container.
  • healthcheck: This allows us to configure the service health check.
  • labels: This key lets you add metadata to your container.
  • links: This key links to containers in another service. Usage of links is strongly discouraged.
  • logging: This is used to configure the logging for the service.
  • network: This is used to join the service to the network defined in the top-level networks key.
  • pid: This enables the PID space sharing between the host and the containers.
  • ports: This key exposes ports and specifies both the HOST_port:CONTAINER_port ports.
  • volumes: This key mounts path or named volumes. The named volumes need to be defined in the top-level volumes key.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset