はじめに
本記事では、IBM Cloud上の「RHEL 9 IBMCloud VSI (VPC)」として提供されているRHEL 9.2 VMに、PostgreSQL のコンテナを立て、簡単なテーブルを作成する手順を紹介します。
Docker ComposeやDockerfileは使わず、テスト用にDocker CLIで1つのコンテナを直接立ち上げる簡易構成としています。
環境はIBM Technology Zoneを通じて作成・利用しています。
IBM社員やBusiness Partner様であればIBM IDを使用してご利用可能です。
1. RHEL 9.2 VM 起動
上記の環境を払い出すと以下のようにSSH鍵でアクセスできます。
$ ssh itzuser@*.*.*.* -p 2223 -i pem_ibmcloudvsi_download.pem
Register this system with Red Hat Insights: insights-client --register
Create an account or view all your systems at https://red.ht/insights-dashboard
* 「*.*.*.*」はパブリックIPアドレス
※RHELのバージョン確認
$ cat /etc/redhat-release
Red Hat Enterprise Linux release 9.2 (Plow)
2. Dockerインストール
以下の公式ドキュメントを参考にRHELにDockerをインストールします。
https://docs.docker.com/engine/install/rhel/
$ sudo dnf install -y dnf-plugins-core
Updating Subscription Management repositories.
Red Hat Enterprise Linux 9 for x86_64 - AppStream (RPMs) 143 kB/s | 4.5 kB 00:00
Red Hat Enterprise Linux 9 for x86_64 - BaseOS (RPMs) 125 kB/s | 4.1 kB 00:00
Red Hat Enterprise Linux 9 for x86_64 - BaseOS - Extended Update Support (RPMs) 124 kB/s | 4.1 kB 00:00
Red Hat Enterprise Linux 9 for x86_64 - Supplementary (RPMs) 83 kB/s | 3.7 kB 00:00
Red Hat Enterprise Linux 9 for x86_64 - AppStream - Extended Update Support (RPMs) 143 kB/s | 4.5 kB 00:00
Red Hat Enterprise Linux 9 for x86_64 - Supplementary - Extended Update Support (RPMs) 119 kB/s | 3.7 kB 00:00
Package dnf-plugins-core-4.3.0-5.el9_2.noarch is already installed.
Dependencies resolved.
Nothing to do.
Complete!
$ sudo dnf config-manager --add-repo https://download.docker.com/linux/rhel/docker-ce.repo
Updating Subscription Management repositories.
Adding repo from: https://download.docker.com/linux/rhel/docker-ce.repo
$ sudo dnf install -y docker-ce docker-ce-cli containerd.io
Updating Subscription Management repositories.
Docker CE Stable - x86_64 100 kB/s | 37 kB 00:00
Red Hat Enterprise Linux 9 for x86_64 - AppStream (RPMs) 61 kB/s | 4.5 kB 00:00
Red Hat Enterprise Linux 9 for x86_64 - BaseOS (RPMs) 55 kB/s | 4.1 kB 00:00
Red Hat Enterprise Linux 9 for x86_64 - BaseOS - Extended Update Support (RPMs) 47 kB/s | 4.1 kB 00:00
Red Hat Enterprise Linux 9 for x86_64 - Supplementary (RPMs) 48 kB/s | 3.7 kB 00:00
Red Hat Enterprise Linux 9 for x86_64 - AppStream - Extended Update Support (RPMs) 62 kB/s | 4.5 kB 00:00
Red Hat Enterprise Linux 9 for x86_64 - Supplementary - Extended Update Support (RPMs) 65 kB/s | 3.7 kB 00:00
Dependencies resolved.
========================================================================================================================
Package Architecture Version Repository Size
========================================================================================================================
Installing:
containerd.io x86_64 1.7.27-3.1.el9 docker-ce-stable 44 M
docker-ce x86_64 3:28.1.1-1.el9 docker-ce-stable 20 M
docker-ce-cli x86_64 1:28.1.1-1.el9 docker-ce-stable 8.3 M
Installing dependencies:
container-selinux noarch 3:2.229.0-1.el9_2 rhel-9-for-x86_64-appstream-eus-rpms 55 k
fuse-common x86_64 3.10.2-5.el9 rhel-9-for-x86_64-baseos-rpms 9.3 k
fuse-overlayfs x86_64 1.11-1.el9_2 rhel-9-for-x86_64-appstream-rpms 74 k
fuse3 x86_64 3.10.2-5.el9 rhel-9-for-x86_64-appstream-rpms 58 k
fuse3-libs x86_64 3.10.2-5.el9 rhel-9-for-x86_64-appstream-rpms 94 k
iptables-nft x86_64 1.8.8-6.el9_1 rhel-9-for-x86_64-baseos-rpms 207 k
libnftnl x86_64 1.2.2-1.el9 rhel-9-for-x86_64-baseos-rpms 85 k
libslirp x86_64 4.4.0-7.el9 rhel-9-for-x86_64-appstream-rpms 72 k
slirp4netns x86_64 1.2.0-3.el9 rhel-9-for-x86_64-appstream-rpms 48 k
Installing weak dependencies:
docker-buildx-plugin x86_64 0.23.0-1.el9 docker-ce-stable 16 M
docker-ce-rootless-extras x86_64 28.1.1-1.el9 docker-ce-stable 3.2 M
docker-compose-plugin x86_64 2.35.1-1.el9 docker-ce-stable 15 M
Transaction Summary
========================================================================================================================
Install 15 Packages
Total download size: 107 M
Installed size: 427 M
Downloading Packages:
(1/15): docker-buildx-plugin-0.23.0-1.el9.x86_64.rpm 25 MB/s | 16 MB 00:00
(2/15): docker-ce-28.1.1-1.el9.x86_64.rpm 26 MB/s | 20 MB 00:00
(3/15): containerd.io-1.7.27-3.1.el9.x86_64.rpm 33 MB/s | 44 MB 00:01
(4/15): docker-ce-cli-28.1.1-1.el9.x86_64.rpm 10 MB/s | 8.3 MB 00:00
(5/15): docker-ce-rootless-extras-28.1.1-1.el9.x86_64.rpm 4.0 MB/s | 3.2 MB 00:00
(6/15): libslirp-4.4.0-7.el9.x86_64.rpm 639 kB/s | 72 kB 00:00
(7/15): fuse3-libs-3.10.2-5.el9.x86_64.rpm 507 kB/s | 94 kB 00:00
(8/15): docker-compose-plugin-2.35.1-1.el9.x86_64.rpm 27 MB/s | 15 MB 00:00
(9/15): fuse3-3.10.2-5.el9.x86_64.rpm 285 kB/s | 58 kB 00:00
(10/15): slirp4netns-1.2.0-3.el9.x86_64.rpm 239 kB/s | 48 kB 00:00
(11/15): iptables-nft-1.8.8-6.el9_1.x86_64.rpm 2.3 MB/s | 207 kB 00:00
(12/15): fuse-overlayfs-1.11-1.el9_2.x86_64.rpm 774 kB/s | 74 kB 00:00
(13/15): fuse-common-3.10.2-5.el9.x86_64.rpm 94 kB/s | 9.3 kB 00:00
(14/15): libnftnl-1.2.2-1.el9.x86_64.rpm 2.3 MB/s | 85 kB 00:00
(15/15): container-selinux-2.229.0-1.el9_2.noarch.rpm 1.6 MB/s | 55 kB 00:00
------------------------------------------------------------------------------------------------------------------------
Total 53 MB/s | 107 MB 00:02
Docker CE Stable - x86_64 11 kB/s | 1.6 kB 00:00
Importing GPG key 0x621E9F35:
Userid : "Docker Release (CE rpm) <docker@docker.com>"
Fingerprint: 060A 61C5 1B55 8A7F 742B 77AA C52F EB6B 621E 9F35
From : https://download.docker.com/linux/rhel/gpg
Key imported successfully
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Running scriptlet: container-selinux-3:2.229.0-1.el9_2.noarch 1/15
Installing : container-selinux-3:2.229.0-1.el9_2.noarch 1/15
Running scriptlet: container-selinux-3:2.229.0-1.el9_2.noarch 1/15
Installing : fuse3-libs-3.10.2-5.el9.x86_64 2/15
Installing : docker-buildx-plugin-0.23.0-1.el9.x86_64 3/15
Running scriptlet: docker-buildx-plugin-0.23.0-1.el9.x86_64 3/15
Installing : docker-compose-plugin-2.35.1-1.el9.x86_64 4/15
Running scriptlet: docker-compose-plugin-2.35.1-1.el9.x86_64 4/15
Installing : docker-ce-cli-1:28.1.1-1.el9.x86_64 5/15
Running scriptlet: docker-ce-cli-1:28.1.1-1.el9.x86_64 5/15
Installing : containerd.io-1.7.27-3.1.el9.x86_64 6/15
Running scriptlet: containerd.io-1.7.27-3.1.el9.x86_64 6/15
Installing : libnftnl-1.2.2-1.el9.x86_64 7/15
Installing : iptables-nft-1.8.8-6.el9_1.x86_64 8/15
Running scriptlet: iptables-nft-1.8.8-6.el9_1.x86_64 8/15
Installing : fuse-common-3.10.2-5.el9.x86_64 9/15
Installing : fuse3-3.10.2-5.el9.x86_64 10/15
Installing : fuse-overlayfs-1.11-1.el9_2.x86_64 11/15
Running scriptlet: fuse-overlayfs-1.11-1.el9_2.x86_64 11/15
Installing : libslirp-4.4.0-7.el9.x86_64 12/15
Installing : slirp4netns-1.2.0-3.el9.x86_64 13/15
Installing : docker-ce-rootless-extras-28.1.1-1.el9.x86_64 14/15
Running scriptlet: docker-ce-rootless-extras-28.1.1-1.el9.x86_64 14/15
Installing : docker-ce-3:28.1.1-1.el9.x86_64 15/15
Running scriptlet: docker-ce-3:28.1.1-1.el9.x86_64 15/15
Running scriptlet: container-selinux-3:2.229.0-1.el9_2.noarch 15/15
Running scriptlet: docker-ce-3:28.1.1-1.el9.x86_64 15/15
Verifying : containerd.io-1.7.27-3.1.el9.x86_64 1/15
Verifying : docker-buildx-plugin-0.23.0-1.el9.x86_64 2/15
Verifying : docker-ce-3:28.1.1-1.el9.x86_64 3/15
Verifying : docker-ce-cli-1:28.1.1-1.el9.x86_64 4/15
Verifying : docker-ce-rootless-extras-28.1.1-1.el9.x86_64 5/15
Verifying : docker-compose-plugin-2.35.1-1.el9.x86_64 6/15
Verifying : fuse3-libs-3.10.2-5.el9.x86_64 7/15
Verifying : libslirp-4.4.0-7.el9.x86_64 8/15
Verifying : fuse3-3.10.2-5.el9.x86_64 9/15
Verifying : slirp4netns-1.2.0-3.el9.x86_64 10/15
Verifying : fuse-overlayfs-1.11-1.el9_2.x86_64 11/15
Verifying : fuse-common-3.10.2-5.el9.x86_64 12/15
Verifying : iptables-nft-1.8.8-6.el9_1.x86_64 13/15
Verifying : libnftnl-1.2.2-1.el9.x86_64 14/15
Verifying : container-selinux-3:2.229.0-1.el9_2.noarch 15/15
Installed products updated.
Installed:
container-selinux-3:2.229.0-1.el9_2.noarch containerd.io-1.7.27-3.1.el9.x86_64
docker-buildx-plugin-0.23.0-1.el9.x86_64 docker-ce-3:28.1.1-1.el9.x86_64
docker-ce-cli-1:28.1.1-1.el9.x86_64 docker-ce-rootless-extras-28.1.1-1.el9.x86_64
docker-compose-plugin-2.35.1-1.el9.x86_64 fuse-common-3.10.2-5.el9.x86_64
fuse-overlayfs-1.11-1.el9_2.x86_64 fuse3-3.10.2-5.el9.x86_64
fuse3-libs-3.10.2-5.el9.x86_64 iptables-nft-1.8.8-6.el9_1.x86_64
libnftnl-1.2.2-1.el9.x86_64 libslirp-4.4.0-7.el9.x86_64
slirp4netns-1.2.0-3.el9.x86_64
Complete!
$ sudo dnf install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
Updating Subscription Management repositories.
Red Hat Enterprise Linux 9 for x86_64 - AppStream (RPMs) 144 kB/s | 4.5 kB 00:00
Red Hat Enterprise Linux 9 for x86_64 - BaseOS (RPMs) 118 kB/s | 4.1 kB 00:00
Red Hat Enterprise Linux 9 for x86_64 - BaseOS - Extended Update Support (RPMs) 117 kB/s | 4.1 kB 00:00
Red Hat Enterprise Linux 9 for x86_64 - Supplementary (RPMs) 122 kB/s | 3.7 kB 00:00
Red Hat Enterprise Linux 9 for x86_64 - AppStream - Extended Update Support (RPMs) 59 kB/s | 4.5 kB 00:00
Red Hat Enterprise Linux 9 for x86_64 - Supplementary - Extended Update Support (RPMs) 93 kB/s | 3.7 kB 00:00
Package docker-ce-3:28.1.1-1.el9.x86_64 is already installed.
Package docker-ce-cli-1:28.1.1-1.el9.x86_64 is already installed.
Package containerd.io-1.7.27-3.1.el9.x86_64 is already installed.
Package docker-buildx-plugin-0.23.0-1.el9.x86_64 is already installed.
Package docker-compose-plugin-2.35.1-1.el9.x86_64 is already installed.
Dependencies resolved.
Nothing to do.
Complete!
$ sudo systemctl enable --now docker
Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /usr/lib/systemd/system/docker.service.
Dockerインストールを確認します。
$ docker --version
Docker version 28.1.1, build 4eba377
3. PostgreSQLコンテナ起動(データベース作成含む)
Docker Hubの公式PostgreSQLイメージを取得します。
今回はバージョン指定をせずデフォルトのlatestバージョンを使用します。
※次に実施するdocker run
でイメージがなければ自動でpullされますが、明示的にpullすることでどのイメージを使っているか把握しやすくしています。
$ docker pull postgres
Using default tag: latest
latest: Pulling from library/postgres
dad67da3f26b: Pull complete
eb3a531023c8: Pull complete
05b641b3bdab: Pull complete
64e8f1b2b243: Pull complete
603ef9fcdd8e: Pull complete
8a1f652e0c97: Pull complete
c6def2c6e21d: Pull complete
b47a445a47f0: Pull complete
c95f49cc11b3: Pull complete
3664068a9b37: Pull complete
abfd68ef219e: Pull complete
928d00623a6e: Pull complete
db3ab53631e4: Pull complete
f4ce9941f6e3: Pull complete
Digest: sha256:6cf6142afacfa89fb28b894d6391c7dcbf6523c33178bdc33e782b3b533a9342
Status: Downloaded newer image for postgres:latest
docker.io/library/postgres:latest
Docker daemonへのアクセス権限でエラーが起きる場合は、sudo
で実行するか、以下のように現在のユーザーに権限を割り当てる必要があります。
$ docker pull postgres
Using default tag: latest
permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post "http://%2Fvar%2Frun%2Fdocker.sock/v1.49/images/create?fromImage=docker.io%2Flibrary%2Fpostgres&tag=latest": dial unix /var/run/docker.sock: connect: permission denied
$ sudo usermod -aG docker itzuser
取得したイメージを使ってコンテナを起動します。
ここでは、ユーザー名・パスワード・初期データベースの設定も環境変数で指定しており、起動と同時にデータベースの初期化まで自動で行われます。
$ docker run -d \
--name my-postgres \
-e POSTGRES_USER=testuser \
-e POSTGRES_PASSWORD=xxxxx \
-e POSTGRES_DB=testdb \
-p 5432:5432 \
postgres
20f46f966e06e686070602502c78b6d4116404c85e587a034b6a7001eb7a3b18
起動したPostgreSQLコンテナを確認します。
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
20f46f966e06 postgres "docker-entrypoint.s…" 2 minutes ago Up About a minute 0.0.0.0:5432->5432/tcp, [::]:5432->5432/tcp my-postgres
4. テーブル作成 & データ投入
コンテナに接続し、指定したユーザーとデータベースでのpsqlの起動(PostgreSQLへのログイン)を実施します。
$ docker exec -it my-postgres /bin/bash
root@20f46f966e06:/#psql -U testuser -d testdb
psql (17.5 (Debian 17.5-1.pgdg120+1))
Type "help" for help.
testdb=#
テーブル作成とデータ投入を行います。
今回は以下のような簡易的なテーブル「customers」を作成しました。
testdb=# CREATE TABLE customers (
customer_id VARCHAR(20) PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(100) UNIQUE NOT NULL,
membership_level VARCHAR(20),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE
testdb=# INSERT INTO customers (customer_id, name, email, membership_level)
VALUES
('C123456', '山田 太郎', 'taro@example.com', 'ゴールド'),
('C234567', '鈴木 花子', 'hanako@example.com', 'シルバー'),
('C345678', '佐藤 次郎', 'jiro@example.com', 'ブロンズ');
INSERT 0 3
作成したテーブルを確認します。
testdb=# SELECT * FROM customers;
customer_id | name | email | membership_level | created_at
-------------+-----------+--------------------+------------------+----------------------------
C123456 | 山田 太郎 | taro@example.com | ゴールド | 2025-06-13 11:13:44.124195
C234567 | 鈴木 花子 | hanako@example.com | シルバー | 2025-06-13 11:13:44.124195
C345678 | 佐藤 次郎 | jiro@example.com | ブロンズ | 2025-06-13 11:13:44.124195
(3 rows)
【応用】Docker Composeへの切り替え
複数コンテナの管理・構築に役立つDocker Composeによるコンテナ再構築も試してみます。
具体的には、起動中のコンテナを停止後に削除し、改めてDocker Composeでコンテナを起動してみます。
なお、docker run
の際にデータ永続化のオプション(-v
)を入れていないため、コンテナを削除すると上記で投入したデータも削除されます。
コンテナの停止
$ docker stop my-postgres
my-postgres
コンテナの削除
$ docker rm my-postgres
my-postgres
削除されたことを確認します。
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
compose.yamlを作成
今度はPostgreSQLのデータ保存ディレクトリをホスト側のボリュームにマウントして、データを永続化する設定も入れています。
services:
db:
image: postgres:latest
container_name: my-postgres2
ports:
- "5432:5432"
environment:
POSTGRES_USER: testuser2
POSTGRES_PASSWORD: xxxxx
POSTGRES_DB: testdb2
volumes:
- postgres-data:/var/lib/postgresql/data
volumes:
postgres-data:
name: postgres-data
Docker Composeでコンテナ起動
$ docker compose up -d
[+] Running 15/15
✔ db Pulled 6.6s
✔ dad67da3f26b Already exists 0.0s
✔ 3cb5663a2cfc Pull complete 0.2s
✔ 5645870ddfdf Pull complete 0.4s
✔ 2b394d233eb5 Pull complete 0.5s
✔ 90cc0ca7a799 Pull complete 0.9s
✔ 8f3f4fa66428 Pull complete 1.0s
✔ 2031aa8ef9b1 Pull complete 1.0s
✔ f427288209f2 Pull complete 1.0s
✔ 97d4026b1ac7 Pull complete 5.8s
✔ 113b71174c6e Pull complete 5.8s
✔ c5bc88ab0d97 Pull complete 5.8s
✔ bfadd549156d Pull complete 5.8s
✔ 2534a56ca63b Pull complete 5.8s
✔ 9cdc3fa4c969 Pull complete 5.8s
[+] Running 3/3
✔ Network work_default Created 0.1s
✔ Volume "postgres-data" Created 0.0s
✔ Container my-postgres2 Started
起動したコンテナを確認します。
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3345cf0a4a5e postgres:latest "docker-entrypoint.s…" 56 seconds ago Up 56 seconds 0.0.0.0:5432->5432/tcp, [::]:5432->5432/tcp my-postgres2
【補足】データ永続化の確認
今回はデータを永続化する設定を入れていたため、同じようにデータを投入してコンテナを削除してもデータは残りました。
再度データ投入
$ docker exec -it my-postgres2 psql -U testuser2 -d testdb2
psql (17.5 (Debian 17.5-1.pgdg120+1))
Type "help" for help.
testdb2=# CREATE TABLE customers (
customer_id VARCHAR(20) PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(100) UNIQUE NOT NULL,
membership_level VARCHAR(20),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE
testdb2=# INSERT INTO customers (customer_id, name, email, membership_level)
VALUES
('C123456', '山田 太郎', 'taro@example.com', 'ゴールド'),
('C234567', '鈴木 花子', 'hanako@example.com', 'シルバー'),
('C345678', '佐藤 次郎', 'jiro@example.com', 'ブロンズ');
INSERT 0 3
testdb2=# SELECT * FROM customers;
customer_id | name | email | membership_level | created_at
-------------+-----------+--------------------+------------------+----------------------------
C123456 | 山田 太郎 | taro@example.com | ゴールド | 2025-06-13 13:52:16.363107
C234567 | 鈴木 花子 | hanako@example.com | シルバー | 2025-06-13 13:52:16.363107
C345678 | 佐藤 次郎 | jiro@example.com | ブロンズ | 2025-06-13 13:52:16.363107
(3 rows)
testdb2=#
コンテナの停止・削除
$ docker compose down
[+] Running 2/2
✔ Container my-postgres2 Removed 0.2s
✔ Network work_default Removed
コンテナ再構築
コンテナ名を変更して再構築しました。
$ docker compose up -d
[+] Running 2/2
✔ Network work_default Created 0.1s
✔ Container my-postgres3 Started
データ確認
上記で投入したデータを確認することができました。
$ docker exec -it my-postgres3 psql -U testuser2 -d testdb2
psql (17.5 (Debian 17.5-1.pgdg120+1))
Type "help" for help.
testdb2=# \dt
List of relations
Schema | Name | Type | Owner
--------+-----------+-------+-----------
public | customers | table | testuser2
(1 row)
testdb2=# SELECT * FROM customers;
customer_id | name | email | membership_level | created_at
-------------+-----------+--------------------+------------------+----------------------------
C123456 | 山田 太郎 | taro@example.com | ゴールド | 2025-06-13 13:52:16.363107
C234567 | 鈴木 花子 | hanako@example.com | シルバー | 2025-06-13 13:52:16.363107
C345678 | 佐藤 次郎 | jiro@example.com | ブロンズ | 2025-06-13 13:52:16.363107
(3 rows)