LoginSignup
2
2

Podmanの学習記録#1

Last updated at Posted at 2024-05-09

関連記事

はじめに

EX188の試験対策の一環として、ローカル環境で学習した内容を備忘録として残していきます。

#1ではPodmanの操作に慣れることを目標としています。主に以下のような内容を行いました。

  • podman run でコンテナを構築
  • コンテナのライフサイクルを理解する
  • コンテナへのアクセス方法を理解する
  • ポートフォワード設定をしたコンテナを構築できる
  • ホストとコンテナ間のファイルコピーができる
  • コンテナの環境変数を設定できる
  • コンテナネットワークの作成・特性を理解する

EX188の詳細

受験された方のブログで、有益な情報が紹介されています。学習教材についても紹介されているので要確認。

環境

  • Ubuntu 22.04.2 LTS (Hyper-V上に構築)

Ubuntu 22.04 にPodmanをインストール

sudo apt-get update
sudo apt-get -y install podman

Podmanの基本操作

バージョン確認

test@ubuntu-podman:~$ podman -v
podman version 3.4.4
test@ubuntu-podman:~$ 

イメージのPull

podman pull nginx:latest

以下のようなエラーがでる。

test@ubuntu-podman:~$ podman pull nginx:latest
Error: short-name "nginx:latest" did not resolve to an alias and no unqualified-search registries are defined in "/etc/containers/registries.conf"
test@ubuntu-podman:~$

イメージ元が設定されていないらしいので、以下のような設定をする。
これでDocker Hub からPullできるようになる。

/etc/containers/registries.conf
- # unqualified-search-registries = ["example.com"]
+ unqualified-search-registries = ['docker.io']

問題なくPullできた。

test@ubuntu-podman:~$ podman pull nginx:latest
Resolving "nginx" using unqualified-search registries (/etc/containers/registries.conf)
Trying to pull docker.io/library/nginx:latest...
Getting image source signatures
Copying blob 72de7d1ce3a4 done  
Copying blob e42dcfe1730b done  
Copying blob 94f34d60e454 done  
Copying blob 27e923fb52d3 done  
Copying blob 360eba32fa65 done  
Copying blob c5903f3678a7 done  
Copying blob 907d1bb4e931 done  
Copying config f5a6b296b8 done  
Writing manifest to image destination
Storing signatures
f5a6b296b8a29b4e3d89ffa99e4a86309874ae400e82b3d3993f84e1e3bb0eb9
test@ubuntu-podman:~$

イメージ一覧の確認

test@ubuntu-podman:~$ podman images
REPOSITORY               TAG         IMAGE ID      CREATED       SIZE
docker.io/library/nginx  latest      f5a6b296b8a2  43 hours ago  191 MB
test@ubuntu-podman:~$ 

コンテナの実行

test@ubuntu-podman:~$ podman run nginx:latest echo "Hello Nginx"
Hello Nginx
test@ubuntu-podman:~$ 

コンテナ一覧の確認

test@ubuntu-podman:~$ podman ps -a
CONTAINER ID  IMAGE                           COMMAND           CREATED         STATUS                     PORTS       NAMES
574faf5721d7  docker.io/library/nginx:latest  echo Hello Nginx  27 seconds ago  Exited (0) 27 seconds ago              brave_shaw
test@ubuntu-podman:~$ 

JSONフォーマットで表示する場合は以下を実行する。

podman ps -a --format=json

コンテナの削除

podman rm <コンテナ名 or コンテナID>
test@ubuntu-podman:~$ podman rm 574faf5721d7
574faf5721d735c428127d55bd6a4a191f205dccc7b1e90b924d14f254405dbe
test@ubuntu-podman:~$ 

Podmanでコンテナサービスを起動する

Nginxコンテナの構築

コンテナ内の80番ポートを8080ポートで公開したNginxのサービスを起動する。--rmオプションはコンテナを終了した際にコンテナを削除するオプション。

podman run --rm -p 8080:80 nginx:latest

curlコマンドでhttp://localhost:8080にアクセスすると、ページが表示される。

test@ubuntu-podman:~$ curl http://localhost:8080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
test@ubuntu-podman:~$ 

Ctrl + Cで終了する。

