(O+P)ut

アウトプット



(O+P)ut

エンジニアのアウトプット

【Podman/RHEL】generateを用いてコンテナを自動起動化する方法

スポンサーリンク

はじめに

Dockerで行った以下の作業はPodmanだとより簡単に可能です。

本記事ではRHELにて特定コンテナを自動起動するための手順をメモしました。尚、ユーザはrootで実行しています。

環境情報
  • podman version 1.6.4
  • Red Hat Enterprise Linux Server 7.9

コンテナ作成

事前準備として今回はNginxコンテナを作成しました。

# podman run --name testweb -d -p 8080:80 nginx
...
# podman ps
CONTAINER ID  IMAGE                           COMMAND               CREATED         STATUS             PORTS                 NAMES
671822eb1d4e  docker.io/library/nginx:latest  nginx -g daemon o...  24 seconds ago  Up 24 seconds ago  0.0.0.0:8080->80/tcp  testweb

ローカルホストに対してcurlを打つことで疎通確認も可能です。

$ curl localhost:8080
<!DOCTYPE html>
...
</html>

systemd用ファイルを作成する

以下コマンドでコンテナ名を指定すると

# podman generate systemd --name testweb --files
/root/container-testweb.service

カレントディレクトリにファイルが生成されます。

同ファイルを/etc/systemd/system/下に配置すればsystemctl start/stopで起動可能になります。

# cat /etc/systemd/system/container-testweb.service
...
[Service]
Restart=on-failure
ExecStart=/usr/bin/podman start testweb
ExecStop=/usr/bin/podman stop -t 10 testweb
...

あとは自動起動の設定を入れると

# systemctl enable container-testweb.service
Created symlink from /etc/systemd/system/multi-user.target.wants/container-testweb.service to /etc/systemd/system/container-testweb.service.

リブート後も

# systemctl reboot

コンテナがサーバ上で自動起動しています。

# podman ps
CONTAINER ID  IMAGE                           COMMAND               CREATED      STATUS             PORTS                 NAMES
671822eb1d4e  docker.io/library/nginx:latest  nginx -g daemon o...  4 hours ago  Up 24 seconds ago  0.0.0.0:8080->80/tcp  testweb

終わりに

Podmanにはdockerコマンドにはないgenerateが存在するので

# podman generate --help
Generate structured data based for a containers and pods
Available Commands:
  kube        Generate Kubernetes pod YAML from a container or pod
  systemd     Generate a systemd unit file for a Podman container

systemdのユニットファイルを自動で生成できます。ただしファイルに記載の通りでstart/stopの処理なので、run/rmではない点は覚えておきたいです。

以上です。