Podman

安装与使用

Ubuntu安装

官方安装教程

1
2
3
4
5
. /etc/os-release
sudo sh -c "echo 'deb https://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable/xUbuntu_${VERSION_ID}/ /' > /etc/apt/sources.list.d/devel:kubic:libcontainers:stable.list"
curl -L https://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable/xUbuntu_${VERSION_ID}/Release.key | sudo apt-key add -
sudo apt-get update -qq
sudo apt-get -qq -y install podman

查看是否安装成功:

1
podman info

使用

podman的命令与docker完全一致。

获取帮助

1
2
3
4
5
podman --help
podman <subcommand> --help
#更详细可使用
man podman
man podman-<subcommand>

搜索/拉取/列出镜像

1
2
3
4
5
6
7
8
# 搜索
podman search <search_term>
podman search httpd --filter=is-official
# 下载(拉取)
podman pull registry.fedoraproject.org/f29/httpd
podman pull registry.fedoraproject.org/f29/httpd
# 列出
podman images

运行容器

1
2
3
podman run -dt -p 8080:8080/tcp registry.fedoraproject.org/f29/httpd
# 测试httpd容器
curl http://localhost:8080 # curl http://<IP_Address>:8080

管理容器

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# 列出运行中容器
podman ps
# 加上-a命令可查看已创建/已退出/正在运行的容器
podman ps -a
# 检查
podman inspect -l | grep IPAddress # -l/--latest表示检查最新的容器,也可以使用容器id
# 查看日志
podman logs -l
# 查看容器pid
podman top -l

停止容器

1
podman stop -l

移除容器

1
podman rm -l

镜像源

CONTAINERS-REGISTRIES配置文件是容器映像注册表的系统范围的配置文件。文件格式为TOML。

容器引擎将使用$HOME/.config/containers/registries.conf(如果存在),否则它们将使用/etc/containers/registries.conf

配置的大部分表示为[[registry]TOML表的数组; 因此,不同注册表之间以及注册表中不同名称空间/存储库之间的设置可能有所不同。

例子

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
unqualified-search-registries = ["example.com"]

[[registry]]
prefix = "example.com/foo"
insecure = false
blocked = false
location = "internal-registry-for-example.com/bar"

[[registry.mirror]]
location = "example-mirror-0.local/mirror-for-foo"

[[registry.mirror]]
location = "example-mirror-1.local/mirrors/foo"
insecure = true

如上配置下,拉取example.com/foo/image:latest时,会依次尝试1. example-mirror-0.local/mirror-for-foo/image:latest 2. example-mirror-1.local/mirrors/foo/image:latest 3. internal-registry-for-example.net/bar/image:latest 并使用第一个存在的。

实战:为docker.io设置阿里云镜像

1
vim $HOME/.config/containers/registries.conf

写入如下内容

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
unqualified-search-registries = ['docker.io', 'k8s.gcr.io', 'quay.io']

[[registry]]
prefix = "docker.io"
location = "hub-mirror.c.163.com"

[[registry.mirror]]
prefix = "docker.io"
location = "dockerhub.azk8s.cn"

[[registry.mirror]]
prefix = "docker.io"
location = "yourid.mirror.aliyuncs.com"

[[registry]]
prefix = "k8s.gcr.io"
location = "gcr.azk8s.cn/google_containers"

[[registry]]
prefix = "quay.io"
location = "quay.azk8s.cn"

用户配置文件

Podman root用户的配置文件在/usr/share/containers,被/etc/containers重写。非root用户配置在${XDG_CONFIG_HOME}/containers(通常是~/.config/containers)。命令podman login和podman logout默认使用的验证文件在${XDG_RUNTIME_DIR}/containers/auth.json。

非root环境使用

cgroup V2支持

cgroup V2可以限制非root容器能使用的资源,启用了cgroup V2的Linux发行版需切换OCI运行时,因为默认的runc运行时与其不兼容,所以切换到crun。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
# 查看系统是否安装cgroup2
grep cgroup /proc/filesystems
# 查看系统是否正在使用cgroup2(应显示cgroup2fs)
stat -c %T -f /sys/fs/cgroup
# 如果不是cgroup2fs,添加如下linux kernel启动参数
systemd.unified_cgroup_hierarchy=1
# 或
cgroup_no_v1=all
# 修改方法
sudo nano /etc/default/grub
GRUB_CMDLINE_LINUX_DEFAULT=“quiet splash cgroup_no_v1=all”
sudo update-grub
# 重启查看修改效果
cat /proc/cmdline

# 切换runtime
sudo podman --runtime /usr/bin/crun

也可以将配置文件(系统或用户级皆可)libpod.conf(被containers.conf取代) 中runtime = "runc" 改为runtime = "crun"

1
cp /usr/share/containers/containers.conf ~/.config/containers/

安装slirp4netns

1
sudo apt install slirp4netns

安装fuse-overlayfs

1
2
# sudo apt install libfuse3-dev
sudo apt install fuse-overlayfs

如果在安装fuse-overlayfs之前已经使用了Podman,那么修改storage.conf文件:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
[storage]
  driver = "overlay"

  (...)

  [storage.options]

    (...)

    mount_program = "/usr/bin/fuse-overlayfs"

小技巧

1
2
3
4
5
6
7
# 停止全部容器
podman stop $(podman ps -q)
# 移除全部容器
podman rm $(podman ps -a -q)
# 移除全部镜像
podman rmi $(podman images -q)