How to create and run multiple postgreSQL containers with docker compose on single docker host at the same time.

# How to create and run multiple postgreSQL containers with docker compose on single docker host at the same time.

## Obejctives
- Run multiple PostgreSQL containers with
    - multiple versions
    - different ports
    - different local volumes

## Restriction
- Official PostgreSQL container image doesn't have Japanese locale such like ja_JP.utf8 .

## Official images and available version tags

- https://hub.docker.com/_/postgres

## System
```
# cat /etc/redhat-release
Rocky Linux release 9.4 (Blue Onyx)

# docker -v
Docker version 26.1.2, build 211e74b
```

## Tips

:::note warn
Separate the project name (small letter) for each containers with docker compose command.
:::
```
Options:
  -p, --project-name string        Project name
```
```
must consist only of lowercase alphanumeric characters, hyphens, and underscores as well as start with a letter or number
```
:::note warn
Mapping different port numbers.
:::
:::note warn
Mapping database data directory to local volume.
:::
- If you remove container with misstake, you can recovery database with recreating conatainer.

## Make 3 compose files
```
# cat PGSQL15.7-PJ1.yml
services:
  db:
    image: postgres:15.7
    container_name: postgres_15.7-1
    ports:
      - 5433:5432
    volumes:
      - type: bind
        source: /docker-postgres/15.7-1/data
        target: /var/lib/postgresql/data
    environment:
      - POSTGRES_PASSWORD=P@ssw0rd
```
```
# cat PGSQL15.7-PJ2.yml
services:
  db:
    image: postgres:15.7
    container_name: postgres_15.7-2
    ports:
      - 5434:5432
    volumes:
      - type: bind
        source: /docker-postgres/15.7-1/data
        target: /var/lib/postgresql/data
    environment:
      - POSTGRES_PASSWORD=P@ssw0rd
```
```
# cat PGSQL14-PJ3.yml
services:
  db:
    image: postgres:14
    container_name: postgres_14-1
    ports:
      - 5435:5432
    volumes:
      - type: bind
        source: /docker-postgres/14-1/data
        target: /var/lib/postgresql/data
    environment:
      - POSTGRES_PASSWORD=P@ssw0rd
```

## Prepare local volumes
```
# mkdir -p /docker-postgres/15.7-1/data /docker-postgres/15.7-1/data /docker-postgres/14-1/data
# ls -l /docker-postgres/15.7-1/data /docker-postgres/15.7-1/data /docker-postgres/14-1/data
/docker-postgres/14-1/data:
合計 0

/docker-postgres/15.7-1/data:
合計 0

/docker-postgres/15.7-1/data:
合計 0
```

