10.3 Podman

Podman 旨在代替 Docker,同时与 Docker 的命令几乎完全相同,你甚至能设置别名来使用。

Podman 在 FreeBSD 上的实现是基于 Jail 的,你可以用命令 jls 查看当前的容器。

安装 Podman

我们安装 sysutils/podman-suite 这个元包,会帮我们安装 sysutils/buildah、sysutils/podman 和 sysutils/skopeo。

  • 使用 pkg 安装

# pkg install podman-suite
  • 使用 Ports 安装:

# cd /usr/ports/sysutils/podman-suite/ 
# make install clean
  • 查看安装后配置:

root@ykla:~ # pkg info -D podman
podman-5.3.2_2:
On install:                              # 安装时:
The FreeBSD port of the Podman container engine is experimental and should be
used for evaluation and testing purposes only.
# Podman 容器引擎的 FreeBSD 移植版本是实验性的,仅供评估和测试使用。

$ sudo podman run --rm quay.io/dougrabson/hello

Podman can restart containers after a host is rebooted. To enable this, use:
# Podman 可以在主机重启后重新启动容器。要启用此功能,请执行:

$ sudo sysrc podman_enable=YES

and start the container with a restart policy:
# 并以重启策略启动容器:

$ sudo podman run -d --restart=always myimage

It is possible to run many Linux container images using FreeBSD's Linux emulation:
# 可以使用 FreeBSD 的 Linux 仿真功能运行许多 Linux 容器镜像:

$ sudo sysrc linux_enable=YES
$ sudo service linux start
$ sudo podman run --rm --os=linux alpine cat /etc/os-release | head -1
NAME="Alpine Linux"

On upgrade from podman<4.7.1:           # 从 podman 小于 4.7.1 的版本升级时:
In Podman-4.7.0 and later, registry authentication creds moved from
/run/containers/0/auth.json to /root/.config/containers/auth.json. Either move
the file to the new location or just re-authenticate to the required registries.
# 在 Podman 4.7.0 及更高版本中,注册表认证凭据的位置已从
# /run/containers/0/auth.json 移动到了 /root/.config/containers/auth.json。
# 可以将该文件移动到新位置,或重新登录所需的镜像注册表。

配置 fstab

将下列行写入 /etc/fstab

fdesc   /dev/fd         fdescfs         rw      0       0

然后执行下列命令使之立即生效:

# mount -t fdescfs fdesc /dev/fd

配置网络

  • 复制配置文件

# cp /usr/local/etc/containers/pf.conf.sample /etc/pf.conf
  • 编辑 /etc/pf.conf,将 ix0 替换为你当前使用的网卡,可使用命令 ifconfig 查看

v4egress_if = "ix0"
v6egress_if = "ix0"
  • 启动 pf

# kldload pf # 加载内核模块,仅需这一次,以后会自动加载
# echo 'net.pf.filter_local=1' >> /etc/sysctl.conf.local # 将容器主机上的连接重定向到容器内部运行
# sysctl net.pf.filter_local=1 # 立即生效
# service pf enable # 开启服务器
# service pf start # 启动 pf 防火墙

创建 zfs 存储池

  • 若你使用 zfs:

# zfs create -o mountpoint=/var/db/containers zroot/containers
  • 若你 使用 zfs:

# sed -I .bak -e 's/driver = "zfs"/driver = "vfs"/' /usr/local/etc/containers/storage.conf

启动服务

# service linux enable
# service linux start
# service podman enable
# service podman start

测试 Ubuntu 镜像

  • 测试拉取 Ubuntu

root@ykla:~ # 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
  • 查看当前拉取的镜像

root@ykla:~ # podman images
REPOSITORY                TAG         IMAGE ID      CREATED      SIZE
docker.io/library/ubuntu  latest      a0e45e2ce6e6  3 weeks ago  80.6 MB
  • 打印系统版本(仅打印前 5 行)

root@ykla:~ # 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
  • 进入容器

root@ykla:~ # 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
root@ykla:~ # # 回到了宿主机

测试 FreeBSD 维护者打包的 nginx 容器

# podman pull quay.io/dougrabson/nginx
# podman run -d --name mynginx -p 8080:80 quay.io/dougrabson/nginx

打开网页 http://ip:8080 即可浏览测试 nginx 页面。

更多用法

  • 查看日志

# podman logs 容器名称
  • 查看运行状态

root@ykla:~ # podman ps # 查看当前运行的容器
CONTAINER ID  IMAGE                            COMMAND               CREATEDSTATUS        PORTS                 NAMES
ca088c9c56fc  quay.io/dougrabson/nginx:latest  /usr/local/sbin/n...  3 minutes agoUp 3 minutes  0.0.0.0:8080->80/tcp  mynginx
root@ykla:~ # podman ps -a # 查看所有状态,包括运行失败的容器
CONTAINER ID  IMAGE                            COMMAND               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 镜像名称

拉取 FreeBSD

# podman pull docker://freebsd/freebsd-runtime:14.2

更多参见 freebsd/freebsd-runtime

参考文献

最后更新于