本教程适用于以下操作系统版本
- Rocky Linux 8.4
- Rocky Linux 8.4
本页内容
- 前提条件
- 在 Rocky Linux 上安装 Podman
- Podman 重要配置
- 设置新用户
- Podman 基本用法 搜索容器镜像 管理容器镜像 使用 Podman 运行容器 使用 Podman 检查容器日志 使用 Podman 停止容器 使用 Podman 设置自定义卷 SSH 连接到正在运行的容器 清理环境
- 结论
- 搜索容器镜像
- 管理容器镜像
- 使用 Podman 运行容器
- 使用 Podman 检查容器日志
- 使用 Podman 停止容器
- 使用 Podman 设置自定义卷
- SSH 连接到正在运行的容器
- 清理环境
Podman 是一款 Linux 原生工具,用于通过 Open Container Initiative (OCI) 容器和容器镜像来部署应用程序。它支持多种容器镜像格式,包括 Docker 镜像和 OCI 容器镜像。此外,Podman 还支持管理 pod(容器组)。
Podman 是一款无守护进程的工具,以单个二进制命令行形式运行,无需服务。它提供与 Docker 类似的命令行,你可以使用别名,例如 'alias docker=podman'。
为了运行容器和管理容器镜像,Podman 依赖 libpod 以及 OCI 容器运行时,例如 runc、crun 和 runv。
前提条件
在本指南中,你将学习如何在 Rocky Linux 系统上安装 Podman。此外,你还将学习使用 Podman 管理容器和容器镜像的基本用法。
在开始之前,请确保你已满足以下要求:
- 操作系统:Rocky Linux 8.4 (Green Obsidian)
- Root 权限:用于安装新软件包和编辑系统配置
现在让我们开始安装 Podman。
在 Rocky Linux 上安装 Podman
默认情况下,Rocky Linux 的 appstream 仓库提供 podman 软件包。
1. 执行下面的 DNF 命令以显示 podman 软件包的信息。
sudo dnf info podman
你将看到类似下面的输出。
Last metadata expiration check: 0:07:09 ago on Tue 16 Nov 2021 09:30:27 AM UTC. Available Packages Name : podman Version : 3.3.1 Release : 9.module+el8.5.0+710+4c471e88 Architecture : x86_64 Size : 12 M Source : podman-3.3.1-9.module+el8.5.0+710+4c471e88.src.rpm Repository : appstream Summary : Manage Pods, Containers and Container Images URL : https://podman.io/ License : ASL 2.0 and GPLv3+ Description : podman (Pod Manager) is a fully featured container engine that is a simple : daemonless tool. podman provides a Docker-CLI comparable command line that : eases the transition from other container engines and allows the management of : pods, containers and images. Simply put: alias docker=podman. : Most podman commands can be run as a regular user, without requiring : additional privileges. : : podman uses Buildah(1) internally to create container images. : Both tools share image (not container) storage, hence each can use or : manipulate images (but not containers) created by the other. : : Manage Pods, Containers and Container Images : podman Simple management tool for pods, containers and images
appstream 仓库提供的 Podman 软件包版本为 ' 3.3.1 '。
2. 使用下面的 DNF 命令安装 Podman。
sudo dnf install podman
输入 ' y' 确认并按 ' Enter ' 继续安装。

3. Podman 安装完成后,使用以下命令进行验证。
podman version
你将看到类似下面的输出。
Version: 3.3.1 API Version: 3.3.1 Go Version: go1.16.7 Built: Wed Nov 10 01:48:06 2021 OS/Arch: linux/amd64
Podman 重要配置
Podman 软件包附带一些默认配置,位于 '/etc/containers' 目录。
下面是一些你必须了解的 Podman 配置。
- policy.json . 镜像签名的策略配置。
- registries.conf . 可用容器镜像仓库的列表,例如 Docker Registry、RHEL Container image registry 和 Fedora Container images registry。
- storage.conf . Podman 默认存储的配置。包括驱动、位置等。
- 目录 registries.d/ . 额外的仓库配置和镜像签名
- 目录 registries.conf.d/ . 容器镜像别名的额外配置。

