hana_shinのLinux技術ブログ

Linuxの技術情報を掲載しています。特にネットワークをメインに掲載していきます。

podmanコマンドの使い方



1 podmanコマンドとは?

・podmanはRedHat社が開発したコンテナ管理ツール
RHEL 8 でDocker コンテナエンジン、docker コマンドを削除
RHEL 8 はpodmanコマンドを使用
・podmanはサービス(docker.service)の起動が不要

2 検証環境

2.1 ネットワーク構成

サーバとクライアントの2台構成です。図中のeth0はNICの名前です。

                           192.168.122.0/24
client(eth0) ------------------------------------------- (eth0) server
           .177                                         .68

2.2 版数

サーバ、クライアントのAlmaLinux版数は以下のとおりです。

[root@server ~]# cat /etc/redhat-release
AlmaLinux release 8.6 (Sky Tiger)

カーネル版数は以下のとおりです。

[root@server ~]# uname -r
4.18.0-372.9.1.el8.x86_64

3 インストール方法

podmanパッケージをインストールします。

[root@server ~]# dnf -y install podman

podmanの版数を確認します。

[root@server ~]# podman -v
podman version 4.0.2

4 オプション一覧

オプションは以下のとおりです。

[root@server ~]# podman --help
Manage pods, containers and images

Usage:
  podman [options] [command]

Available Commands:
  attach      Attach to a running container
  auto-update Auto update containers according to their auto-update policy
  build       Build an image using instructions from Containerfiles
  commit      Create new image based on the changed container
  container   Manage containers
  cp          Copy files/folders between a container and the local filesystem
  create      Create but do not start a container
  diff        Display the changes to the object's file system
  events      Show podman events
  exec        Run a process in a running container
  export      Export container's filesystem contents as a tar archive
  generate    Generate structured data based on containers, pods or volumes
  healthcheck Manage health checks on containers
  help        Help about any command
  history     Show history of a specified image
  image       Manage images
  images      List images in local storage
  import      Import a tarball to create a filesystem image
  info        Display podman system information
  init        Initialize one or more containers
  inspect     Display the configuration of object denoted by ID
  kill        Kill one or more running containers with a specific signal
  load        Load image(s) from a tar archive
  login       Login to a container registry
  logout      Logout of a container registry
  logs        Fetch the logs of one or more containers
  machine     Manage a virtual machine
  manifest    Manipulate manifest lists and image indexes
  mount       Mount a working container's root filesystem
  network     Manage networks
  pause       Pause all the processes in one or more containers
  play        Play containers, pods or volumes from a structured file
  pod         Manage pods
  port        List port mappings or a specific mapping for the container
  ps          List containers
  pull        Pull an image from a registry
  push        Push an image to a specified destination
  rename      Rename an existing container
  restart     Restart one or more containers
  rm          Remove one or more containers
  rmi         Removes one or more images from local storage
  run         Run a command in a new container
  save        Save image(s) to an archive
  search      Search registry for image
  secret      Manage secrets
  start       Start one or more containers
  stats       Display a live stream of container resource usage statistics
  stop        Stop one or more containers
  system      Manage podman
  tag         Add an additional name to a local image
  top         Display the running processes of a container
  unmount     Unmounts working container's root filesystem
  unpause     Unpause the processes in one or more containers
  unshare     Run a command in a modified user namespace
  untag       Remove a name from a local image
  version     Display the Podman version information
  volume      Manage volumes
  wait        Block on one or more containers