## Execute docker compose with dry run at first
```
# docker compose -p pj1 -f PGSQL15.7-PJ1.yml --dry-run up
[+] Running 1/1
 ✔ DRY-RUN MODE -  db Pulled                                                                                                        1.3s
[+] Running 1/0
 ✔ DRY-RUN MODE -  Container postgres_15.7-1  Created                                                                               0.0s
end of 'compose up' output, interactive run is not supported in dry-run mode
```
(snip)
## Execute docker compose
```
# docker compose -p pj1 -f PGSQL15.7-PJ1.yml up -d
[+] Running 15/15
 ✔ db Pulled                                                                                                                       18.5s
   ✔ 09f376ebb190 Pull complete                                                                                                     6.0s
   ✔ 119215dfb3e3 Pull complete                                                                                                     6.0s
   ✔ 94fccb772ad3 Pull complete                                                                                                     6.5s
   ✔ 0fc3acb16548 Pull complete                                                                                                     6.7s
   ✔ d7dba7d03fe8 Pull complete                                                                                                     7.8s
   ✔ 898ae395a1ca Pull complete                                                                                                     8.0s
   ✔ 088e651df7e9 Pull complete                                                                                                     8.0s
   ✔ ed155773e5e0 Pull complete                                                                                                     8.0s
   ✔ 52df7d12fb73 Pull complete                                                                                                    15.3s
   ✔ bab1ecc22dc9 Pull complete                                                                                                    15.4s
   ✔ 1655a257a5b5 Pull complete                                                                                                    15.4s
   ✔ 978f02dfc247 Pull complete                                                                                                    15.4s
   ✔ d715d7d9aee0 Pull complete                                                                                                    15.4s
   ✔ b2e9251b2f8d Pull complete                                                                                                    15.4s
[+] Running 1/1
 ✔ Container postgres_15.7-1  Started 
```
```: same image
 # docker compose -p pj2 -f PGSQL15.7-PJ2.yml up -d
[+] Running 1/1
 ✔ Container postgres_15.7-2  Started  
```
```
# docker compose -p pj3 -f PGSQL14-PJ3.yml up -d
[+] Running 15/15
 ✔ db Pulled                                                                                                                       22.7s
   ✔ 09f376ebb190 Already exists                                                                                                    0.0s
   ✔ eae1b5cf3e42 Pull complete                                                                                                     0.7s
   ✔ a9404ea998bc Pull complete                                                                                                     1.1s
   ✔ 9b72f3da5ca8 Pull complete                                                                                                     1.2s
   ✔ 0862c192c702 Pull complete                                                                                                     2.4s
   ✔ ca21a003e48e Pull complete                                                                                                     2.6s
   ✔ 16e7bd8396f1 Pull complete                                                                                                     2.6s
   ✔ 1c42d8ac920e Pull complete                                                                                                     7.0s
   ✔ 3dc5508cdb5e Pull complete                                                                                                    19.8s
   ✔ 277e41134633 Pull complete                                                                                                    19.8s
   ✔ 8fec6d10d079 Pull complete                                                                                                    19.8s
   ✔ ff5496d1ae7d Pull complete                                                                                                    19.9s
   ✔ acb6b31dbd6d Pull complete                                                                                                    19.9s
   ✔ faf3364210ef Pull complete                                                                                                    19.9s
[+] Running 1/1
 ✔ Container postgres_14-1  Started 
```