设置新用户
在此步骤中,你将创建一个新用户,并通过运行 Docker 镜像 ' hello-world ' 来验证 Podman 的安装。
1. 运行下面的命令以添加新用户 'johndoe'。
useradd -m -s /bin/bash johndoe
passwd johndoe
为用户 ' johndoe ' 输入新的强密码。
2. 接下来,使用以下命令以用户 'johndoe' 身份登录。
su - johndoe
3. 现在执行以下命令,基于 Docker 镜像 ' hello-world ' 运行一个新容器。
podman run hello-world
你将看到 ' Hello World ' 消息输出,这表示容器已运行,但现在已经退出。
? docker.io/library/hello-world:latest Trying to pull docker.io/library/hello-world:latest... Getting image source signatures Copying blob 2db29710123e done Copying config feb5d9fea6 done Writing manifest to image destination Storing signatures Hello from Docker! This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (amd64) 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal. To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash Share images, automate workflows, and more with a free Docker ID: https://hub.docker.com/ For more examples and ideas, visit: https://docs.docker.com/get-started/
4. 使用下面的 Podman 命令检查系统上的容器状态
podman ps -a
你将看到基于 Docker 镜像 'hello-world' 的容器,其当前状态为 'Exited'。
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES cea9bbfeac4d docker.io/library/hello-world:latest /hello 6 minutes ago Exited (0) 6 minutes ago recursing_shaw
Podman 基本用法
在此步骤中,你将学习使用 Podman 管理容器和容器镜像的基本用法。
搜索容器镜像
1. 要使用 Podman 查找容器镜像,请使用以下命令。在本示例中,我们搜索名称为 ' nginx ' 的容器镜像。
podman search nginx
你将看到来自多个仓库的大量容器镜像。
2. 你可以使用选项 '--limit n ' 限制每个容器镜像仓库的搜索结果。
podman search nginx --limit 3
现在你将看到每个仓库显示 3 条名称为 ' nginx ' 的容器镜像结果。
INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED fedoraproject.org registry.fedoraproject.org/f29/nginx 0 fedoraproject.org registry.fedoraproject.org/f29/origin-nginx-router 0 redhat.com registry.access.redhat.com/ubi8/nginx-120 Platform for running nginx 1.20 or building ... 0 redhat.com registry.access.redhat.com/ubi8/nginx-118 Platform for running nginx 1.18 or building ... 0 redhat.com registry.access.redhat.com/rhscl/nginx-18-rhel7 Nginx 1.8 server and a reverse proxy server 0 centos.org registry.centos.org/bamachrn/nginx-header 0 centos.org registry.centos.org/centos/nginx 0 centos.org registry.centos.org/centos/nginx-110-centos7 0 docker.io docker.io/library/nginx Official build of Nginx. 15839 [OK] docker.io docker.io/jwilder/nginx-proxy Automated Nginx reverse proxy for docker con... 2096 [OK] docker.io docker.io/nginxinc/nginx-unprivileged Unprivileged NGINX Dockerfiles 54
管理容器镜像
Podman 支持 OCI 和 Docker 镜像格式。在此阶段,你将使用 Podman 管理容器镜像。
1. 使用以下命令下载容器镜像 ' nginx:alpine '。
podman pull nginx:alpine
选择你要使用的容器仓库,然后按 ' Enter ' 确认。在本示例中,我们将使用 Docker 仓库。
? Please select an image: registry.fedoraproject.org/nginx:alpine registry.access.redhat.com/nginx:alpine registry.centos.org/nginx:alpine ? docker.io/library/nginx:alpine
下载过程将开始。
? docker.io/library/nginx:alpine Trying to pull docker.io/library/nginx:alpine... Getting image source signatures Copying blob a4e156412037 done Copying blob a2402c2da473 done Copying blob e0bae2ade5ec done Copying blob 97518928ae5f done Copying blob 3f3577460f48 done Copying blob e362c27513c3 done Copying config b46db85084 done Writing manifest to image destination Storing signatures b46db85084b80a87b94cc930a74105b74763d0175e14f5913ea5b07c312870f8
2. 容器镜像下载过程完成后,使用以下命令验证已下载的镜像。
podman images
下面是你将得到的类似输出。
REPOSITORY TAG IMAGE ID CREATED SIZE docker.io/library/nginx alpine b46db85084b8 3 days ago 24.7 MB docker.io/library/hello-world latest feb5d9fea6a5 7 weeks ago 19.9 kB
使用 Podman 运行容器
下载容器镜像 'nginx:alpine' 之后,你将学习如何使用 Podman 运行一个新容器。
1. 执行以下命令,基于镜像 ' nginx:alpine ' 运行容器。
podman run -it --rm -d -p 8080:80 --name web nginx:alpine
你将得到该容器的随机字符串和数字。
2. 使用以下命令验证系统上正在运行的容器。
podman ps
你将看到类似下面的输出。