Options:
      --cgroup-manager string       Cgroup manager to use ("cgroupfs"|"systemd") (default "systemd")
      --conmon string               Path of the conmon binary
  -c, --connection string           Connection to use for remote Podman service
      --events-backend string       Events backend to use ("file"|"journald"|"none") (default "file")
      --help                        Help for podman
      --hooks-dir strings           Set the OCI hooks directory path (may be set multiple times) (default [/usr/share/containers/oci/hooks.d])
      --identity string             path to SSH identity file, (CONTAINER_SSHKEY)
      --log-level string            Log messages above specified level (trace, debug, info, warn, warning, error, fatal, panic) (default "warn")
      --namespace string            Set the libpod namespace, used to create separate views of the containers and pods on the system
      --network-cmd-path string     Path to the command for configuring the network
      --network-config-dir string   Path of the configuration directory for networks
      --noout                       do not output to stdout
  -r, --remote                      Access remote Podman service
      --root string                 Path to the root directory in which data, including images, is stored
      --runroot string              Path to the 'run directory' where all state information is stored
      --runtime string              Path to the OCI-compatible binary used to run containers. (default "runc")
      --runtime-flag stringArray    add global flags for the container runtime
      --storage-driver string       Select which storage driver is used to manage storage of images and containers
      --storage-opt stringArray     Used to pass an option to the storage driver
      --syslog                      Output logging information to syslog as well as the console (default false)
      --tmpdir string               Path to the tmp directory for libpod state content.

                                    Note: use the environment variable 'TMPDIR' to change the temporary storage location for container images, '/var/tmp'.

      --url string                  URL to access Podman service (CONTAINER_HOST) (default "unix:/run/podman/podman.sock")
  -v, --version                     version for podman

5 イメージを取得/削除する方法(pull/rmi)

5.1 AlmaLinuxのイメージ取得(最新版)

下記ページを参考にAlmaLinuxのイメージを取得してみます。
https://hub.docker.com/_/almalinux

まず、TAGにlatestを指定して、AlmaLinuxの最新版イメージを取得してみます。

[root@server ~]# podman pull almalinux:latest

イメージ一覧を確認します。

[root@server ~]# podman images
REPOSITORY                   TAG         IMAGE ID      CREATED      SIZE
docker.io/library/almalinux  latest      4580d9e4bab7  2 weeks ago  195 MB

確認したイメージIDを指定して、イメージを削除します。

[root@server ~]# podman rmi 4580d9e4bab7
Untagged: docker.io/library/almalinux:latest
Deleted: 4580d9e4bab7a568a73d120b6a7d189a0a12ba9311cd71506c5dbedec94ceb9b

イメージ一覧を確認します。イメージが削除されたことがわかります。

[root@server ~]# podman images
REPOSITORY  TAG         IMAGE ID    CREATED     SIZE

5.2 AlmaLinuxのイメージ取得(版数指定)

TAGに8.6を指定して、AlmaLinuxの8.6版のイメージを取得してみます。

[root@server ~]# podman pull almalinux:8.6

イメージ一覧を確認します。

[root@server ~]# podman images
REPOSITORY                   TAG         IMAGE ID      CREATED      SIZE
docker.io/library/almalinux  8.6         4580d9e4bab7  2 weeks ago  195 MB

確認したイメージIDを指定してイメージを削除します。

[root@server ~]# podman rmi 2e19f62a21cc

イメージ一覧を確認します。イメージが削除されたことがわかります。

[root@server ~]# podman images
REPOSITORY  TAG         IMAGE ID    CREATED     SIZE

5.3 SUSE のイメージ取得(最新版)

下記ページを参考に、SLES154SP4のイメージを取得してみます。
https://registry.suse.com/static/bci/bci-base-15sp4/index.html

[root@server ~]# podman pull registry.suse.com/bci/bci-base:latest

イメージ一覧を確認します。

[root@server ~]# podman images
REPOSITORY                      TAG         IMAGE ID      CREATED     SIZE
registry.suse.com/bci/bci-base  latest      86698fe00598  8 days ago  122 MB

確認したイメージIDを指定してイメージを削除します。

[root@server ~]# podman rmi 86698fe00598

イメージ一覧を確認します。SUSEのイメージが削除されたことがわかります。

[root@server ~]# podman images
REPOSITORY  TAG         IMAGE ID    CREATED     SIZE

5.4 SUSE のイメージ取得(版数指定)

下記ページを参考に、SLES15SP3のイメージを取得してみます。
https://registry.suse.com/static/bci/bci-base/index.html

[root@server ~]# podman pull registry.suse.com/bci/bci-base:15.3