## Check containers and database connections
```
# docker ps
CONTAINER ID   IMAGE           COMMAND                   CREATED              STATUS              PORTS                                       NAMES
f0130bc31557   postgres:14     "docker-entrypoint.s…"   About a minute ago   Up About a minute   0.0.0.0:5435->5432/tcp, :::5435->5432/tcp   postgres_14-1
0360be108fdf   postgres:15.7   "docker-entrypoint.s…"   2 minutes ago        Up 2 minutes        0.0.0.0:5434->5432/tcp, :::5434->5432/tcp   postgres_15.7-2
fbf9655439f0   postgres:15.7   "docker-entrypoint.s…"   3 minutes ago        Up 3 minutes        0.0.0.0:5433->5432/tcp, :::5433->5432/tcp   postgres_15.7-1
```
```
# docker compose ls
NAME                STATUS              CONFIG FILES
pj1                 running(1)          /root/docker-compose/PGSQL15.7-PJ1.yml
pj2                 running(1)          /root/docker-compose/PGSQL15.7-PJ2.yml
pj3                 running(1)          /root/docker-compose/PGSQL14-PJ3.yml
```
```
# docker inspect postgres_15.7-1
(snip)
            "Mounts": [
                {
                    "Type": "bind",
                    "Source": "/docker-postgres/15.7-1/data",
                    "Target": "/var/lib/postgresql/data"
                }
            ],
(snip)
```
```
# ls -l /docker-postgres/15.7-1/data /docker-postgres/15.7-1/data /docker-postgres/14-1/data
/docker-postgres/14-1/data:
合計 64
-rw-------. 1 systemd-coredump input     3  5月 16 20:23 PG_VERSION
drwx------. 5 systemd-coredump input    41  5月 16 20:23 base
drwx------. 2 systemd-coredump input  4096  5月 16 20:24 global
drwx------. 2 systemd-coredump input     6  5月 16 20:23 pg_commit_ts
drwx------. 2 systemd-coredump input     6  5月 16 20:23 pg_dynshmem
-rw-------. 1 systemd-coredump input  4821  5月 16 20:23 pg_hba.conf
(snip)
-rw-------. 1 systemd-coredump input    88  5月 16 20:23 postgresql.auto.conf
-rw-------. 1 systemd-coredump input 28827  5月 16 20:23 postgresql.conf
-rw-------. 1 systemd-coredump input    36  5月 16 20:23 postmaster.opts
-rw-------. 1 systemd-coredump input    94  5月 16 20:23 postmaster.pid

/docker-postgres/15.7-1/data:
合計 64
-rw-------. 1 systemd-coredump input     3  5月 16 20:22 PG_VERSION
drwx------. 5 systemd-coredump input    33  5月 16 20:22 base
drwx------. 2 systemd-coredump input  4096  5月 16 20:24 global
drwx------. 2 systemd-coredump input     6  5月 16 20:22 pg_commit_ts
drwx------. 2 systemd-coredump input     6  5月 16 20:22 pg_dynshmem
-rw-------. 1 systemd-coredump input  4821  5月 16 20:22 pg_hba.conf
(snip)
-rw-------. 1 systemd-coredump input    88  5月 16 20:22 postgresql.auto.conf
-rw-------. 1 systemd-coredump input 29517  5月 16 20:22 postgresql.conf
-rw-------. 1 systemd-coredump input    36  5月 16 20:22 postmaster.opts
-rw-------. 1 systemd-coredump input    94  5月 16 20:22 postmaster.pid

/docker-postgres/15.7-1/data:
合計 64
-rw-------. 1 systemd-coredump input     3  5月 16 20:22 PG_VERSION
drwx------. 5 systemd-coredump input    33  5月 16 20:22 base
drwx------. 2 systemd-coredump input  4096  5月 16 20:24 global
drwx------. 2 systemd-coredump input     6  5月 16 20:22 pg_commit_ts
drwx------. 2 systemd-coredump input     6  5月 16 20:22 pg_dynshmem
-rw-------. 1 systemd-coredump input  4821  5月 16 20:22 pg_hba.conf
(snip)
-rw-------. 1 systemd-coredump input    88  5月 16 20:22 postgresql.auto.conf
-rw-------. 1 systemd-coredump input 29517  5月 16 20:22 postgresql.conf
-rw-------. 1 systemd-coredump input    36  5月 16 20:22 postmaster.opts
-rw-------. 1 systemd-coredump input    94  5月 16 20:22 postmaster.pid
```
```
#  docker exec -it postgres_15.7-1 /bin/bash
root@fbf9655439f0:/# ls -al /var/lib/postgresql/data
total 68
drwx------. 19 postgres root      4096 May 16 11:22 .
drwxr-xr-x.  1 postgres postgres    18 May 14 02:57 ..
drwx------.  5 postgres postgres    33 May 16 11:22 base
drwx------.  2 postgres postgres  4096 May 16 11:24 global
drwx------.  2 postgres postgres     6 May 16 11:22 pg_commit_ts
drwx------.  2 postgres postgres     6 May 16 11:22 pg_dynshmem
-rw-------.  1 postgres postgres  4821 May 16 11:22 pg_hba.conf
(snip)
```
- ctrl p > q for detach
```: from the local host with psql command
# psql postgres -h 127.0.0.1 -p 5434 -U postgres
ユーザ postgres のパスワード:
psql (13.14、サーバ 15.7 (Debian 15.7-1.pgdg120+1))
警告： psql のメジャーバージョンは 13 ですが、サーバのメジャーバージョンは 15 です。
         psql の機能の中で、動作しないものがあるかもしれません。
"help"でヘルプを表示します。

postgres=# select version();
                                                       version
---------------------------------------------------------------------------------------------------------------------
 PostgreSQL 15.7 (Debian 15.7-1.pgdg120+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 12.2.0-14) 12.2.0, 64-bit
(1 行)

postgres=# \q
```
- If you don't have psql command on the docker host, install psql command "dnf install postgresql" or connect from remote psql with proper hg_hba.conf.