1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

RHEL8でコンテナの自動起動を設定する

Posted at

はじめに

AWSのRHEL 8イメージでPodmanを使ったときに「VMを立ち上げたら一緒にコンテナが立ち上がる」を実現したかったのですが、少し調べる必要があったのでここに残しておきます。

環境

Red Hat Enterprise Linux 8.9
Podman 4.6.1

ユーザ名がec2-userの場合の例です。

コンテナの自動起動を設定する

任意のコンテナを起動します。ここではhelloという名前で起動しています。

$ podman run -it -d --name hello ubuntu bash

コンテナ名を指定して、ユニットファイルをhello-container.serviceという名前で作成しています。名前は任意です。このときコンテナのStatusはRunningでもStoppedでもOKです。

$ podman generate systemd --name hello > ~/.config/systemd/user/hello-container.service

ユーザのユニットファイルをsystemdに反映し、サービスに登録します。

$ systemctl --user daemon-reload

サービスを有効化し登録します。

$ systemctl --user enable hello-container.service
Created symlink /home/ec2-user/.config/systemd/user/default.target.wants/hello-container.service → /home/ec2-user/.config/systemd/user/hello-container.service.

サービス追加されたことを確認します。

$ systemctl --user list-dependencies
default.target
● hello-container.service
● └─basic.target
●   ├─paths.target
●   ├─sockets.target
●   │ └─dbus.socket
●   └─timers.target
●     └─grub-boot-success.timer

以上です。これで次回から起動時に指定のコンテナが起動します。

ユーザのセッションが切れてもコンテナを起動したままにする

設定によってはユーザのセッションが切れたときにユーザプロセスが落ちるようになっていることがあります。
そんな時は以下のコマンド。

$ loginctl enable-linger $UID
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?