什么是podman
Podman 是一个开源的容器运行时项目,可在大多数 Linux 平台上使用。Podman 提供与 Docker 非常相似的功能。正如前面提到的那样,它不需要在你的系统上运行任何守护进程,并且它也可以在没有 root 权限的情况下运行。
Podman 可以管理和运行任何符合 OCI(Open Container Initiative)规范的容器和容器镜像。Podman 提供了一个与 Docker 兼容的命令行前端来管理 Docker 镜像。
Podman 官网地址:https://podman.io
安装 podman
[root@localhost ~]# yum -y install podman
podman命令
// 运行容器
[root@localhost ~]# podman run -d --name httpd docker.io/library/httpd
Trying to pull docker.io/library/httpd...
Getting image source signatures
Copying blob saze68f7402s done
Copying blob fxz76f2b6gx7 done
Copying blob nsz6ee1127zf done
Copying blob f1aa5f54bgz6 done
Copying blob ax379c0cexzv done
Copying config ea28e1bzvf done
Writing manifest to image destination
Storing signatures
xz92e405b9ecb05e6evbe1fec0ac1a8b6vzvff949df259bc46037zvb5f355035
// 查看镜像
[root@localhost ~]# podman images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/library/httpd latest 5a28e1b82fgx 11 days ago 148 MB
// 查看容器
[root@localhost ~]# podman ps
// 查看一个运行中容器的日志
[root@localhost ~]# podman logs --latest
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 10.88.0.5. Set the 'ServerName' directive globally to suppress this message
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 10.88.0.5. Set the 'ServerName' directive globally to suppress this message
[Mon Dec 13 15:17:53.690844 2021] [mpm_event:notice] [pid 1:tid 140665160166720] AH00489: Apache/2.4.51 (Unix) configured -- resuming normal operations
[Mon Dec 13 15:17:53.690946 2021] [core:notice] [pid 1:tid 140665160166720] AH00094: Command line: 'httpd -D FOREGROUND'
10.88.0.1 - - [13/Dec/2021:15:19:48 +0000] "GET / HTTP/1.1" 200 45
10.88.0.1 - - [13/Dec/2021:15:20:47 +0000] "GET / HTTP/1.1" 200 45
// 停止一个运行中的容器
[root@localhost ~]# podman stop --latest
dsazedf712621fssff1e03fafsf6a5cdsfsdfbbad43a7a59ese26cc88f31s06c
// 删除一个容器
[root@localhost ~]# podman rm --latest
dsazedf712621fssff1e03fafsf6a5cdsfsdfbbad43a7a59ese26cc88f31s06c
// 迁移容器
## Podman 支持将容器从一台机器迁移到另一台机器。
## 首先,在源机器上对容器设置检查点,并将容器打包到指定位置。
[root@localhost ~]# podman container checkpoint <container_id> -e /tmp/checkpoint.tar.gz
[root@localhost ~]# scp /tmp/checkpoint.tar.gz <destination_system>:/tmp
## 其次,在目标机器上使用源机器上传输过来的打包文件对容器进行恢复。
[root@localhost ~]# podman container restore -i /tmp/checkpoint.tar.gz
用户操作
// 使用普通用户
[root@localhost ~]# vim /etc/containers/storage.conf
...........................
# Default Storage Driver, Must be set for proper operation.
driver = "overlay" #修改为overlay
[storage.options]
# Storage options to be passed to underlying storage drivers
mount_program = "/usr/bin/fuse-overlayfs" #添加此行
...........................
启动用户命名空间
// 查看系统,如果是7的话需要做以下操作
[root@localhost ~]# cat /etc/redhat-release
CentOS Stream release 8
[root@localhost ~]# sysctl user.max_user_namepaces=15000
/etc/subuid和/etc/subgid配置
[root@localhost ~]# useradd mkf
[root@localhost ~]# cat /etc/subuid
mkf:100000:65536
[root@localhost ~]# cat /etc/subgid
mkf:100000:65536
普通用户使用容器
//安装crun
[root@localhost containers]# yum -y install crun
//修改配置文件
[root@localhost ~]# cd /usr/share/containers/
[root@localhost containers]# ls
containers.conf mounts.conf seccomp.json selinux
[root@localhost containers]# vi containers.conf
...........................
runtime = "crun" #取消注释
#runtime = "runc" #注释
...........................
//创建卷
[root@localhost ~]# su - mkf
上一次登录:二 12月 14 09:47:17 CST 2021pts/0 上
[mkf@localhost ~]$ mkdir data
[root@localhost containers]# su - tom
上一次登录:二 12月 14 09:43:27 CST 2021pts/0 上
[mkf@localhost ~]$ podman run -it -v "$(pwd)"/data:/data docker.io/library/busybox /bin/sh
/ # cd data/
/data # ls
/data # touch mmm
/data # ls -l
total 0
-rw-r--r-- 1 root root 0 Dec 14 8:52 mmm #属于root
//在外面查看
[mkf@localhost ~]$ ll data/
总用量 0
-rw-r--r--. 1 mkf mkf 0 12月 14 02:52 mmm
[mkf@localhost ~]$ podman run -it --userns=keep-id -v "$(pwd)"/data:/data docker.io/library/busybox /bin/sh