コンテナをバックグラウンドで起動する

-dオプションを付ける。--nameでコンテナ名を指定した。

podman run -d -p 8080:80 --name nginx_test nginx:latest

コンテナの調査とライフサイクル

inspectでコンテナの詳細を確認できる。

podman inspect nginx_test
実行結果を見る
test@ubuntu-podman:~$ podman inspect nginx_test
[
    {
        "Id": "232e166b362061656c7379a4809b666d59c288e90a4539ff079342010aa6a19a",
        "Created": "2023-09-09T09:39:31.247919402Z",
        "Path": "/docker-entrypoint.sh",
        "Args": [
            "nginx",
            "-g",
            "daemon off;"
        ],
        "State": {
            "OciVersion": "1.0.2-dev",
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 6570,
            "ConmonPid": 6567,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2023-09-09T09:39:31.561750915Z",
            "FinishedAt": "0001-01-01T00:00:00Z",
            "Healthcheck": {
                "Status": "",
                "FailingStreak": 0,
                "Log": null
            },
            "CgroupPath": "/user.slice/user-1000.slice/user@1000.service/user.slice/libpod-232e166b362061656c7379a4809b666d59c288e90a4539ff079342010aa6a19a.scope"
        },
        "Image": "f5a6b296b8a29b4e3d89ffa99e4a86309874ae400e82b3d3993f84e1e3bb0eb9",
        "ImageName": "docker.io/library/nginx:latest",
        "Rootfs": "",
        "Pod": "",
        "ResolvConfPath": "/run/user/1000/containers/overlay-containers/232e166b362061656c7379a4809b666d59c288e90a4539ff079342010aa6a19a/userdata/resolv.conf",
        "HostnamePath": "/run/user/1000/containers/overlay-containers/232e166b362061656c7379a4809b666d59c288e90a4539ff079342010aa6a19a/userdata/hostname",
        "HostsPath": "/run/user/1000/containers/overlay-containers/232e166b362061656c7379a4809b666d59c288e90a4539ff079342010aa6a19a/userdata/hosts",
        "StaticDir": "/home/test/.local/share/containers/storage/overlay-containers/232e166b362061656c7379a4809b666d59c288e90a4539ff079342010aa6a19a/userdata",
        "OCIConfigPath": "/home/test/.local/share/containers/storage/overlay-containers/232e166b362061656c7379a4809b666d59c288e90a4539ff079342010aa6a19a/userdata/config.json",
        "OCIRuntime": "crun",
        "ConmonPidFile": "/run/user/1000/containers/overlay-containers/232e166b362061656c7379a4809b666d59c288e90a4539ff079342010aa6a19a/userdata/conmon.pid",
        "PidFile": "/run/user/1000/containers/overlay-containers/232e166b362061656c7379a4809b666d59c288e90a4539ff079342010aa6a19a/userdata/pidfile",
        "Name": "nginx_test",
        "RestartCount": 0,
        "Driver": "overlay",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "EffectiveCaps": [
            "CAP_CHOWN",
            "CAP_DAC_OVERRIDE",
            "CAP_FOWNER",
            "CAP_FSETID",
            "CAP_KILL",
            "CAP_NET_BIND_SERVICE",
            "CAP_SETFCAP",
            "CAP_SETGID",
            "CAP_SETPCAP",
            "CAP_SETUID",
            "CAP_SYS_CHROOT"
        ],
        "BoundingCaps": [
            "CAP_CHOWN",
            "CAP_DAC_OVERRIDE",
            "CAP_FOWNER",
            "CAP_FSETID",
            "CAP_KILL",
            "CAP_NET_BIND_SERVICE",
            "CAP_SETFCAP",
            "CAP_SETGID",
            "CAP_SETPCAP",
            "CAP_SETUID",
            "CAP_SYS_CHROOT"
        ],
        "ExecIDs": [],
        "GraphDriver": {
            "Name": "overlay",
            "Data": {
                "LowerDir": "/home/test/.local/share/containers/storage/overlay/59bab5d60ce210eee3f367d06d2a29aeac36d0b880630d10699e24f1715f74a2/diff:/home/test/.local/share/containers/storage/overlay/3434a7efcf7d09995ea16875f76af790c314f7e92f7afbf17191628c3046452d/diff:/home/test/.local/share/containers/storage/overlay/b161a4c3108c3819629502e1c65655490695c0d0af61f4730806cb8836430ed9/diff:/home/test/.local/share/containers/storage/overlay/786151e97d17534cfa3b7b130906b6496173024756df678426a2ded4d3e95d7c/diff:/home/test/.local/share/containers/storage/overlay/67805750832404aa1a220e4478b8d808d9af4d890cab59ada30c9317ea5b9ac0/diff:/home/test/.local/share/containers/storage/overlay/e13f23d2b41eddd1b18541620ae4f227bdcb3382ee52670c78737b3aadc20d53/diff:/home/test/.local/share/containers/storage/overlay/a2d7501dfb3541f3d736125cdfd748618194f60cbb5c63f4de67a92530899628/diff",
                "MergedDir": "/home/test/.local/share/containers/storage/overlay/1125589d8f15f0a98db6b9f52c34854a8acca973b542d77f6abd23bcfcad2a15/merged",
                "UpperDir": "/home/test/.local/share/containers/storage/overlay/1125589d8f15f0a98db6b9f52c34854a8acca973b542d77f6abd23bcfcad2a15/diff",
                "WorkDir": "/home/test/.local/share/containers/storage/overlay/1125589d8f15f0a98db6b9f52c34854a8acca973b542d77f6abd23bcfcad2a15/work"
            }
        },
        "Mounts": [],
        "Dependencies": [],
        "NetworkSettings": {
            "EndpointID": "",
            "Gateway": "",
            "IPAddress": "",
            "IPPrefixLen": 0,
            "IPv6Gateway": "",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "MacAddress": "",
            "Bridge": "",
            "SandboxID": "",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "80/tcp": [
                    {
                        "HostIp": "",
                        "HostPort": "8080"
                    }
                ]
            },
            "SandboxKey": "/run/user/1000/netns/cni-9f408244-0f6c-e44f-f595-c7440b057fb2"
        },
        "ExitCommand": [
            "/usr/bin/podman",
            "--root",
            "/home/test/.local/share/containers/storage",
            "--runroot",
            "/run/user/1000/containers",
            "--log-level",
            "warning",
            "--cgroup-manager",
            "systemd",
            "--tmpdir",
            "/run/user/1000/libpod/tmp",
            "--runtime",
            "crun",
            "--storage-driver",
            "overlay",
            "--events-backend",
            "journald",
            "container",
            "cleanup",
            "--rm",
            "232e166b362061656c7379a4809b666d59c288e90a4539ff079342010aa6a19a"
        ],
        "Namespace": "",
        "IsInfra": false,
        "Config": {
            "Hostname": "232e166b3620",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "TERM=xterm",
                "container=podman",
                "NGINX_VERSION=1.25.2",
                "NJS_VERSION=0.8.0",
                "PKG_RELEASE=1~bookworm",
                "HOME=/root",
                "HOSTNAME=232e166b3620"
            ],
            "Cmd": [
                "nginx",
                "-g",
                "daemon off;"
            ],
            "Image": "docker.io/library/nginx:latest",
            "Volumes": null,
            "WorkingDir": "/",
            "Entrypoint": "/docker-entrypoint.sh",
            "OnBuild": null,
            "Labels": {
                "maintainer": "NGINX Docker Maintainers \u003cdocker-maint@nginx.com\u003e"
            },
            "Annotations": {
                "io.container.manager": "libpod",
                "io.kubernetes.cri-o.Created": "2023-09-09T09:39:31.247919402Z",
                "io.kubernetes.cri-o.TTY": "false",
                "io.podman.annotations.autoremove": "TRUE",
                "io.podman.annotations.init": "FALSE",
                "io.podman.annotations.privileged": "FALSE",
                "io.podman.annotations.publish-all": "FALSE",
                "org.opencontainers.image.stopSignal": "3"
            },
            "StopSignal": 3,
            "CreateCommand": [
                "podman",
                "run",
                "--rm",
                "-d",
                "-p",
                "8080:80",
                "--name",
                "nginx_test",
                "nginx:latest"
            ],
            "Umask": "0022",
            "Timeout": 0,
            "StopTimeout": 10
        },
        "HostConfig": {
            "Binds": [],
            "CgroupManager": "systemd",
            "CgroupMode": "private",
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "journald",
                "Config": null,
                "Path": "",
                "Tag": "",
                "Size": "0B"
            },
            "NetworkMode": "slirp4netns",
            "PortBindings": {
                "80/tcp": [
                    {
                        "HostIp": "",
                        "HostPort": "8080"
                    }
                ]
            },
            "RestartPolicy": {
                "Name": "",
                "MaximumRetryCount": 0
            },
            "AutoRemove": true,
            "VolumeDriver": "",
            "VolumesFrom": null,
            "CapAdd": [],
            "CapDrop": [
                "CAP_AUDIT_WRITE",
                "CAP_MKNOD",
                "CAP_NET_RAW"
            ],
            "Dns": [],
            "DnsOptions": [],
            "DnsSearch": [],
            "ExtraHosts": [],
            "GroupAdd": [],
            "IpcMode": "private",
            "Cgroup": "",
            "Cgroups": "default",
            "Links": null,
            "OomScoreAdj": 0,
            "PidMode": "private",
            "Privileged": false,
            "PublishAllPorts": false,
            "ReadonlyRootfs": false,
            "SecurityOpt": [],
            "Tmpfs": {},
            "UTSMode": "private",
            "UsernsMode": "",
            "ShmSize": 65536000,
            "Runtime": "oci",
            "ConsoleSize": [
                0,
                0
            ],
            "Isolation": "",
            "CpuShares": 0,
            "Memory": 0,
            "NanoCpus": 0,
            "CgroupParent": "user.slice",
            "BlkioWeight": 0,
            "BlkioWeightDevice": null,
            "BlkioDeviceReadBps": null,
            "BlkioDeviceWriteBps": null,
            "BlkioDeviceReadIOps": null,
            "BlkioDeviceWriteIOps": null,
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpuRealtimePeriod": 0,
            "CpuRealtimeRuntime": 0,
            "CpusetCpus": "",
            "CpusetMems": "",
            "Devices": [],
            "DiskQuota": 0,
            "KernelMemory": 0,
            "MemoryReservation": 0,
            "MemorySwap": 0,
            "MemorySwappiness": 0,
            "OomKillDisable": false,
            "PidsLimit": 2048,
            "Ulimits": [],
            "CpuCount": 0,
            "CpuPercent": 0,
            "IOMaximumIOps": 0,
            "IOMaximumBandwidth": 0,
            "CgroupConf": null
        }
    }
]
test@ubuntu-podman:~$