名为 ' web ' 的容器状态为 ' Up ',并在主机上暴露端口 ' 8080 '。
3. 现在打开 Web 浏览器,在地址栏中输入 Rocky Linux 的 IP 地址。在本示例中,Rocky Linux 机器的 IP 地址为 ' 192.168.1.10 '。
http://192.168.1.10:8080/
你将看到容器 ' web ' 的默认 index.html,该容器基于容器镜像 ' nginx:alpine '。

使用 Podman 检查容器日志
现在你将学习使用选项 ' logs ' 检查容器日志的 Podman 命令。
1. 要检查容器的日志,你可以使用下面的 Podman 命令。
podman logs web
你将看到容器 ' web ' 的详细日志。
2. 要限制容器日志输出,请使用选项 ' --tail n '。
podman logs --tail 10 web
下面是容器 ' web ' 的类似日志输出。

使用 Podman 停止容器
1. 要停止容器 'web',请使用下面的 Podman 命令。
podman stop web
现在容器 ' web ' 将被停止。
2. 使用以下命令进行验证。
podman ps
podman ps -a
你将看到容器 'web' 被自动删除,因为你在运行容器时使用了选项 ' --rm '。

使用 Podman 设置自定义卷
在此步骤中,你将学习如何使用 Podman 管理卷。
1. 创建新目录 'data' 和自定义文件 'index.html'。这将替换 Nginx 容器上默认的 Nginx 'index.html' 文件。
mkdir -p ~/data/
nano ~/data/inde.html
复制并粘贴以下 HTML 脚本。
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>Welcome to Container Nginx</title> </head> <body> <h2>Hello from Nginx container - Managed with Podman</h2> </body> </html>
保存脚本并退出。
2. 接下来,执行以下命令以创建一个带有自定义卷的新容器。
podman run -it --rm -d -p 8080:80 --name web -v ~/data:/usr/share/nginx/html nginx:alpine
使用选项 ' -v data:/usr/share/nginx/html ' 为新容器设置自定义卷。' data ' 目录将被挂载到容器目录 ' /usr/share/nginx/html '。
3. 使用下面的 Podman 命令检查正在运行的容器。
podman ps
名为 ' web ' 的新容器状态为 ' Up ',并带有自定义卷。

4. 打开 Web 浏览器,输入带有端口 ' 8080 ' 的服务器 IP 地址。
http://192.168.1.10:8080/
现在你将看到如下自定义的 'index.html' 页面。

SSH 连接到正在运行的容器
在此步骤中,你将访问正在运行的容器的 shell。
1. 要登录到正在运行的容器,请使用带有 'exec' 选项的 Podman,如下所示。
podman exec -it web /bin/sh
2. 登录到容器后,检查容器的主机名、IP 地址和路由表。
hostname
ip a
route -n
现在输入 'exit' 并按 ' Enter ' 从容器退出登录。

清理环境
在最后一步中,你将清理你的环境。
1. 使用以下命令停止容器 ' web '。
podman stop web
2. 接下来,使用下面的 podman 命令删除所有状态为 ' Exited ' 的容器。
podman rm $(podman ps --filter "status=exited" -q)
现在你已完成使用 Podman 管理容器和容器镜像的基本用法。
结论
恭喜!你已成功在 Rocky Linux 系统上安装了 Podman。此外,你还学习了使用 Podman 管理容器和容器镜像的基本用法。
下一步,你可能会有兴趣基于 Docker 镜像格式或 OCI 容器镜像规范创建自定义容器镜像。