Post

Docker Compose file and Docker Compose Commands

Let's learn Docker Compose commands.

Docker Compose file and Docker Compose Commands

Docker Compose file Commands

Docker Compose is a tool that allows you to define and run multiple containers at once. To operate a single service, multiple applications need to run together. The content previously written in the dockerfile can be written in docker-compose.yml. If you are not yet fully familiar with Dockerfile commands, I recommend reviewing them again. Let’s apply the newly added commands.
For reference, I haven’t personally worked on a project where I created and deployed a Docker Compose file from scratch. In most cases, I have utilized pre-existing Docker Compose files that are already set up to run multiple applications for a single service.(I’ll make sure to explore creating and deploying a Docker Compose file myself in the future. If you want to define, build, and run it yourself, check out the Docker Docs.)

CategoryDescriptionExample
servicesDefines the container options
to run using Compose.
service:
etcd:
image: quay.io/coreos/etcd:v3.5.16
minio:
image: minio/minio:RELEASE.2023-03-20T20-16-18Z
buildBuilds the container.webapp:
build: .
imageSpecifies the image
to run using Compose.
etcd:
image: quay.io/coreos/etcd:v3.5.16
commandSpecifies the command
to execute when running the container.
etcd:
image: quay.io/coreos/etcd:v3.5.16
command: etcd -advertise-client-urls=http://127.0.0.1:2379 -listen-client-urls
portDeclares the ports exposed
by the container.
minio:
image: minio/minio:RELEASE.2023-03-20T20-16-18Z
port:
- "9001:9001"
- "9000:9000"
linkSpecifies the container to link
when connecting to another container.
webserver:
image: wordpress:latest
link:
- db:mysql
exposePorts exposed to linked containers.webapp:
expose:
volumesMounts volumes in the container.etcd:
image: quay.io/coreos/etcd:v3.5.16
volumes:
- /var/lib/docker/volumes/etcd:/etcd
environmentDefines environment variables
to apply in the container.
etcd:
image: quay.io/coreos/etcd:v3.5.16
environment:
- ETCD_AUTO_COMPACTION_MODE=revision
restartRestart policy applied
when the container stops.
- no: Do not restart
- always: Always restart the container
- on-failure: Restart only an error occurs
etcd:
image: quay.io/coreos/etcd:v3.5.16
restart: always
depends_onDefines dependencies
between containers.
The container must start first.
services:
standalone:
image: milvusdb/milvus:v2.5.4-gpu
depends_on:
- "etcd"
- "minio"

Docker Compose file Example

The following docker-compose-gpu.yml file defines the installation of Milvus, a type of VectorDB, along with the necessary applications such as MinIO and Attu. I will use this as a reference for the explanation.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
(base) jaoneol@DESKTOP-B7GM3C5:~$ vi docker-compose.yml
# compose version, The supported syntax varies depending on the version.
# Starting from Docker Compose V2, the version is automatically detected, so there is no need to specify it explicitly.
# version: '3.5'

# Defines the container options to be executed via Compose.
# The services are broadly defined into four containers: `etcd`, `MinIO`, `Standalone`, and `Attu`.
services:
  etcd: # Default container name, If `container_name` is not specified, this will be used.
    container_name: milvus-etcd
    image: quay.io/coreos/etcd:v3.5.16
    restart: always # There are three options available.(no, always, on-failure)
    environment:
      - ETCD_AUTO_COMPACTION_MODE=revision
      - ETCD_AUTO_COMPACTION_RETENTION=1000
      - ETCD_QUOTA_BACKEND_BYTES=4294967296
      - ETCD_SNAPSHOT_COUNT=50000
    volumes: # Mounts volumes in the container.
      # `DOCKER_VOLUME_DIRECTORY` must be predefined as an environment variable in the Linux (Ubuntu) system.
      - ${DOCKER_VOLUME_DIRECTORY:-.}/volumes/etcd:/etcd
    command: etcd -advertise-client-urls=http://127.0.0.1:2379 -listen-client-urls http://0.0.0.0:2379 --data-dir /etcd
    healthcheck: # **monitors the health of a container** by running a command at specified intervals.
      test: ["CMD", "etcdctl", "endpoint", "health"]
      interval: 30s
      timeout: 20s
      retries: 3

  minio:
    container_name: milvus-minio
    image: minio/minio:RELEASE.2023-03-20T20-16-18Z
    restart: always
    environment:
      MINIO_ACCESS_KEY: minioadmin
      MINIO_SECRET_KEY: minioadmin
    ports:
      - "9001:9001"
      - "9000:9000"
    volumes:
      - ${DOCKER_VOLUME_DIRECTORY:-.}/volumes/minio:/minio_data
    command: minio server /minio_data --console-address ":9001"
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:9000/minio/health/live"]
      interval: 30s
      timeout: 20s
      retries: 3

  standalone:
    container_name: milvus-standalone
    image: milvusdb/milvus:v2.5.4-gpu
    restart: always
    command: ["milvus", "run", "standalone"]
    security_opt:
    - seccomp:unconfined
    environment:
      ETCD_ENDPOINTS: etcd:2379
      MINIO_ADDRESS: minio:9000
    volumes:
      - ${DOCKER_VOLUME_DIRECTORY:-.}/volumes/milvus:/var/lib/milvus
    ports:
      - "19530:19530"
      - "9091:9091"
    deploy:
      resources:
        reservations:
          devices:
            - driver: nvidia
              capabilities: ["gpu"]
              device_ids: ["0"]
    depends_on: # The container must start first.
      - "etcd"
      - "minio"

  attu:
    container_name: attu
    image: zilliz/attu:v2.4.7
    restart: always
    environment:
      MILVUS_URL: milvus-standalone:19530
    ports:
      - "8000:3000"
    depends_on:
      - "standalone"