イメージ一覧を確認します。

[root@server ~]# podman images
REPOSITORY                      TAG         IMAGE ID      CREATED     SIZE
registry.suse.com/bci/bci-base  15.3        c675c295c6ba  2 days ago  122 MB

確認したイメージIDを指定してイメージを削除します。

[root@server ~]# podman rmi c675c295c6ba

イメージ一覧を確認します。イメージが削除されたことがわかります。

[root@server ~]# podman images
REPOSITORY  TAG         IMAGE ID    CREATED     SIZE

5.5 Ubuntuのイメージ取得(最新版)

下記ページを参考に、Ubuntuの最新版イメージを取得してみます。
https://hub.docker.com/_/ubuntu

[root@server ~]# podman pull ubuntu:latest

イメージ一覧を確認します。

[root@server ~]# podman images
REPOSITORY                TAG         IMAGE ID      CREATED      SIZE
docker.io/library/ubuntu  latest      27941809078c  6 weeks ago  80.3 MB

確認したイメージIDを指定して、イメージを削除します。

[root@server ~]# podman rmi 27941809078c

イメージ一覧を確認します。イメージが削除されたことがわかります。

[root@server ~]# podman images
REPOSITORY  TAG         IMAGE ID    CREATED     SIZE

5.6 Ubuntuのイメージ取得(版数指定)

Ubuntuの22.10版イメージを取得してみます。

[root@server ~]# podman pull ubuntu:22.10

イメージ一覧を確認します。

[root@server ~]# podman images
REPOSITORY                TAG         IMAGE ID      CREATED      SIZE
docker.io/library/ubuntu  22.10       8264e2ec2ece  6 weeks ago  72.7 MB

確認したイメージIDを指定して、イメージを削除します。

[root@server ~]# podman rmi 8264e2ec2ece

イメージ一覧を確認します。イメージが削除されたことがわかります。

[root@server ~]# podman images
REPOSITORY  TAG         IMAGE ID    CREATED     SIZE

5.7 CentOSのイメージ取得(最新版)

下記ページを参考に、CentOSのイメージを取得してみます。
https://hub.docker.com/_/centos

CentOS7の最新イメージを取得してみます。

[root@server ~]# podman pull centos:7

CentOS6の最新イメージを取得してみます。

[root@server ~]# podman pull centos:6

イメージ一覧を確認します。

[root@server ~]# podman images
REPOSITORY             TAG         IMAGE ID      CREATED        SIZE
quay.io/centos/centos  7           8652b9f0cb4c  20 months ago  212 MB
quay.io/centos/centos  6           d0957ffdf8a2  3 years ago    202 MB

取得したイメージを一括で削除します。

[root@server ~]# podman rmi -a
Untagged: quay.io/centos/centos:7
Untagged: quay.io/centos/centos:6
Deleted: 8652b9f0cb4c0599575e5a003f5906876e10c1ceb2ab9fe1786712dac14a50cf
Deleted: d0957ffdf8a2ea8c8925903862b65a1b6850dbb019f88d45e927d3d5a3fa0c31

イメージ一覧を確認します。イメージが削除されたことがわかります。

[root@server ~]# podman images
REPOSITORY  TAG         IMAGE ID    CREATED     SIZE

5.8 CentOSのイメージ取得(版数指定)

下記ページを参考に、CentOS6.10,CentOS6.9のイメージを取得してみます。
https://hub.docker.com/_/centos/

[root@server ~]# podman pull centos:6.10
[root@server ~]# podman pull centos:6.9

イメージ一覧を確認します。

[root@server ~]# podman images
REPOSITORY             TAG         IMAGE ID      CREATED      SIZE
quay.io/centos/centos  6.9         2199b8eb8390  3 years ago  202 MB
quay.io/centos/centos  6.10        48650444e419  3 years ago  202 MB

取得したイメージを一括で削除します。

[root@server ~]# podman rmi -a

5.9 Nginxイメージ取得(最新版数)

