Ubuntu 25.04
Sponsored Link

Podman : コンテナーの自動起動の設定2025/04/22

 

Systemd ユニットファイルを生成して、システム起動時にコンテナーが自動起動できるよう設定します。

[1] Quadlet を使用したコンテナーサービスの設定です。
root@dlp:~#
podman images

REPOSITORY                  TAG               IMAGE ID      CREATED            SIZE
localhost/podman-pause      5.4.1-1742477809  1b9916037b93  20 minutes ago     793 kB
docker.io/library/root-web  latest            c65d1e928abe  26 minutes ago     137 MB
srv.world/iproute           latest            e9449427b6fe  33 minutes ago     135 MB
srv.world/ubuntu-nginx      latest            1fbf2477f02c  59 minutes ago     137 MB
srv.world/ubuntu-apache2    latest            b7f1351d1b3c  About an hour ago  246 MB
docker.io/library/ubuntu    latest            602eb6fb314b  13 days ago        80.6 MB
docker.io/moby/buildkit     buildx-stable-1   3c2d38015344  4 weeks ago        209 MB
docker.io/library/mariadb   latest            9f3d79eba61e  2 months ago       334 MB

# ファイル名 ⇒ (任意の名称).container

root@dlp:~#
vi /etc/containers/systemd/ubuntu-nginx.container
[Unit]
Description=Nginx container
After=local-fs.target

[Container]
# 任意の名称
ContainerName=ubuntu-nginx
# 使用するコンテナーイメージ
Image=srv.world/ubuntu-nginx
# ポート
PublishPort=80:80

[Service]
Restart=always

[Install]
WantedBy=multi-user.target default.target

root@dlp:~#
systemctl daemon-reload

root@dlp:~#
systemctl start ubuntu-nginx.service
[2] Quadlet を使用した Pod サービスの設定です。
root@dlp:~#
podman images

REPOSITORY                  TAG               IMAGE ID      CREATED            SIZE
localhost/podman-pause      5.4.1-1742477809  1b9916037b93  22 minutes ago     793 kB
docker.io/library/root-web  latest            c65d1e928abe  27 minutes ago     137 MB
srv.world/iproute           latest            e9449427b6fe  34 minutes ago     135 MB
srv.world/ubuntu-nginx      latest            1fbf2477f02c  About an hour ago  137 MB
srv.world/ubuntu-apache2    latest            b7f1351d1b3c  About an hour ago  246 MB
docker.io/library/ubuntu    latest            602eb6fb314b  13 days ago        80.6 MB
docker.io/moby/buildkit     buildx-stable-1   3c2d38015344  4 weeks ago        209 MB
docker.io/library/mariadb   latest            9f3d79eba61e  2 months ago       334 MB

# Pod 設定ファイル作成
# 書式は Kubernetes と同じ

root@dlp:~#
vi /etc/containers/systemd/nginx-pod.yml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-pod
  labels:
    name: nginx-pod
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx-pod
  template:
    metadata:
      labels:
        app: nginx-pod
    spec:
      containers:
      - name: nginx-pod
        image: ubuntu-nginx
        ports:
          - name: web
            containerPort: 80

# ファイル名 ⇒ (任意の名称).kube

root@dlp:~#
vi /etc/containers/systemd/nginx-pod.kube
[Unit]
Description=Web service pod
After=local-fs.target

[Kube]
Yaml=/etc/containers/systemd/nginx-pod.yml
PublishPort=80:80

[Service]
Restart=always

[Install]
WantedBy=multi-user.target default.target

root@dlp:~#
systemctl daemon-reload

root@dlp:~#
systemctl start nginx-pod.service
root@dlp:~#
podman pod ls

POD ID        NAME           STATUS      CREATED        INFRA ID      # OF CONTAINERS
c94e9b88908d  nginx-pod-pod  Running     3 seconds ago  fab8e391a124  2
関連コンテンツ