networks:
  default:
    name: milvus

Docker Compose Commands

The following docker-compose.yml file defines the installation of Milvus, a type of VectorDB, along with the necessary applications such as MinIO and Attu. I will use this as a reference for the explanation.

CommandDescriptionExample
docker compose up -dStarts services defined in docker-compose.ymldocker compose up -d
docker compose downStops and removes Compose servicesdocker compose down
docker compose down --volumesStops and removes Compose services including volumesdocker compose down
docker compose psLists running Compose servicesdocker compose ps
docker compose logsDisplays logs from Compose servicesdocker compose logs
docker compose buildBuilds images for Compose servicesdocker compose build

Docker Compose Commands Practice

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# Check the Docker volumes before running `docker compose up -d`.
(base) jaoneol@DESKTOP-B7GM3C5:~$ docker volume ls
DRIVER    VOLUME NAME
local     mysql_volume

# Starts services defined in `docker-compose-gpu.yml` 
(base) jaoneol@DESKTOP-B7GM3C5:~$ sudo docker compose up -d
[sudo] password for jaoneol:
[+] Running 4/4
 ✔ Container milvus-etcd        Started                                                                                                                                                                                              0.5s 
 ✔ Container milvus-minio       Started                                                                                                                                                                                              0.4s 
 ✔ Container milvus-standalone  Started                                                                                                                                                                                              0.7s 
 ✔ Container attu               Started                                                                                                                                                                                              1.0s 

# Check the running containers using `docker compose ps` or `docker compose ps -a`
(base) jaoneol@DESKTOP-B7GM3C5:~$ docker compose ps
NAME                IMAGE                                      COMMAND                  SERVICE      CREATED         STATUS                   PORTS
attu                zilliz/attu:v2.4.7                         "docker-entrypoint.s…"   attu         6 minutes ago   Up 2 minutes             0.0.0.0:8000->3000/tcp, [::]:8000->3000/tcp
milvus-etcd         quay.io/coreos/etcd:v3.5.5                 "etcd -advertise-cli…"   etcd         6 minutes ago   Up 2 minutes (healthy)   2379-2380/tcp
milvus-minio        minio/minio:RELEASE.2023-03-20T20-16-18Z   "/usr/bin/docker-ent…"   minio        6 minutes ago   Up 2 minutes (healthy)   0.0.0.0:9000-9001->9000-9001/tcp, :::9000-9001->9000-9001/tcp
milvus-standalone   milvusdb/milvus:v2.3.3                     "/tini -- milvus run…"   standalone   6 minutes ago   Up 2 minutes (healthy)   0.0.0.0:9091->9091/tcp, :::9091->9091/tcp, 0.0.0.0:19530->19530/tcp, :::19530->19530/tcp
(base) jaoneol@DESKTOP-B7GM3C5:~$ docker volume ls
DRIVER    VOLUME NAME
local     f2b67c39616d834b6266397c6d0acc0226db6ad5587ddc13621327d4e1cc68b2
local     mysql_volume

# Stops and removes Compose services including volumes
(base) jaoneol@DESKTOP-B7GM3C5:~$ docker compose down --volumes
[+] Running 5/5
 ✔ Container attu               Removed                                                                                                                                                                                                                                                     0.3s 
 ✔ Container milvus-standalone  Removed                                                                                                                                                                                                                                                     0.4s 
 ✔ Container milvus-minio       Removed                                                                                                                                                                                                                                                     1.0s 
 ✔ Container milvus-etcd        Removed                                                                                                                                                                                                                                                     0.4s 
 ✔ Network milvus               Removed                                                                                                                                                                                                                                                     0.4s 
# Check the Docker volumes after running `docker compose down --volumes`.
# You can verify that the volume has been deleted.
(base) jaoneol@DESKTOP-B7GM3C5:~$ docker volume ls
DRIVER    VOLUME NAME
local     mysql_volume

This post is licensed under CC BY 4.0 by the author.