podman-composeでシステム起動時にコンテナを自動起動する

podman-composeでシステム起動時にコンテナを自動起動する

Tags
Server
Container
Linux
Published
Author
DDlia
Description
FedoraやRed Hat Enterprise Linux (または AlmaLinux や Rocky Linux などの RHEL クローン) でコンテナを扱う場合、Podmanが便利です。Podmanはdnfコマンド1発でインストールできます。

環境

本記事ではRHELクローンであるAlmaLinux 9を用います。
$ fastfetch 'c:. ddlia@test lkkkx, .. .. ,cc, ---------------- okkkk:ckkx' .lxkkx.okkkkd OS: AlmaLinux 9.5 (Teal Serval) x86_64 .:llcokkx' :kkkxkko:xkkd, Host: OptiPlex 3020 (00) .xkkkkdood: ;kx, .lkxlll; Kernel: Linux 6.8.12-7-pve xkkx. xk' xkkkkk: Uptime: 8 mins 'xkx. xd .....,. Packages: 515 (rpm) .. :xkl' :c ..''.. Shell: bash 5.1.8 .dkx' .:ldl:'. ' ':lollldkkxo; Cursor: Adwaita .''lkkko' ckkkx. Terminal: /dev/pts/3 'xkkkd:kkd. .. ;' :kkxo. CPU: Intel(R) Core(TM) i5-4570 (4) @ 3.60 GHz ,xkkkd;kk' ,d; ld. ':dkd::cc, GPU: Intel Xeon E3-1200 v3/4th Gen Core Processor Integrated Graphics Controller @ 1.15 GHz [Integrated] .,,.;xkko'.';lxo. dx, :kkk'xkkkkc Memory: 1.26 GiB / 4.00 GiB (32%) 'dkkkkkxo:. ;kx .kkk:;xkkd. Swap: 0 B / 2.00 GiB (0%) ..... .;dk:. lkk. :;, Disk (/): 4.17 GiB / 7.78 GiB (54%) - ext4 :kkkkkkkdoxkkx Local IP (eth0): 10.1.70.1/16 ,c,,;;;:xkkd. Locale: en_US.utf8 ;kkkkl... ;kkkkl ,od;
podman及びpodman-composeのバージョンは以下の通りです。
podman-compose version 1.0.6 podman version 5.2.2

Podmanのインストール

$ sudo dnf install podman
 
podman-compose やdockerのエイリアスを提供する podman-docker などをインストールするには以下を実行します。なお、これらのインストールには EPEL (Extra Packages for Enterprise Linux) のインストールが必要です。
 
EPELの有効化
$ sudo dnf install epel-release
RHEL 9では以下のように実行します。
$ subscription-manager repos --enable codeready-builder-for-rhel-9-$(arch)-rpms $ dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm
 
podman-compose podman-plugins podman-docker のインストール
$ sudo dnf install podman-compose podman-plugins podman-docker
 

lingerの有効化

システムの起動時にサービスを起動し、ログアウト後もそのまま起動した状態を保つようにします。
$ sudo loginctl enable-linger <username>
 

systemd ユニットファイルの生成

systemdテンプレートを生成します。rootで実行します。
$ sudo podman-compose systemd -a create-unit
通常ユーザーで実行した場合は、出力されたsystemdユニットファイルを書き込みます。
$ sudo vi /etc/systemd/user/podman-compose\@.service
 
podman-compose version 1.0.6 では、テンプレートにエラーがあるため修正します。
$ sudo vi /etc/systemd/user/podman-compose\@.service
ExecStartPre=-/usr/bin/podman-compose up --no-startExecStartPre=-/usr/bin/podman-compose --in-pod pod_%i up --no-start に、また ExecStopPost=/usr/bin/podman pod rm pod_%i を追記します。
 
修正後は以下のようになるはずです。
$ sudo cat /etc/systemd/user/podman-compose\@.service # /etc/systemd/user/[email protected] [Unit] Description=%i rootless pod (podman-compose) [Service] Type=simple EnvironmentFile=%h/.config/containers/compose/projects/%i.env ExecStartPre=-/usr/bin/podman-compose --in-pod pod_%i up --no-start ExecStartPre=/usr/bin/podman pod start pod_%i ExecStart=/usr/bin/podman-compose wait ExecStop=/usr/bin/podman pod stop pod_%i ExecStopPost=/usr/bin/podman pod rm pod_%i [Install] WantedBy=default.target
 
コンテナをsystemdサービスとして登録します。
$ podman-compose systemd -a register

自動起動の有効化

systemctl enable コマンドで自動起動するように設定します。
$ systemctl --user enable --now 'podman-compose@<project-name>'
 