下記ページを参考に、Nginxのイメージを取得してみます。
https://hub.docker.com/_/nginx

[root@server ~]# podman pull nginx

イメージ一覧を確認します。

[root@server ~]# podman images
REPOSITORY               TAG         IMAGE ID      CREATED     SIZE
docker.io/library/nginx  latest      670dcc86b69d  6 days ago  146 MB

6 コンテナの起動/停止/削除する方法(run/stop/rm)

AlmaLinuxの最新版イメージを取得します。

[root@server ~]# podman pull almalinux:latest

取得したイメージを確認します。

[root@server ~]# podman images
REPOSITORY                   TAG         IMAGE ID      CREATED     SIZE
docker.io/library/almalinux  latest      39f63d416992  9 days ago  196 MB

IMAGE IDを指定して、testという名前のコンテナを起動します。

[root@server ~]# podman run -it --name test 39f63d416992
[root@7681ef14d47b /]#

オプションの意味は以下のとおりです。

オプション 意味
--detach(-d) コンテナをバックグラウンドで実行する時に指定する
--interactive(-i) 対話式プロセスの場合には、-i と -t を併用してコンテナプロセスに端末を割り当てる
--name コンテナの名前を指定する

ホストでもう1つターミナルを開いて、コンテナの状態を確認します。testという名前のコンテナが起動したことがわかります。

[root@server ~]# podman ps -a
CONTAINER ID  IMAGE                               COMMAND     CREATED         STATUS             PORTS       NAMES
7681ef14d47b  docker.io/library/almalinux:latest  /bin/bash   32 seconds ago  Up 33 seconds ago              test

少し話がそれますが、コンテナを起動すると、ネームスペースが作成されることがわかります。なお、ip netnsコマンドの使い方は、ip netnsコマンドの使い方(ネットワークの実験の幅が広がるなぁ~) - hana_shinのLinux技術ブログを参照してください。

[root@server ~]# ip netns
netns-1c9a7443-c30e-742d-ed19-bcd1e72b10d4 (id: 0)

コンテを停止します。

[root@server ~]# podman stop test
test

コンテナの状態を確認します。

[root@server ~]# podman ps -a
CONTAINER ID  IMAGE                               COMMAND     CREATED             STATUS                       PORTS       NAMES
7681ef14d47b  docker.io/library/almalinux:latest  /bin/bash   About a minute ago  Exited (130) 13 seconds ago              test

コンテナを停止すると、ネームスペースが削除されることがわかります。

[root@server ~]# ip netns
[root@server ~]#

コンテナを削除します。

[root@server ~]# podman rm test
7681ef14d47b7d0abe94f5a7e019ecf803100856e6f117b3f769f19675c1cce2

コンテナの状態を確認します。コンテナが削除されたことがわかります。

[root@server ~]# podman ps -a
CONTAINER ID  IMAGE       COMMAND     CREATED     STATUS      PORTS       NAMES

7 コンテナでコマンドを実行する方法(exec)

7.1 bashを実行する場合

SUSEの最新版イメージを取得してみます。

[root@server ~]# podman pull registry.suse.com/bci/bci-base:latest

取得したイメージを確認します。

[root@server ~]# podman images
REPOSITORY                      TAG         IMAGE ID      CREATED     SIZE
registry.suse.com/bci/bci-base  latest      86698fe00598  9 days ago  122 MB

IMAGE IDを指定して、testという名前のコンテナを起動します。

[root@server ~]# podman run -it --name test 86698fe00598

testコンテナで /bin/bash コマンドを実行します。

[root@server ~]# podman exec -it test /bin/bash
2a0aa7efdfe1:/ #

コンテナ内でSLES版数を確認します。SLES版数はSLES15SP4であることがわかります。

2a0aa7efdfe1:/ # cat /etc/os-release
NAME="SLES"
VERSION="15-SP4"
VERSION_ID="15.4"
PRETTY_NAME="SUSE Linux Enterprise Server 15 SP4"
ID="sles"
ID_LIKE="suse"
ANSI_COLOR="0;32"
CPE_NAME="cpe:/o:suse:sles:15:sp4"
DOCUMENTATION_URL="https://documentation.suse.com/"

