Podman 入门
Podman 与 Docker 的区别
Section titled “Podman 与 Docker 的区别”Podman 是 Red Hat 主导开发的容器引擎,与 Docker 的核心区别如下:
| 特性 | Podman | Docker |
|---|---|---|
| 守护进程 | 无守护进程(daemonless) | 需要 dockerd 守护进程 |
| Root 权限 | 原生支持 rootless 容器 | 需要额外配置 |
| 命令兼容 | 兼容 Docker CLI | - |
| Pod 支持 | 原生支持 Pod(类似 Kubernetes) | 不支持 |
| 系统集成 | 使用 systemd 管理容器生命周期 | 自有守护进程管理 |
| 默认可用 | RHEL/CentOS/AlmaLinux 默认仓库自带 | 需添加第三方仓库 |
安装 Podman
Section titled “安装 Podman”sudo dnf install -y podmansudo dnf module install -y container-toolssudo yum install -y podman验证安装:
podman --versionpodman info拉取与运行容器
Section titled “拉取与运行容器”# 从 Docker Hub 拉取镜像podman pull docker.io/library/nginx:latest
# 从 Quay.io 拉取镜像podman pull quay.io/centos/centos:stream9
# 查看本地镜像列表podman images# 前台运行,退出后自动删除podman run --rm -it centos:stream9 /bin/bash
# 后台运行 Nginx,映射端口 8080 到容器 80podman run -d --name my-nginx -p 8080:80 docker.io/library/nginx:latest
# 挂载本地目录到容器podman run -d --name my-web \ -p 8080:80 \ -v /srv/www:/usr/share/nginx/html:Z \ docker.io/library/nginx:latest查看容器状态
Section titled “查看容器状态”# 查看正在运行的容器podman ps
# 查看所有容器(包括已停止的)podman ps -a
# 查看容器详细信息podman inspect my-nginx
# 查看容器日志podman logs my-nginx
# 实时跟踪日志podman logs -f my-nginx停止与删除容器
Section titled “停止与删除容器”# 停止容器podman stop my-nginx
# 启动已停止的容器podman start my-nginx
# 重启容器podman restart my-nginx
# 删除已停止的容器podman rm my-nginx
# 强制删除运行中的容器podman rm -f my-nginx
# 删除所有已停止的容器podman container prune进入运行中的容器
Section titled “进入运行中的容器”# 以交互式终端进入容器podman exec -it my-nginx /bin/bash
# 在容器中执行单条命令podman exec my-nginx cat /etc/nginx/nginx.conf编写 Containerfile
Section titled “编写 Containerfile”创建一个 Containerfile(等同于 Dockerfile):
FROM docker.io/library/almalinux:9-minimal
# 安装应用RUN microdnf install -y httpd && microdnf clean all
# 复制配置和网页文件COPY index.html /var/www/html/index.htmlCOPY httpd.conf /etc/httpd/conf/httpd.conf
# 开放端口EXPOSE 80
# 启动命令CMD ["/usr/sbin/httpd", "-D", "FOREGROUND"]# 构建镜像,-t 指定名称和标签podman build -t my-httpd:v1 .
# 查看构建好的镜像podman images
# 使用自建镜像运行容器podman run -d --name web -p 8080:80 my-httpd:v1导出与导入镜像
Section titled “导出与导入镜像”# 将镜像保存为 tar 文件podman save -o my-httpd-v1.tar my-httpd:v1
# 从 tar 文件加载镜像podman load -i my-httpd-v1.tar使用 podman-compose
Section titled “使用 podman-compose”podman-compose 是 Docker Compose 的兼容替代工具。
安装 podman-compose
Section titled “安装 podman-compose”sudo dnf install -y python3-pippip3 install podman-compose编写 compose 文件
Section titled “编写 compose 文件”创建 docker-compose.yml:
version: "3"services: web: image: docker.io/library/nginx:latest ports: - "8080:80" volumes: - ./html:/usr/share/nginx/html:Z depends_on: - app
app: image: docker.io/library/python:3.11-slim working_dir: /app volumes: - ./app:/app:Z command: python3 -m http.server 5000 ports: - "5000:5000"
db: image: docker.io/library/mariadb:10.11 environment: MYSQL_ROOT_PASSWORD: changeme MYSQL_DATABASE: myapp volumes: - db_data:/var/lib/mysql
volumes: db_data:# 启动所有服务(后台运行)podman-compose up -d
# 查看服务状态podman-compose ps
# 查看日志podman-compose logs -f
# 停止并删除所有服务podman-compose downRootless 容器
Section titled “Rootless 容器”Rootless 容器是 Podman 的核心优势之一,允许普通用户无需 root 权限即可运行容器。
配置 rootless 环境
Section titled “配置 rootless 环境”-
确认用户的 UID 映射已配置:
Terminal window cat /etc/subuidcat /etc/subgid如果当前用户没有条目,手动添加:
Terminal window sudo usermod --add-subuids 100000-165535 $(whoami)sudo usermod --add-subgids 100000-165535 $(whoami) -
以普通用户身份运行容器:
Terminal window podman run -d --name rootless-nginx -p 8080:80 docker.io/library/nginx:latest -
验证容器进程归属:
Terminal window ps aux | grep nginxpodman top rootless-nginx
使用 systemd 管理 rootless 容器
Section titled “使用 systemd 管理 rootless 容器”Podman 可以生成 systemd 用户服务单元,使容器在用户登录时自动启动:
# 为已有容器生成 systemd 单元文件podman generate systemd --name rootless-nginx --files --new
# 将生成的文件移到用户 systemd 目录mkdir -p ~/.config/systemd/user/mv container-rootless-nginx.service ~/.config/systemd/user/
# 重新加载并启用服务systemctl --user daemon-reloadsystemctl --user enable --now container-rootless-nginx.service
# 允许用户服务在用户退出登录后继续运行loginctl enable-linger $(whoami)查看服务状态:
systemctl --user status container-rootless-nginx.serviceQuadlet:EL 9+ 推荐方式
Section titled “Quadlet:EL 9+ 推荐方式”Quadlet 文件放置位置:
- 系统级(root):
/etc/containers/systemd/ - 用户级(rootless):
~/.config/containers/systemd/
创建一个 .container 文件描述容器:
[Unit]Description=Nginx (Quadlet 管理)After=network-online.target
[Container]Image=docker.io/library/nginx:latestPublishPort=8080:80# 卷挂载会自动加 SELinux 标签,相当于 :ZVolume=%h/nginx/html:/usr/share/nginx/html:Z
[Service]Restart=always
[Install]# 开机(用户登录)自动启动WantedBy=default.target应用配置:
# 让 systemd 重新读取并生成对应的 nginx.servicesystemctl --user daemon-reloadsystemctl --user start nginx.service
# 查看状态(注意服务名是文件名 .container -> .service)systemctl --user status nginx.service
# rootless 容器在用户退出后继续运行loginctl enable-linger $(whoami)修改容器配置时,只需编辑 .container 文件并 daemon-reload,无需像 podman generate systemd 那样重新生成单元。系统级单元把文件放到 /etc/containers/systemd/ 并改用 systemctl(不带 --user)即可。
Podman Pod 管理
Section titled “Podman Pod 管理”Pod 是 Podman 的独特功能,类似于 Kubernetes 的 Pod 概念,多个容器共享同一个网络命名空间。
# 创建一个 Pod,映射端口podman pod create --name my-pod -p 8080:80 -p 3306:3306
# 在 Pod 中运行 Nginxpodman run -d --pod my-pod --name pod-nginx docker.io/library/nginx:latest
# 在同一 Pod 中运行 MariaDBpodman run -d --pod my-pod --name pod-db \ -e MYSQL_ROOT_PASSWORD=changeme \ docker.io/library/mariadb:10.11
# 查看 Pod 状态podman pod pspodman pod inspect my-pod
# 停止和删除 Pod(会同时操作 Pod 内所有容器)podman pod stop my-podpodman pod rm my-pod常用维护命令
Section titled “常用维护命令”# 清理未使用的镜像podman image prune
# 清理所有未使用的资源(镜像、容器、卷)podman system prune -a
# 查看磁盘使用情况podman system df
# 查看容器资源占用podman statsEL 10 注意事项:Podman 5
Section titled “EL 10 注意事项:Podman 5”EL 10 将 Podman 从 4.x 升级到了 Podman 5.x,主要变化:
| 特性 | Podman 4.x(EL 9) | Podman 5.x(EL 10) |
|---|---|---|
| Rootless 网络后端 | slirp4netns | pasta(默认) |
| 性能 | 基准 | 网络性能更好 |
| API 兼容 | - | 与 Podman 4.x 基本兼容 |
Rootless 网络后端变化
EL 10 上 rootless 容器默认使用 pasta 作为网络后端(替代 slirp4netns),通常性能更好且无需额外配置。如有需要可手动切换:
sudo dnf install slirp4netnspodman --versionpodman info | grep -i network