コンテナのステータスを確認したいとき、

test@ubuntu-podman:~$ podman inspect --format='{{.State.Status}}' nginx_test
running
test@ubuntu-podman:~$ 

という風に確認もできる。

コンテナの停止

test@ubuntu-podman:~$ podman stop nginx_test
nginx_test
test@ubuntu-podman:~$ podman ps -a
CONTAINER ID  IMAGE                           COMMAND               CREATED         STATUS                    PORTS                 NAMES
94855343908b  docker.io/library/nginx:latest  nginx -g daemon o...  31 seconds ago  Exited (0) 4 seconds ago  0.0.0.0:8080->80/tcp  nginx_test
test@ubuntu-podman:~$ 

コンテナーの停止とコンテナーの削除同じではありません。コンテナの停止をしてもpodman ps -aで確認するとコンテナが残っている。

コンテナのスタート

test@ubuntu-podman:~$ podman start nginx_test
nginx_test
test@ubuntu-podman:~$

コンテナの再起動

test@ubuntu-podman:~$ podman restart nginx_test
94855343908bcd6762a38ab13755fc5ef746d35c4eed82b17a684ed5bf5a6df3
test@ubuntu-podman:~$ 

コンテナーを強制的に停止する

test@ubuntu-podman:~$ podman kill nginx_test
nginx_test
test@ubuntu-podman:~$ 