testコンテナから抜けます。

2a0aa7efdfe1:/ # exit
exit

7.2 catを実行する場合

AlmaLinuxの8.6版のイメージを取得します。

[root@server ~]#  podman pull almalinux:8.6

取得したイメージを確認します。

[root@server ~]# podman images
REPOSITORY                   TAG         IMAGE ID      CREATED      SIZE
docker.io/library/almalinux  8.6         4580d9e4bab7  2 weeks ago  195 MB

IMAGE IDを指定して、testという名前のコンテナを起動します。

[root@server ~]# podman run -it --name test 4580d9e4bab7

testコンテナで cat コマンドを実行します。

[root@server ~]# podman exec -it test cat /etc/redhat-release
AlmaLinux release 8.6 (Sky Tiger)

8 Nginxコンテナを起動する方法

Nginxのイメージを取得します。

[root@server ~]# podman pull nginx

取得したイメージを確認します。

[root@server ~]# podman images
REPOSITORY               TAG         IMAGE ID      CREATED     SIZE
docker.io/library/nginx  latest      670dcc86b69d  6 days ago  146 MB

web1という名前でNginxコンテナを起動します。-pは、ホストの8080番ポートへのパケットをNginxがTCPパケットを待ち受けている80番ポートにDNATするオプションです。

[root@server ~]# podman run -d -p 8080:80 --name web1 670dcc86b69d
6cf767a55b6fcfc231d023071f0173e67d35d093c721055de638d7879ac14165

コンテナの状態を確認します。web1という名前のコンテナが動作していることがわかります。

[root@server ~]# podman ps
CONTAINER ID  IMAGE                           COMMAND               CREATED            STATUS                PORTS                 NAMES
6cf767a55b6f  docker.io/library/nginx:latest  nginx -g daemon o...  About an hour ago  Up About an hour ago  0.0.0.0:8080->80/tcp  web1

web1コンテナで動作しているプロセスを確認します。nginx プロセスが動作していることがわかります。

