32.3 Podman 容器管理
最后更新于
# pkg install podman-suite# cd /usr/ports/sysutils/podman-suite/
# make install clean# pkg info -D podmanfdesc /dev/fd fdescfs rw 0 0# mount -t fdescfs fdesc /dev/fd# cp /usr/local/etc/containers/pf.conf.sample /etc/pf.conf# 设置 IPv4 出口网络接口
v4egress_if = "ix0"
# 设置 IPv6 出口网络接口
v6egress_if = "ix0"# Podman 容器网络 NAT 锚点与规则
nat-anchor "cni-rdr/*"
rdr-anchor "cni-rdr/*"
nat on $ext_if inet from 10.88.0.0/16 to any -> ($ext_if)# kldload pf # 加载内核模块,仅需执行一次,以后会自动加载
# echo 'net.pf.filter_local=1' >> /etc/sysctl.conf # 将容器主机的连接重定向到容器内部
# sysctl net.pf.filter_local=1 # 立即生效
# service pf enable # 启用 pf 防火墙服务
# service pf start # 启动 pf 防火墙# zfs create -o mountpoint=/var/db/containers zroot/containers# service linux enable # 设置 Linux 兼容服务开机自启
# service linux start # 启动 Linux 兼容服务
# service podman enable # 设置 Podman 服务开机自启
# service podman start # 启动 Podman 服务/
├── etc/
│ ├── fstab # 文件系统挂载配置
│ ├── pf.conf # PF 防火墙配置
│ └── sysctl.conf # sysctl 配置
├── run/
│ └── containers/
│ └── 0/
│ └── auth.json # 运行时认证凭据位置(默认读写位置,重启后可能丢失)
├── root/
│ └── .config/
│ └── containers/
│ └── auth.json # 持久化认证凭据位置(备用读取位置,重启后保留)
├── var/
│ └── db/
│ └── containers/ # 容器数据库目录
└── usr/
└── local/
└── etc/
└── containers/
└── pf.conf.sample # PF 防火墙配置示例
# podman pull --os=linux docker.io/library/ubuntu:latest
Trying to pull docker.io/library/ubuntu:latest...
Getting image source signatures
Copying blob 0622fac788ed done |
Copying config a0e45e2ce6 done |
Writing manifest to image destination
a0e45e2ce6e6e22e73185397d162a64fcf2f80a41c597015cab05d9a7b5913ce# podman images
REPOSITORYTAG IMAGE ID CREATED SIZE
docker.io/library/ubuntu latest a0e45e2ce6e6 3 weeks ago 80.6 MB# podman run --os=linux ubuntu /usr/bin/cat "/etc/os-release" | head -5
PRETTY_NAME="Ubuntu 24.04.2 LTS"
NAME="Ubuntu"
VERSION_ID="24.04"
VERSION="24.04.2 LTS (Noble Numbat)"
VERSION_CODENAME=noble# podman run -it --os=linux ubuntu /bin/bash # 进入容器
root@3b6d47dea81e:/# apt update # 当前已进入容器内部
Get:1 http://archive.ubuntu.com/ubuntu noble InRelease [256 kB]
Get:2 http://security.ubuntu.com/ubuntu noble-security InRelease [126 kB]
……以下省略……
root@3b6d47dea81e:/# exit # 退出容器
exit
# # 已返回宿主机# 从 quay.io 拉取 nginx 镜像
# podman pull quay.io/dougrabson/nginx
# 使用该镜像创建并后台运行容器 mynginx,将宿主机 8080 端口映射到容器 80 端口
# podman run -d --name mynginx -p 8080:80 quay.io/dougrabson/nginx# podman logs 容器名称# podman ps # 查看当前运行的容器
CONTAINER ID IMAGECOMMAND CREATED STATUSPORTS NAMES
ca088c9c56fc quay.io/dougrabson/nginx:latest /usr/local/sbin/n... 3 minutes agoUp 3 minutes 0.0.0.0:8080->80/tcp mynginx
# podman ps -a # 查看所有状态,包括运行失败的容器
CONTAINER ID IMAGECOMMAND CREATED STATUS PORTS NAMES
e8ea65b7e6c9 docker.io/library/nginx:latest nginx -g daemon o... 17 minutes ago Exited (0) 292 years ago 0.0.0.0:8080->80/tcp nginx-test
ca088c9c56fc quay.io/dougrabson/nginx:latest /usr/local/sbin/n... 3 minutes ago Up 3 minutes 0.0.0.0:8080->80/tcp mynginx# podman stop 容器名称 # 停止容器
# podman rm 容器名称 # 删除容器# podman rmi 镜像名称# podman pull docker://freebsd/freebsd-runtime:15.0