コンテナを強制削除する

test@ubuntu-podman:~$ podman rm nginx_test --force
94855343908bcd6762a38ab13755fc5ef746d35c4eed82b17a684ed5bf5a6df3
test@ubuntu-podman:~$ podman ps -a
CONTAINER ID  IMAGE       COMMAND     CREATED     STATUS      PORTS       NAMES
test@ubuntu-podman:~$ 

コンテナへのアクセス

実行中のコンテナ内を確認する方法を確認する。

コンテナを起動します。

podman run -d -p 8080:80 --name nginx_test nginx:latest

起動したコンテナに対して以下を実行してみる。コンテナ内のファイルを確認できる。

test@ubuntu-podman:~$ podman exec nginx_test cat /etc/nginx/nginx.conf

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}
test@ubuntu-podman:~$ 

コンテナで対話型シェルを開く

--interactive(-i) オプションと --tty(-t) オプションを付ける。

test@ubuntu-podman:~$ podman exec -it nginx_test bash
root@c84c85b0a5e5:/# pwd
/
root@c84c85b0a5e5:/# ls
bin   dev                  docker-entrypoint.sh  home  lib32  libx32  mnt  proc  run   srv  tmp  var
boot  docker-entrypoint.d  etc                   lib   lib64  media   opt  root  sbin  sys  usr
root@c84c85b0a5e5:/# exit
exit
test@ubuntu-podman:~$ 