[root@server ~]# podman top web1
[USER        PID         PPID        %CPU        ELAPSED             TTY         TIME        COMMAND
root        1           0           0.000       1h12m34.751738038s  ?           0s          nginx: master process nginx -g daemon off;
nginx       31          1           0.000       1h12m31.752241266s  ?           0s          nginx: worker process
nginx       32          1           0.000       1h12m31.75239912s   ?           0s          nginx: worker process
nginx       33          1           0.000       1h12m31.752494334s  ?           0s          nginx: worker process
nginx       34          1           0.000       1h12m31.752615683s  ?           0s          nginx: worker process

コンテナとホストのポート番号の対応関係を確認します。コンテナの80番ポートがホストの8080番ポートに対応していることがわかります。

[root@server ~]# podman port web1
80/tcp -> 0.0.0.0:8080

iptablesのターゲットを確認します。Nginxコンテナを起動すると、以下のターゲットがiptablesに作成されます。

[root@server ~]# iptables -t nat -nvL |grep 8080
 pkts bytes target     prot opt in     out     source               destination
    0     0 CNI-DN-ddb746c8c102be9c936ab  tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            /* dnat name: "podman" id: "6cf767a55b6fcfc231d023071f0173e67d35d093c721055de638d7879ac14165" */ multiport dports 8080
    0     0 CNI-HOSTPORT-SETMARK  tcp  --  *      *       10.88.0.0/16         0.0.0.0/0            tcp dpt:8080
    0     0 CNI-HOSTPORT-SETMARK  tcp  --  *      *       127.0.0.1            0.0.0.0/0            tcp dpt:8080
    0     0 DNAT       tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:8080 to:10.88.0.2:80

curlコマンドでNginxにアクセスしてみます。なお。curlコマンドの使い方は、curlコマンドの使い方 - hana_shinのLinux技術ブログを参照してください。

[root@client ~]# curl -I http://192.168.122.68:8080
HTTP/1.1 200 OK
Server: nginx/1.23.1
Date: Tue, 26 Jul 2022 12:37:13 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Tue, 19 Jul 2022 14:05:27 GMT
Connection: keep-alive
ETag: "62d6ba27-267"
Accept-Ranges: bytes

curlコマンドを1回実行すると、DNATターゲットが処理したパケット数が1増加していることがわかります。DNATでは、ホストが受信したTCPパケットの宛先IPアドレスを10.88.0.2、宛先ポート番号を8080から80に変更しています。

[root@server ~]# iptables -t nat -nvL |grep 8080
 pkts bytes target     prot opt in     out     source               destination
    1    60 CNI-DN-ddb746c8c102be9c936ab  tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            /* dnat name: "podman" id: "6cf767a55b6fcfc231d023071f0173e67d35d093c721055de638d7879ac14165" */ multiport dports 8080
    0     0 CNI-HOSTPORT-SETMARK  tcp  --  *      *       10.88.0.0/16         0.0.0.0/0            tcp dpt:8080
    0     0 CNI-HOSTPORT-SETMARK  tcp  --  *      *       127.0.0.1            0.0.0.0/0            tcp dpt:8080
    1    60 DNAT       tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:8080 to:10.88.0.2:80

9 コンテナが出力するログを確認する方法(logs)

コンテナの状態を確認します。web1という名前のコンテナが動作しています。

[root@server ~]# podman ps
CONTAINER ID  IMAGE                           COMMAND               CREATED      STATUS          PORTS                 NAMES
6cf767a55b6f  docker.io/library/nginx:latest  nginx -g daemon o...  2 hours ago  Up 2 hours ago  0.0.0.0:8080->80/tcp  web1

web1コンテナが出力するログを確認します。なお、-tは時刻を表示するオプションです。

[root@server ~]# podman logs -t web1
2022-07-27T06:36:27.689125596-04:00 /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
2022-07-27T06:36:27.689125596-04:00 /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
2022-07-27T06:36:27.793904921-04:00 /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
-snip-

-fオプションを指定すると、ログ末尾からメッセージを表示できます。

[root@server ~]# podman logs -ft web1
-snip-
2022-07-27T06:36:29.295646046-04:00 2022/07/27 10:36:29 [notice] 1#1: start worker process 33
2022-07-27T06:36:29.362196075-04:00 2022/07/27 10:36:29 [notice] 1#1: start worker process 34
2022-07-27T07:27:10.427918068-04:00 192.168.122.177 - - [27/Jul/2022:11:27:10 +0000] "HEAD / HTTP/1.1" 200 0 "-" "curl/7.61.1" "-"

10 ファイルをコピーする方法(cp)

ここでは、Nginxコンテナとホスト間でファイルのコピーを試してみます。

10.1 ホストからコンテナにファイルをコピーする方法

まず、コンテナの名前を確認します。実行結果の右端を確認すると、コンテナの名前がweb1であることがわかります。

root@server ~]# podman ps
CONTAINER ID  IMAGE                           COMMAND               CREATED         STATUS             PORTS                 NAMES
2a5cbc04259e  docker.io/library/nginx:latest  nginx -g daemon o...  26 minutes ago  Up 26 minutes ago  0.0.0.0:8080->80/tcp  web1

ホストでファイルを作成します。

[root@server ~]# touch index.html
[root@server ~]# echo "Hello" > index.html
[root@server ~]# cat index.html
Hello

ホストからコンテナにファイルをコピーをします。cpコマンドの書式は以下になります。

podman cp [ホスト側のファイルパス名] コンテナ名:[コンテナ側のファイルパス名]

ファイルをコピーします。

[root@server ~]# podman cp /root/index.html web1:/usr/share/nginx/html/

curlコマンドでNginxコンテナにアクセスすると、ホストで作成したindex.htmlファイルの中身が表示されることがわかります。なお、curlコマンドの使い方は、curlコマンドの使い方 - hana_shinのLinux技術ブログを参照してください。

[root@server ~]# curl http://192.168.122.68:8080
Hello