systemctl status コマンドで確認します。正常に動作していれば、以下のような出力が得られるはずです。
$ systemctl --user status 'podman-compose@<project-name>'
$ systemctl --user status podman-compose@koe ● [email protected] - koe rootless pod (podman-compose) Loaded: loaded (/etc/xdg/systemd/user/[email protected]; enabled; preset: disabled) Active: active (running) since Sun 2025-01-26 18:00:20 UTC; 3min 0s ago Process: 192 ExecStartPre=/usr/bin/podman-compose --in-pod pod_koe up --no-start (code=exited, status=0/SUCCESS) Process: 321 ExecStartPre=/usr/bin/podman pod start pod_koe (code=exited, status=0/SUCCESS) Main PID: 539 (podman) Tasks: 12 (limit: 101429) Memory: 84.9M CPU: 1.616s CGroup: /user.slice/user-0.slice/[email protected]/app.slice/app-podman\x2dcompose.slice/[email protected] ├─400 /usr/bin/conmon --api-version 1 -c 5f9680fc404348739b628bbd49d171ff9ed0266fe066b2d6cbdb048b3b5dde64 -u 5f9680fc404348739b628bbd49d171ff9ed0266fe066b2d6cbdb048b3b5dde64 -r /usr/bin/crun -b /var/lib/containe rs/storage/overlay-containers/5f9680fc404348739b628bbd49d171ff9ed0266fe066b2d6cbdb048b3b5dde64/userdata -p /run/containers/storage/overlay-containers/5f9680fc404348739b628bbd49d171ff9ed0266fe066b2d6cbdb048b3b5dde64/userdata/pidfile -n k oe_redis_1 --exit-dir /run/libpod/exits --persist-dir /run/libpod/persist/5f9680fc404348739b628bbd49d171ff9ed0266fe066b2d6cbdb048b3b5dde64 --full-attach -s -l journald --log-level warning --syslog --runtime-arg --log-format=json --runti me-arg --log --runtime-arg=/run/containers/storage/overlay-containers/5f9680fc404348739b628bbd49d171ff9ed0266fe066b2d6cbdb048b3b5dde64/userdata/oci-log --conmon-pidfile /run/containers/storage/overlay-containers/5f9680fc404348739b628bbd 49d171ff9ed0266fe066b2d6cbdb048b3b5dde64/userdata/conmon.pid --exit-command /usr/bin/podman --exit-command-arg --root --exit-command-arg /var/lib/containers/storage --exit-command-arg --runroot --exit-command-arg /run/containers/storage --exit-command-arg --log-level --exit-command-arg warning --exit-command-arg --cgroup-manager --exit-command-arg systemd --exit-command-arg --tmpdir --exit-command-arg /run/libpod --exit-command-arg --network-config-dir --exit-command- arg "" --exit-command-arg --network-backend --exit-command-arg netavark --exit-command-arg --volumepath --exit-command-arg /var/lib/containers/storage/volumes --exit-command-arg --db-backend --exit-command-arg sqlite --exit-command-arg --transient-store=false --exit-command-arg --runtime --exit-command-arg crun --exit-command-arg --storage-driver --exit-command-arg overlay --exit-command-arg --storage-opt --exit-command-arg overlay.mountopt=nodev,metacopy=on --exit-co mmand-arg --events-backend --exit-command-arg journald --exit-command-arg container --exit-command-arg cleanup --exit-command-arg 5f9680fc404348739b628bbd49d171ff9ed0266fe066b2d6cbdb048b3b5dde64 ├─470 /usr/bin/conmon --api-version 1 -c e415eb619ebda1f932c994a436b6a4c95d92bb093ee0926520a4b96e5bb80b27 -u e415eb619ebda1f932c994a436b6a4c95d92bb093ee0926520a4b96e5bb80b27 -r /usr/bin/crun -b /var/lib/containe rs/storage/overlay-containers/e415eb619ebda1f932c994a436b6a4c95d92bb093ee0926520a4b96e5bb80b27/userdata -p /run/containers/storage/overlay-containers/e415eb619ebda1f932c994a436b6a4c95d92bb093ee0926520a4b96e5bb80b27/userdata/pidfile -n k oe_voicevox_1 --exit-dir /run/libpod/exits --persist-dir /run/libpod/persist/e415eb619ebda1f932c994a436b6a4c95d92bb093ee0926520a4b96e5bb80b27 --full-attach -s -l journald --log-level warning --syslog --runtime-arg --log-format=json --ru ntime-arg --log --runtime-arg=/run/containers/storage/overlay-containers/e415eb619ebda1f932c994a436b6a4c95d92bb093ee0926520a4b96e5bb80b27/userdata/oci-log --conmon-pidfile /run/containers/storage/overlay-containers/e415eb619ebda1f932c99 4a436b6a4c95d92bb093ee0926520a4b96e5bb80b27/userdata/conmon.pid --exit-command /usr/bin/podman --exit-command-arg --root --exit-command-arg /var/lib/containers/storage --exit-command-arg --runroot --exit-command-arg /run/containers/stor age --exit-command-arg --log-level --exit-command-arg warning --exit-command-arg --cgroup-manager --exit-command-arg systemd --exit-command-arg --tmpdir --exit-command-arg /run/libpod --exit-command-arg --network-config-dir --exit-comma nd-arg "" --exit-command-arg --network-backend --exit-command-arg netavark --exit-command-arg --volumepath --exit-command-arg /var/lib/containers/storage/volumes --exit-command-arg --db-backend --exit-command-arg sqlite --exit-command-a rg --transient-store=false --exit-command-arg --runtime --exit-command-arg crun --exit-command-arg --storage-driver --exit-command-arg overlay --exit-command-arg --storage-opt --exit-command-arg overlay.mountopt=nodev,metacopy=on --exit -command-arg --events-backend --exit-command-arg journald --exit-command-arg container --exit-command-arg cleanup --exit-command-arg e415eb619ebda1f932c994a436b6a4c95d92bb093ee0926520a4b96e5bb80b27 ├─534 /usr/bin/conmon --api-version 1 -c 954c925d2069a4af482f9c0a31974bee3956bf5856385c04e4976f8833bec9f7 -u 954c925d2069a4af482f9c0a31974bee3956bf5856385c04e4976f8833bec9f7 -r /usr/bin/crun -b /var/lib/containe rs/storage/overlay-containers/954c925d2069a4af482f9c0a31974bee3956bf5856385c04e4976f8833bec9f7/userdata -p /run/containers/storage/overlay-containers/954c925d2069a4af482f9c0a31974bee3956bf5856385c04e4976f8833bec9f7/userdata/pidfile -n k oe_app_1 --exit-dir /run/libpod/exits --persist-dir /run/libpod/persist/954c925d2069a4af482f9c0a31974bee3956bf5856385c04e4976f8833bec9f7 --full-attach -s -l journald --log-level warning --syslog --runtime-arg --log-format=json --runtime -arg --log --runtime-arg=/run/containers/storage/overlay-containers/954c925d2069a4af482f9c0a31974bee3956bf5856385c04e4976f8833bec9f7/userdata/oci-log --conmon-pidfile /run/containers/storage/overlay-containers/954c925d2069a4af482f9c0a31 974bee3956bf5856385c04e4976f8833bec9f7/userdata/conmon.pid --exit-command /usr/bin/podman --exit-command-arg --root --exit-command-arg /var/lib/containers/storage --exit-command-arg --runroot --exit-command-arg /run/containers/storage - -exit-command-arg --log-level --exit-command-arg warning --exit-command-arg --cgroup-manager --exit-command-arg systemd --exit-command-arg --tmpdir --exit-command-arg /run/libpod --exit-command-arg --network-config-dir --exit-command-ar g "" --exit-command-arg --network-backend --exit-command-arg netavark --exit-command-arg --volumepath --exit-command-arg /var/lib/containers/storage/volumes --exit-command-arg --db-backend --exit-command-arg sqlite --exit-command-arg -- transient-store=false --exit-command-arg --runtime --exit-command-arg crun --exit-command-arg --storage-driver --exit-command-arg overlay --exit-command-arg --storage-opt --exit-command-arg overlay.mountopt=nodev,metacopy=on --exit-comm and-arg --events-backend --exit-command-arg journald --exit-command-arg container --exit-command-arg cleanup --exit-command-arg 954c925d2069a4af482f9c0a31974bee3956bf5856385c04e4976f8833bec9f7 └─539 podman wait -- koe_redis_1 koe_voicevox_1 koe_app_1 Jan 26 18:02:16 discord-tts koe_voicevox_1[470]: INFO: 127.0.0.1:43778 - "GET /version HTTP/1.1" 200 OK Jan 26 18:02:27 discord-tts koe_voicevox_1[470]: INFO: 127.0.0.1:50142 - "GET /version HTTP/1.1" 200 OK Jan 26 18:02:38 discord-tts koe_voicevox_1[470]: INFO: 127.0.0.1:46478 - "GET /version HTTP/1.1" 200 OK Jan 26 18:02:49 discord-tts koe_voicevox_1[470]: INFO: 127.0.0.1:49596 - "GET /version HTTP/1.1" 200 OK Jan 26 18:02:54 discord-tts koe_voicevox_1[470]: INFO: 10.89.0.4:53352 - "GET /presets HTTP/1.1" 200 OK Jan 26 18:02:54 discord-tts koe_voicevox_1[470]: INFO: 10.89.0.4:53352 - "GET /presets HTTP/1.1" 200 OK
トップへ戻る