コンテナ内でコマンドを実行できる。抜けるときはexitコマンドを実行する。

ホストとコンテナ間のファイルコピー

コンテナ内のファイルをホストへコピー

podman cp <コンテナ名>:<コンテナ内のファイルパス> <コピー先のファイルパス>

コンテナ内の/usr/share/nginx/html/index.htmlをホスト側のindex.htmlとしてコピーする。

test@ubuntu-podman:~$ podman cp nginx_test:/usr/share/nginx/html/index.html index.html
test@ubuntu-podman:~$ ls -al index.html 
-rw-r--r-- 1 test test 615 Aug 15 17:03 index.html
test@ubuntu-podman:~$ 

ホストのファイルをコンテ内にコピー

podman cp <コピー元のファイルパス(ホスト側)> <コンテナ名>:<コンテナ内のファイルパス> 

ホストにあるindex.htmlnginx_testコンテナ内の/usr/share/nginx/html/index.htmlにコピーする。

test@ubuntu-podman:~$ echo "Hello Nginx" > index.html
test@ubuntu-podman:~$ cat index.html 
Hello Nginx
test@ubuntu-podman:~$ podman cp index.html nginx_test:/usr/share/nginx/html/index.html 
test@ubuntu-podman:~$ curl http://localhost:8080
Hello Nginx
test@ubuntu-podman:~$ 

コンテナの環境変数

-eオプションで環境変数を設定できる。

test@ubuntu-podman:~$ podman run -d -p 8080:80 -e ENV=test --name nginx_test nginx:latest
65f769160ed742ff9bc3df914e62c41fe49096795961c9cbf732a6ad14ae8a7c
test@ubuntu-podman:~$ podman exec nginx_test printenv ENV
test
test@ubuntu-podman:~$ 

複数環境変数を指定したい場合は、-eオプションを追加で付ける。

podman run -d -p 8080:80 -e ENV=test -e ENV2=test2 --name nginx_test nginx:latest

コンテナネットワークの基本

コンテナネットワークを作成

podman network create <ネットワーク名>
test@ubuntu-podman:~$ podman network create nginx_net
/home/test/.config/cni/net.d/nginx_net.conflist
test@ubuntu-podman:~$ 

ネットワークの一覧

test@ubuntu-podman:~$ podman network ls
WARN[0000] Error validating CNI config file /home/test/.config/cni/net.d/nginx_net.conflist: [plugin bridge does not support config version "1.0.0" plugin portmap does not support config version "1.0.0" plugin firewall does not support config version "1.0.0" plugin tuning does not support config version "1.0.0"] 
NETWORK ID    NAME        VERSION     PLUGINS
2f259bab93aa  podman      0.4.0       bridge,portmap,firewall,tuning
4423554fd99c  nginx_net   1.0.0       bridge,portmap,firewall,tuning,dnsname
test@ubuntu-podman:~$

何かWarningがでてしまった。詳細はよく分からないが、どうやらバグっぽい?

/home/test/.config/cni/net.d/nginx_net.conflistcniVersion0.4.0 に直してみると、以下のように上手くいった。