10.2 コンテナからホストにファイルをコピーする方法

コンテナでファイルを作成します。

root@2a5cbc04259e:~# pwd
/root
root@2a5cbc04259e:~# touch container.dat
root@2a5cbc04259e:~# echo "I'm in container" > container.dat
root@2a5cbc04259e:~# cat container.dat
I'm in container

コンテナからホストにファイルをコピーをします。このときcpコマンドの書式は以下になります。

podman cp コンテナ名:[コンテナ側のファイルパス名]  [ホスト側のファイルパス名] 

ファイルをコピーをします。

[root@server ~]# podman cp web1:/root/container.dat /tmp/

コンテナからホストにコピーしたファイルの中身を確認します。コンテナで作成したファイルであることがわかります。

[root@server ~]# cat /tmp/container.dat
I'm in container

11 コンテナを他のホストに移動する方法(commit/save/load)

ここでは、10章で作成したNginxコンテナをサーバからクライアントに移動してみます。手順は次のようになります。
(1) コンテナをイメージ化(サーバで実施)
(2) イメージをtarファイルに保存(サーバで実施)
(3) サーバからクライアンにtarファイルを転送
(4) tarファイルをイメージに保存(クライアントで実施)
(5) イメージからコンテナ起動(クライアントで実施)

11.1 サーバ側の手順

コンテナをイメージ化します。コンテナをイメージ化すると、コンテナを他のホストに移動したり、同じ構成のコンテナを複数作成することができます。

イメージに書き出すコンテナの名前を確認します。名前はweb1であることがわかります。

[root@server ~]# podman ps
CONTAINER ID  IMAGE                           COMMAND               CREATED         STATUS             PORTS                 NAMES
0b6e8582e445  docker.io/library/nginx:latest  nginx -g daemon o...  28 minutes ago  Up 28 minutes ago  0.0.0.0:8080->80/tcp  web1

コンテナをweb1_imageという名前のイメージに書き出します。
このときcommitコマンドの書式は以下になります。
podman commit コンテナ名 イメージ名

[root@server ~]# podman commit web1 web1_image
Getting image source signatures
Copying blob ec4a38999118 skipped: already exists
Copying blob f7ed3797e296 skipped: already exists
Copying blob f86e88a471f4 skipped: already exists
Copying blob 5a5bafd53f76 skipped: already exists
Copying blob e2d75d87993c skipped: already exists
Copying blob 6cffb086835a skipped: already exists
Copying blob 1e966fea3cb1 done
Copying config 5dd8a2bd0e done
Writing manifest to image destination
Storing signatures
5dd8a2bd0ee2108b1a0816a56975ebd6363de0690e14ebd287e4014d78681ded

作成したイメージを確認します。

[root@server ~]# podman images
REPOSITORY               TAG         IMAGE ID      CREATED         SIZE
localhost/web1_image     latest      5dd8a2bd0ee2  53 minutes ago  146 MB
docker.io/library/nginx  latest      88736fe82739  4 days ago      146 MB

web1_imageというイメージをweb1_image.tarという名前のファイルに保存します。

[root@server ~]# podman save -o web1_image.tar web1_image
Copying blob ec4a38999118 done
Copying blob f7ed3797e296 done
Copying blob f86e88a471f4 done
Copying blob 5a5bafd53f76 done
Copying blob e2d75d87993c done
Copying blob 6cffb086835a done
Copying blob 1e966fea3cb1 done
Copying config 5dd8a2bd0e done
Writing manifest to image destination
Storing signatures

作成したファイルを確認します。

[root@server ~]# ls -l web1_image.tar
-rw-r--r--. 1 root root 146211840 11月 19 21:39 web1_image.tar

作成したファイルをクライアントに転送します。

[root@server ~]# scp web1_image.tar root@192.168.122.177:/root
root@192.168.122.177's password:
web1_image.tar

11.2 クライアント側の手順

ファイルの確認をします。

[root@client ~]# ls -l web1_image.tar
-rw-r--r--. 1 root root 146211840 11月 19 21:42 web1_image.tar

