Hai semuanya di materi kali ini kita akan membahas basic syntax dalam penulisan Compose file, diantaranya yang akan kita bahas yaitu

  1. Compose file version
  2. Service configuration reference
  3. Volume configuration reference
  4. Network configuration reference

Ok langsung ja kita kepembahasan yang pertama yaitu

Compose file version

There are several versions of the Compose file format – 1, 2, 2.x, and 3.x.The Compose file formats are now described in these references, specific to each version.

  1. Version 3
  2. Version 2
  3. Version 1 (Deprecated / obsolate)

The latest Compose file format is defined by the Compose Specification and is implemented by Docker Compose 1.27.0+.

Several things differ depending on which version you use:

  1. The structure and permitted configuration keys
  2. The minimum Docker Engine version you must be running
  3. Compose’s behaviour with regards to networking

Berikut adalah table compatibility matrix

Compose file format Docker engine release
3.8 19.03.0+
3.7 18.06.0+
3.6 18.02.0+
3.5 17.12.0+
3.4 17.09.0+
3.3 17.06.0+
3.2 17.04.0+

so in this tutorial we will covers latest version docker-compose file which is version 3.x, contoh penggunaanya seperti berikut

version: '3.8'
services:
    ...

Service configuration reference

A service definition contains configuration that is applied to each container started for that service, much like passing command-line parameters to docker run. Likewise, network and volume definitions are analogous to docker network create and docker volume create.

As with docker run, options specified in the Dockerfile, such as CMD, EXPOSE, VOLUME, ENV, are respected by default - you don’t need to specify them again in docker-compose.yml.

Berikut adalah beberapa list yang termasuk service configuration yang sering saya gunakan pada compose file specification version 3.x diantaranya:

  1. build
  2. command
  3. depends_on
  4. deploy
  5. dns
  6. dns_search
  7. entrypoint
  8. env_file
  9. environment
  10. healthcheck
  11. image
  12. networks
  13. ports
  14. profiles
  15. restart
  16. volumes

Volume configuration reference

While it is possible to declare volumes on the fly as part of the service declaration, this section allows you to create named volumes that can be reused across multiple services (without relying on volumes_from), and are easily retrieved and inspected using the docker command line or API. See the docker volume subcommand documentation for more information.

See use volumes and volume plugins for general information on volumes

Berikut adalah beberapa list yang termasuk volume configuration yang sering saya gunakan pada compose file specification version 3.x diantaranya:

  1. driver
  2. driver_opts
  3. labels
  4. name

Network configuration reference

The top-level networks key lets you specify networks to be created.

Berikut adalah beberapa list yang termasuk networking config yang sering saya gunakan pada compose file specification version 3.x diantaranya:

  1. driver
  2. driver_opts
  3. labels

Examples

For example if you wont run docker config like this:

docker network create backend && \
docker volume create pg_data && \
docker run --name db_test \
-p 5432:5432 \
-e POSTGRES_PASSWORD=password \
-e POSTGRES_USER=test_sit_user \
-e POSTGRES_DB=test_sit_db \
-v pg_data:/var/lib/postgresql/data \
-d postgres:12.6

First you need create/define a file docker-compose.yaml like this:

Setelah itu kita bisa jalankan menggunakan perintah

docker-compose up -d

Jika dijalankan maka outputnya seperti berikut:

 09-docker-compose  docker-compose -f .\docker-compose.yaml up -d
Creating network "09-docker-compose_backend" with the default driver
Creating volume "09-docker-compose_pg_data" with default driver

 09-docker-compose   docker-compose ps
         Name                       Command              State                    Ports
---------------------------------------------------------------------------------------------------------
09-docker-compose_db_1   docker-entrypoint.sh postgres   Up      0.0.0.0:5432->5432/tcp,:::5432->5432/tcp

 09-docker-compose  docker container inspect 09-docker-compose_db_1 -f '{{json .Config.Env}}' | python.exe -m json.tool

[
    "POSTGRES_PASSWORD=password",
    "POSTGRES_USER=test_sit_user",
    "POSTGRES_DB=test_sit_db"
]

 09-docker-compose  docker network ls
NETWORK ID     NAME                        DRIVER    SCOPE
8896f4f5181c   09-docker-compose_backend   bridge    local

 09-docker-compose  docker volume ls
DRIVER    VOLUME NAME
local     09-docker-compose_pg_data

 09-docker-compose  docker container ls
CONTAINER ID   IMAGE           COMMAND                  CREATED          STATUS          PORTS
             NAMES
08e9f406c84a   postgres:12.6   "docker-entrypoint.s…"   12 minutes ago   Up 12 minutes   0.0.0.0:5432->5432/tcp, :::5432->5432/tcp   09-docker-compose_db_1

Jadi kedepannya kita tidak perlu lagi mengingat config yang kita perlu run, cukup menggunakan template docker-compose.yaml dan perintah docker-compose up done!.