test@ubuntu-podman:~$ podman network ls
NETWORK ID    NAME        VERSION     PLUGINS
2f259bab93aa  podman      0.4.0       bridge,portmap,firewall,tuning
4423554fd99c  nginx_net   0.4.0       bridge,portmap,firewall,tuning,dnsname
test@ubuntu-podman:~$ 

ネットワークの詳細を確認

podman inspect <ネットワーク名>
実行結果を見る
test@ubuntu-podman:~$ podman inspect nginx_net
[
    {
        "cniVersion": "0.4.0",
        "name": "nginx_net",
        "plugins": [
            {
                "bridge": "cni-podman1",
                "hairpinMode": true,
                "ipMasq": true,
                "ipam": {
                    "ranges": [
                        [
                            {
                                "gateway": "10.89.0.1",
                                "subnet": "10.89.0.0/24"
                            }
                        ]
                    ],
                    "routes": [
                        {
                            "dst": "0.0.0.0/0"
                        }
                    ],
                    "type": "host-local"
                },
                "isGateway": true,
                "type": "bridge"
            },
            {
                "capabilities": {
                    "portMappings": true
                },
                "type": "portmap"
            },
            {
                "backend": "",
                "type": "firewall"
            },
            {
                "type": "tuning"
            },
            {
                "capabilities": {
                    "aliases": true
                },
                "domainName": "dns.podman",
                "type": "dnsname"
            }
        ]
    }
]
test@ubuntu-podman:~$ 

ネットワークの削除

podman network rm <ネットワーク名>

ネットワークを指定してコンテナを起動

--net オプションでネットワークを指定できる。

test@ubuntu-podman:~$ podman run -d -p 8080:80 --name nginx_test --net nginx_net nginx:latest
1ab91776712636f38836322a575165f29b5b265f881a39fc75245e36108d8c16
test@ubuntu-podman:~$ 

ネットワークに接続されていることを確認する。

test@ubuntu-podman:~$ podman inspect --format='{{.NetworkSettings.Networks}}' nginx_test
map[nginx_net:0xc00042fc20]
test@ubuntu-podman:~$ 

サービス名による名前解決

同じネットワークに所属するコンテナ間では、コンテナのサービス名等で名前解決が可能になる。

ネットワーク nginx_net に所属する2つのコンテナnginx_test_anginx_test_bを構築する。

podman run -d -p 8080:80 --name nginx_test_a --net nginx_net nginx:latest
podman run -d -p 8088:80 --name nginx_test_b --net nginx_net nginx:latest
test@ubuntu-podman:~$ podman ps -a
CONTAINER ID  IMAGE                           COMMAND               CREATED         STATUS             PORTS                 NAMES
82ab0a27db7d  docker.io/library/nginx:latest  nginx -g daemon o...  55 seconds ago  Up 56 seconds ago  0.0.0.0:8080->80/tcp  nginx_test_a
dfcdc7ef118c  docker.io/library/nginx:latest  nginx -g daemon o...  2 seconds ago   Up 3 seconds ago   0.0.0.0:8088->80/tcp  nginx_test_b
test@ubuntu-podman:~$ 

nginx_test_aコンテナ内に入り、http://nginx_test_b にcurlすると、ページが表示される。(名前解決できていることが分かる。)

test@ubuntu-podman:~$ podman exec -it nginx_test_a bash
root@82ab0a27db7d:/# curl http://nginx_test_b
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
root@82ab0a27db7d:/# exit
exit
test@ubuntu-podman:~$ 

まとめ

  • podman run でコンテナを構築できた
  • podman psでコンテナ一覧を確認できた
  • コンテナのライフサイクルをコマンドを通して理解できた
  • コンテナへのアクセス方法を理解できた
  • ポートフォワード設定をしたコンテナを構築できた
  • podman cp でホストとコンテナ間のファイルコピーができた
  • コンテナの環境変数を設定できる
  • コンテナネットワークの作成・利用方法を確認

Podmanの学習記録#2では以下を扱う予定です。

  • イメージの管理方法
  • カスタムコンテナーイメージの作成
  • データの永続化(ボリュームマウント)
  • コンテナのトラブルシューティングの基本
2
2
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
2
2