登録されているイメージを確認します。まだイメージが何も登録されていないことがわかります。

[root@client ~]# podman images
REPOSITORY  TAG         IMAGE ID    CREATED     SIZE

tarファイルをイメージとして保存します。

[root@client ~]# podman load -i web1_image.tar
Getting image source signatures
Copying blob 6cffb086835a done
Copying blob 5a5bafd53f76 done
Copying blob ec4a38999118 done
Copying blob e2d75d87993c done
Copying blob f7ed3797e296 done
Copying blob f86e88a471f4 done
Copying blob 1e966fea3cb1 done
Copying config 5dd8a2bd0e done
Writing manifest to image destination
Storing signatures
Loaded image: localhost/web1_image:latest

イメージを確認すると、tarファイルがイメージとして保存されたことがわかります。

[root@client ~]# podman images
REPOSITORY            TAG         IMAGE ID      CREATED         SIZE
localhost/web1_image  latest      5dd8a2bd0ee2  52 minutes ago  146 MB

イメージからコンテナを起動します。今回は、web2という名前でコンテナを起動してみます。

[root@client ~]# podman run -d -p 8080:80 --name web2 5dd8a2bd0ee2
bc87b3fccb5fe70973aac96024e938bbe3d7ca7f9769e5e636500de89a52ab1e

コンテナを確認します。web2という名前のコンテナが起動できたことがわかります。

[root@client ~]# podman ps
CONTAINER ID  IMAGE                        COMMAND               CREATED        STATUS            PORTS                 NAMES
bc87b3fccb5f  localhost/web1_image:latest  nginx -g daemon o...  5 seconds ago  Up 5 seconds ago  0.0.0.0:8080->80/tcp  web2

lsofコマンドを実行します。コンテナがTCPの8080番ポートでListenしていることがわかります。なお、lsofコマンドの使い方は、lsofコマンドの使い方 - hana_shinのLinux技術ブログを参照してください。

[root@client ~]# lsof -i:8080 -a -i -a -nP
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
conmon  2458 root    5u  IPv4  75500      0t0  TCP *:8080 (LISTEN)

curlコマンドでNginxにアクセスします。サーバで作成したindex.htmlの内容が表示できたことがわかります。

[root@client ~]# curl http://192.168.122.177:8080
Hello

12 ボリュームをマウントする方法

ボリュームをマウントする方法には、以下の方法があります。

方法 概要
バインドマウント コンテナ内やホストのファイルやディレクトリをマウントする
ボリュームマウント コンテナ内にボリュームを作成してマウントする

12.1 バインドマウントする方法

マウントするディレクトリを作成します。

[root@server ~]# mkdir /test_html

作成したディレクトリにテスト用のindex.htmlファイルを作成します。

[root@server ~]# echo "Test" > /test_html/index.html

起動するコンテナのイメージIDを確認します。

[root@server ~]# podman images
REPOSITORY               TAG         IMAGE ID      CREATED     SIZE
docker.io/library/nginx  latest      88736fe82739  4 days ago  146 MB

確認したイメージIDを指定して、web3という名前のコンテナを起動します。このとき"-v"オプションを使って、/test_htmlをコンテナのディレクトリにマウントをします。

[root@server ~]# podman run -d -p 8080:80 -v /test_html:/usr/share/nginx/html --name web3 88736fe82739
e6c10a2857cf397c62c86f482c2fd16984fa4385dcd7832073f6c2e40c35f408

SELinuxが有効になっていると、この後の動作確認がうまくいきません。本来、SELinuxのルールを作成するべきですが、お手軽にマウントの実行結果を試すため、一時的にSELinuxをPermissiveにします。

[root@server ~]# setenforce 0
[root@server ~]# getenforce
Permissive

curlコマンドを使ってコンテナにアクセスします。テスト用に作成したindex.htmlファイルの中身を確認することができました。

[root@server ~]# curl http://192.168.122.68:8080
Test

12.2 ボリュームマウントする方法

未稿