文章目录
1 podman简介
-
Podman是一个开源项目,可在大多数Linux平台上使用并开源在GitHub上。Podman是一个无守护进程的容器引擎,用于在Linux系统上开发,管理和运行Open Container Initiative(OCI)容器和容器镜像。Podman提供了一个与Docker兼容的命令行前端,它可以简单地作为Docker cli,简单地说你可以直接添加别名:alias docker = podman来使用podman。
-
Podman控制下的容器可以由root用户运行,也可以由非特权用户运行。Podman管理整个容器的生态系统,其包括pod,容器,容器镜像,和使用libpod library的容器卷。Podman专注于帮助您维护和修改OCI容器镜像的所有命令和功能,例如拉取和标记。它允许您在生产环境中创建,运行和维护从这些映像创建的容器。
2 Podman工作机制
Podman 原来是 CRI-O 项目的一部分,后来被分离成一个单独的项目叫 libpod。Podman 的使用体验和 Docker 类似,不同的是 Podman 没有 daemon。以前使用 Docker CLI 的时候,Docker CLI 会通过 gRPC API 去跟 Docker Engine 说「我要启动一个容器」,然后 Docker Engine 才会通过 OCI Container runtime(默认是 runc)来启动一个容器。这就意味着容器的进程不可能是 Docker CLI 的子进程,而是 Docker Engine 的子进程。
Podman 比较简单粗暴,它不使用 Daemon,而是直接通过 OCI runtime(默认也是 runc)来启动容器,所以容器的进程是 podman 的子进程。这比较像 Linux 的 fork/exec 模型,而 Docker 采用的是 C/S(客户端/服务器)模型。与 C/S 模型相比,fork/exec 模型有很多优势,比如:
- 系统管理员可以知道某个容器进程到底是谁启动的。
- 如果利用 cgroup 对 podman 做一些限制,那么所有创建的容器都会被限制。
- SD_NOTIFY : 如果将 podman 命令放入 systemd 单元文件中,容器进程可以通过 podman返回通知,表明服务已准备好接收任务。
- socket 激活 : 可以将连接的 socket 从 systemd 传递到 podman,并传递到容器进程以便使用它们。
3 Podman与Docker的区别
podman(Pod Manager)是一个由RedHat公司推出的容器管理工具,它的定位就是docker的替代品,在使用上与docker的体验类似。podman源于CRI-O项目,可以直接访问OCI的实现(如runC),流程比docker要短
二者主要的区别在于,podman是一个开源的产品;而docker已经是商业化的产品
- podman的定位也是与docker兼容,因此在使用上面尽量靠近docker。在使用方面,可以分成两个方面来说,一是系统构建者的角度,二是使用者的角度。
- 在系统构建者方面,用podman的默认软件,与docker的区别不大,只是在进程模型、进程关系方面有所区别。如果习惯了docker几个关联进程的调试方法,在podman中则需要适应。可以通过pstree命令查看进程的树状结构。总体来看,podman比docker要简单。由于podman比docker少了一层daemon,因此重启的机制也就不同了。
- 在使用者方面,podman与docker的命令基本兼容,都包括容器运行时(run/start/kill/ps/inspect),本地镜像(images/rmi/build)、镜像仓库(login/pull/push)等几个方面。
- 因此podman的命令行工具与docker类似,比如构建镜像、启停容器等。甚至可以通过
alias docker=podman
可以进行替换。因此,即便使用了podman,仍然可以使用docker.io作为镜像仓库,这也是兼容性最关键的部分。 - podman相比docker也缺失了一些功能,比如不支持windows,不支持docker-compoese编排工具。显然在Kubernetes或者OpenShift体系中,这些并不重要
4 podman安装
//centos8系统中自带podmam包
[root@localhost ~]# yum -y install podman
[root@localhost ~]# rpm -qa |grep podman
podman-catatonit-3.4.1-3.module_el8.6.0+954+963caf36.x86_64
podman-3.4.1-3.module_el8.6.0+954+963caf36.x86_64
[root@localhost ~]#
[root@localhost ~]# podman version
Version: 3.4.1-dev
API Version: 3.4.1-dev
Go Version: go1.16.7
Built: Wed Oct 20 00:11:42 2021
OS/Arch: linux/amd64
[root@localhost ~]#
//使用此命令后podman命令就可以docker命令做一个替换使用
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
[root@localhost ~]# podman images
REPOSITORY TAG IMAGE ID CREATED SIZE
[root@localhost ~]#
4.1 podman info(显示整个系统的信息)
要获得一些帮助并了解Podman的工作原理,您可以查看帮助文档
$ podman --help
$ podman <subcommand> --help
//有关更多详细信息,您可以查看手册
$ man podman
$ man podman-<subcommand>
5 Podman常用命令
5.1 podman version(查看版本号)
[root@localhost ~]# podman info
host:
arch: amd64
buildahVersion: 1.23.1
cgroupControllers:
- cpuset
- cpu
- cpuacct
- blkio
- memory
- devices
- freezer
- net_cls
- perf_event
- net_prio
- hugetlb
- pids
- rdma
cgroupManager: systemd
cgroupVersion: v1
conmon:
package: conmon-2.0.30-1.module_el8.6.0+944+d413f95e.x86_64
path: /usr/bin/conmon
version: 'conmon version 2.0.30, commit: e28f6ed9f4a6f18e27f3efdab92de483806e6b9c'
cpus: 4
distribution:
distribution: '"centos"'
version: "8"
eventLogger: file
hostname: localhost.localdomain
idMappings:
gidmap: null
uidmap: null
kernel: 4.18.0-257.el8.x86_64
linkmode: dynamic
logDriver: journald
memFree: 6762815488
memTotal: 8116858880
5.2 podman search(查找镜像)
// --filter=is-official:指定查找官方版本的镜像
[root@localhost ~]# podman search nginx --filter=is-official
INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED
docker.io docker.io/library/nginx Official build of Nginx. 15928 [OK]
5.3 podman pull (拉取镜像)
Podman在不同的镜像管理机构中搜索。因此,建议使用完整的镜像名称来确保拉取的是你要使用的映像
[root@localhost ~]# podman pull docker.io/library/nginx
Trying to pull docker.io/library/nginx:latest...
Getting image source signatures
Copying blob 77700c52c969 done
Copying blob 881ff011f1c9 done
Copying blob ed835de16acd done
Copying blob e5ae68f74026 done
Copying blob 21e0df283cd6 done
Writing manifest to image destination
Storing signatures
f652ca386ed135a4cbe356333e08ef0816f81b2ac8d0619af01e2b256837ed3e
//查看镜像
[root@podman ~]# podman images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/library/nginx latest f652ca386ed1 11 days ago 146 MB
5.4 podman run(运行容器)
[root@localhost ~]# podman run -dt -p 80:80/tcp docker.io/library/nginx
d3615a2cbe4195eba02009dc2f3dd229b6846b97a886a84a0070e52dda6d7250
[root@localhost ~]# podman ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d3615a2cbe41 docker.io/library/nginx:latest nginx -g daemon o... 26 seconds ago Up 25 seconds ago 0.0.0.0:80->80/tcp quizzical_poitras
[root@localhost ~]#
访问测试
[root@localhost ~]# curl 10.88.0.1
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@localhost ~]#
5.5 podman inspect(查看容器详细信息)
[root@localhost ~]# podman ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d3615a2cbe41 docker.io/library/nginx:latest nginx -g daemon o... 5 minutes ago Up 5 minutes ago 0.0.0.0:80->80/tcp quizzical_poitras
[root@localhost ~]# podman inspect d3615a2cbe41
[
{
"Id": "d3615a2cbe4195eba02009dc2f3dd229b6846b97a886a84a0070e52dda6d7250",
"Created": "2021-12-14T21:42:41.829845932+08:00",
"Path": "/docker-entrypoint.sh",
"Args": [
"nginx",
"-g",
"daemon off;"
],
"State": {
"OciVersion": "1.0.2-dev",
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 18567,
"ConmonPid": 18555,
"ExitCode": 0,
"Error": "",
"StartedAt": "2021-12-14T21:42:42.445934165+08:00",
"FinishedAt": "0001-01-01T00:00:00Z",
"Healthcheck": {
"Status": "",
"FailingStreak": 0,
"Log": null
}
},
......
5.6 podman logs(查看容器日志)
//这里-l是最新创建的容器的意思,不然需要指定一个容器
[root@localhost ~]# podman logs -l
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2021/12/14 13:42:42 [notice] 1#1: using the "epoll" event method
2021/12/14 13:42:42 [notice] 1#1: nginx/1.21.4
2021/12/14 13:42:42 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
2021/12/14 13:42:42 [notice] 1#1: OS: Linux 4.18.0-257.el8.x86_64
2021/12/14 13:42:42 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2021/12/14 13:42:42 [notice] 1#1: start worker processes
2021/12/14 13:42:42 [notice] 1#1: start worker process 32
2021/12/14 13:42:42 [notice] 1#1: start worker process 33
2021/12/14 13:42:42 [notice] 1#1: start worker process 34
2021/12/14 13:42:42 [notice] 1#1: start worker process 35
10.88.0.1 - - [14/Dec/2021:13:47:29 +0000] "GET / HTTP/1.1" 200 615 "-" "curl/7.61.1" "-"
[root@localhost ~]#
5.7 podman top(查看容器的 pids)
[root@localhost ~]# podman top -l
USER PID PPID %CPU ELAPSED TTY TIME COMMAND
root 1 0 0.000 25m18.250753839s pts/0 0s nginx: master process nginx -g daemon off;
nginx 32 1 0.000 25m18.250893221s pts/0 0s nginx: worker process
nginx 33 1 0.000 25m18.250934625s pts/0 0s nginx: worker process
nginx 34 1 0.000 25m18.250972605s pts/0 0s nginx: worker process
nginx 35 1 0.000 25m18.251008944s pts/0 0s nginx: worker process
[root@localhost ~]#
5.8 podman status(查看运行中容器资源使用情况)
//这里-l指的也是最新创建的容器
[root@localhost ~]# podman stats -l
ID NAME CPU % MEM USAGE / LIMIT MEM % NET IO BLOCK IO PIDS CPU TIME AVG CPU %
6a6dcac5ce50 nginx 3.21% 16.22MB / 8.117GB 0.20% 698B / 1.68kB -- / -- 5 52.565788ms 3.21%
5.9 podman push(上传镜像)
[root@localhost ~]# podman login docker.io
Username: caiaoc
Password:
Login Succeeded!
[root@localhost ~]# cat /run/user/0/containers/auth.json
{
"auths": {
"docker.io": {
"auth": "Y2FpYW9jOmNubTE0NzI1OA=="
}
}
}[root@localhost ~]# podman tag docker.io/library/nginx:latest caiaoc/podman:nginx //修改镜像标签
[root@localhost ~]# podman push caiaoc/podman:nginx
6 普通用户使用podman的方式
6.1 普通用户使用的配置
在允许没有root特权的用户运行Podman之前,管理员必须安装或构建Podman并完成以下配置
- cgroup V2Linux内核功能允许用户限制普通用户容器可以使用的资源,如果使用cgroup V2启用了运行Podman的Linux发行版,则可能需要更改默认的OCI运行时。某些较旧的版本runc不适用于cgroup V2,必须切换到备用OCI运行时crun。
[root@localhost ~]# yum -y install crun
[root@localhost ~]# vim /usr/share/containers/containers.conf
......
#
#remote = false
# Default OCI runtime
#
runtime = "crun" //取消注释并将值改为crun
6.2 安装slirp4netns
slirp4netns软件包为非特权网络命名空间提供用户模式网络,并且必须安装在机器上才能使 Podman 在无根环境中运行
//默认在安装podman时已经安装
[root@localhost ~]# yum -y install slirp4netns
上次元数据过期检查:2:07:10 前,执行于 2021年12月14日 星期二 20时14分00秒。
软件包 slirp4netns-1.1.8-1.module_el8.6.0+926+8bef8ae7.x86_64 已安装。
依赖关系解决。
无需任何处理。
完毕!
[root@localhost ~]#
6.3 安装fuse-overlayfs
在普通用户环境中使用Podman时,建议使用fuse-overlayfs而不是VFS文件系统,至少需要版本0.7.6。现在新版本默认就是了
[root@localhost ~]# yum -y install fuse-overlayfs
上次元数据过期检查:2:08:51 前,执行于 2021年12月14日 星期二 20时14分00秒。
软件包 fuse-overlayfs-1.7.1-1.module_el8.6.0+926+8bef8ae7.x86_64 已安装。
依赖关系解决。
无需任何处理。
完毕!
[root@localhost ~]# yum -y install fuse-overlayfs
上次元数据过期检查:2:09:08 前,执行于 2021年12月14日 星期二 20时14分00秒。
软件包 fuse-overlayfs-1.7.1-1.module_el8.6.0+926+8bef8ae7.x86_64 已安装。
依赖关系解决。
无需任何处理。
完毕!
[root@localhost ~]# vim /etc/containers/storage.conf
......
# Inodes is used to set a maximum inodes of the container image.
# inodes = ""
# Path to an helper program to use for mounting the file system instead of mounting it
# directly.
mount_program = "/usr/bin/fuse-overlayfs" //取消此行注释
......
6.4 /etc/subuid和/etc/subgid配置
Podman要求运行它的用户在/etc/subuid和/etc/subgid文件中列出一系列UID,shadow-utils提供这些文件
该文件的格式为USERNAME:UID:RANGE
- 在/ etc / passwd或getpwent中列出的用户名。
- 为用户分配的初始uid。
- 为用户分配的UID范围的大小
[root@localhost ~]# yum -y install shadow-utils
//我们启动一个容器,可以在/etc/subuid和/etc/subgid查看,每个用户的值必须唯一且没有任何重叠
[root@localhost ~]# podman exec -it d3615a2cbe41 /bin/bash
root@d3615a2cbe41:/# id
uid=0(root) gid=0(root) groups=0(root)
root@d3615a2cbe41:/# useradd tom
root@d3615a2cbe41:/# cat /etc/subuid
tom:100000:65536
root@d3615a2cbe41:/# useradd jerry
root@d3615a2cbe41:/# cat /etc/subuid
tom:100000:65536
jerry:165536:65536
root@d3615a2cbe41:/#
6.5 用户的配置文件
三个主要的配置文件是container.conf
,storage.conf
和registries.conf
。用户可以根据需要修改这些文件
6.5.1 container.conf
Podman读取时,按照循序来了,当前面一位找不到时,就去找下一个
1./usr/share/containers/containers.conf
2./etc/containers/containers.conf
3.$HOME/.config/containers/containers.conf
6.5.2 storage.conf
podman保存在本地的容器镜像和其运行容器的文件目录。
1./etc/containers/storage.conf
2.$HOME/.config/containers/storage.conf
在普通用户中/etc/containers/storage.conf的一些字段将被忽略
graphroot=``""`` ``container storage graph ``dir` `(default: ``"/var/lib/containers/storage"``)`` ``Default directory to store all writable content created by container storage programs.` `runroot=``""`` ``container storage run ``dir` `(default: ``"/run/containers/storage"``)`` ``Default directory to store all temporary writable content created by container storage programs.
在普通用户中这些字段默认
graphroot=``"$HOME/.local/share/containers/storage"``runroot=``"$XDG_RUNTIME_DIR/containers"
6.5.3 registries.conf
配置按此顺序读入,这些文件不是默认创建的,可以从/usr/share/containers或复制文件/etc/containers并进行修改
1./etc/containers/registries.conf
2./etc/containers/registries.d/*
3.HOME/.config/containers/registries.conf
7 podman 网络配置
- rootfull和rootless容器网络之间的差异
- podman容器联网的指导因素之一将是容器是否由root用户运行。这是因为非特权用户无法在主机上创建网络接口。
- 因此,对于rootfull容器,默认网络模式是使用容器网络接口(CNI)插件,特别是桥接插件。对于rootless,默认的网络模式是slir4netns。由于权限有限,slirnetns缺少CNI组网的一些功能;例如,slirp4netns无法为容器提供可路由的IP地址。cni是容器网络接口。
容器网络配置三部曲
- podman network create 创建网络
- 修改 /etc/cni/net.d/mynetwork.conflist 配置文件
- 修改设置/usr/share/containers/containers.conf 将其为默认网络
7.1 创建网络
[root@localhost ~]# podman network create new
/etc/cni/net.d/new.conflist
[root@localhost ~]# podman network ls
NETWORK ID NAME VERSION PLUGINS
2f259bab93aa podman 0.4.0 bridge,portmap,firewall,tuning
11507a0e2f5e new 0.4.0 bridge,portmap,firewall,tuning
[root@localhost ~]#
7.2 修改/etc/cni/net.d/mynetwork.conflist 配置文件
[root@localhost ~]# cat /etc/cni/net.d/new.conflist
{
"cniVersion": "0.4.0", //cni 版本
"name": "new", //网络名
"plugins": [
{
"type": "bridge", //类型
"bridge": "cni-podman1",
"isGateway": true,
"ipMasq": true,
"hairpinMode": true,
"ipam": {
"type": "host-local",
"routes": [
{
"dst": "0.0.0.0/0"
}
],
"ranges": [
[
{
"subnet": "192.168.200.0/24", //修改网段
"gateway": "192.168.200.1" //修改网关
}
]
]
}
},
{
"type": "portmap",
"capabilities": {
"portMappings": true
}
},
{
"type": "firewall",
"backend": ""
},
{
"type": "tuning"
}
]
}
[root@localhost ~]#
7.3 修改/usr/share/containers/containers.conf 将其为默认网络
[root@localhost ~]# vim /usr/share/containers/containers.conf mnb mnbh hjmn hmnb
......
# The network name of the default CNI network to attach pods to.
#
default_network = "new" //添加此行
#default_network = "podman"
......
创建容器测试
[root@localhost ~]# podman run -dit --name test1 docker.io/library/busybox
bb14fdedb700ca65bdfd204628545de9eb6bb8aee6af2d7b957c28a2893fcbe7
[root@localhost ~]# podman exec -it test1 /bin/sh
/ # ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0@if7: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue
link/ether 4e:22:e4:d3:0f:18 brd ff:ff:ff:ff:ff:ff
inet 192.168.10.3/24 brd 192.168.10.255 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::4c22:e4ff:fed3:f18/64 scope link
valid_lft forever preferred_lft forever
/ #
8 podman容器的开机自启
8.1 root模式的podman
//创建一个nginx容器
[root@localhost ~]# podman run -dit --name nginx nginx
c58ca7266bdb646c535b0c3a3ad8f58f7793f6ddf838ca3475a06ff685b04b2f
[root@localhost ~]# podman ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1933fec22a49 docker.io/library/httpd:latest httpd-foreground 3 minutes ago Up 3 minutes ago test
c58ca7266bdb docker.io/library/nginx:latest nginx -g daemon o... 5 seconds ago Up 5 seconds ago nginx
//生成service文件
[root@localhost ~]# podman generate systemd --files --name nginx
/root/container-nginx.service
[root@localhost ~]# ls
anaconda-ks.cfg container-nginx.service
[root@localhost ~]# cat container-nginx.service
# container-nginx.service
# autogenerated by Podman 3.4.1-dev
# Wed Dec 15 22:59:46 CST 2021
[Unit]
Description=Podman container-nginx.service
Documentation=man:podman-generate-systemd(1)
Wants=network-online.target
After=network-online.target
RequiresMountsFor=/run/containers/storage
[Service]
Environment=PODMAN_SYSTEMD_UNIT=%n
Restart=on-failure
TimeoutStopSec=70
ExecStart=/usr/bin/podman start nginx
ExecStop=/usr/bin/podman stop -t 10 nginx
ExecStopPost=/usr/bin/podman stop -t 10 nginx
PIDFile=/run/containers/storage/overlay-containers/c58ca7266bdb646c535b0c3a3ad8f58f7793f6ddf838ca3475a06ff685b04b2f/userdata/conmon.pid
Type=forking
[Install]
WantedBy=multi-user.target default.target
[root@localhost ~]#
//将容器的service文件移动到/usr/lib/systemd/system下
[root@localhost ~]# mv container-nginx.service /usr/lib/systemd/system
//设置容器服务的开机自启动
[root@localhost ~]# systemctl enable --now container-nginx.service
Created symlink /etc/systemd/system/multi-user.target.wants/container-nginx.service → /usr/lib/systemd/system/container-nginx.service.
Created symlink /etc/systemd/system/default.target.wants/container-nginx.service → /usr/lib/systemd/system/container-nginx.service.
[root@localhost ~]# systemctl status container-nginx.service
● container-nginx.service - Podman container-nginx.service
Loaded: loaded (/usr/lib/systemd/system/container-nginx.service; enabled; vendor preset>
Active: active (running) since Wed 2021-12-15 23:04:46 CST; 38s ago
Docs: man:podman-generate-systemd(1)
Process: 5359 ExecStart=/usr/bin/podman start nginx (code=exited, status=0/SUCCESS)
Main PID: 5163 (conmon)
Tasks: 0 (limit: 49290)
Memory: 2.0M
CGroup: /system.slice/container-nginx.service
‣ 5163 /usr/bin/conmon --api-version 1 -c c58ca7266bdb646c535b0c3a3ad8f58f7793f>
12月 15 23:04:46 localhost.localdomain systemd[1]: Starting Podman container-nginx.service>
12月 15 23:04:46 localhost.localdomain systemd[1]: Started Podman container-nginx.service.
lines 1-13/13 (END)
8 .2 非根用户容器开机自启
//普通用户使用ssh连接,必须使用ssh连接,否则会报错(Failed to connect to bus: No such file or directory)
//先创建一个用户
[root@podman ~]# useradd tom
[root@podman ~]# echo '123456' | passwd --stdin tom
更改用户 tom 的密码 。
passwd:所有的身份验证令牌已经成功更新。
[root@podman ~]# ssh tom@192.168.200.145
//运行一个容器,非root用户只能映射1024以上的端口,1024以下的端口只能root用户映射。
[tom@localhost ~]$ podman run -dit --name nginx nginx
f0f478670345539c4278a2d5d3113982035ce3f94d01875e8ef3895918d7092b
[tom@localhost ~]$ podman ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c58ca7266bdb docker.io/library/nginx:latest nginx -g daemon o... 9 seconds ago Up 9 seconds ago nginx
//创建service文件目录结构,必须按照以下要求创建,任何都不能更改
[tom@localhost ~]$ mkdir -p ~/.config/systemd/user/
[tom@localhost ~]$ cd .config/systemd/user/
[tom@localhost ~]$ pwd
/home/tom/.config/systemd/user
//生成service文件
[tom@localhost ~]$ podman generate systemd --files --new --name nginx
/home/tom/.config/systemd/user/container-nginx.service
[tom@localhost ~]$ ls
container-nginx.service
[tom@localhost ~]$ cat container-nginx.service
# container-nginx.service
# autogenerated by Podman 3.4.1-dev
# Wed Dec 15 23:16:24 EST 2021
[Unit]
Description=Podman container-nginx.service
Documentation=man:podman-generate-systemd(1)
Wants=network-online.target
After=network-online.target
RequiresMountsFor=%t/containers
[Service]
Environment=PODMAN_SYSTEMD_UNIT=%n
Restart=on-failure
TimeoutStopSec=70
ExecStartPre=/bin/rm -f %t/%n.ctr-id
ExecStart=/usr/bin/podman run --cidfile=%t/%n.ctr-id --cgroups=no-conmon --rm --sdnotify=conmon --replace -dit --name nginx nginx
ExecStop=/usr/bin/podman stop --ignore --cidfile=%t/%n.ctr-id
ExecStopPost=/usr/bin/podman rm -f --ignore --cidfile=%t/%n.ctr-id
Type=notify
NotifyAccess=all
[Install]
WantedBy=multi-user.target default.target
//手动关闭容器,以便后续测试开机自启
[tom@localhost ~]$ podman stop -l
f0f478670345539c4278a2d5d3113982035ce3f94d01875e8ef3895918d7092b
[tom@localhost ~]$ podman ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
//重载服务,设置开机自启,无根用户启动时需要加上--user
[tom@localhost ~]$ systemctl --user daemon-reload
[tom@localhost ~]$ systemctl --user enable --now container-nginx.service
Created symlink /home/tom/.config/systemd/user/multi-user.target.wants/container-nginx.service → /home/tom/.config/systemd/user/container-nginx.service.
Created symlink /home/tom/.config/systemd/user/default.target.wants/container-nginx.service → /home/tom/.config/systemd/user/container-nginx.service.
//查看容器服务状态
[tom@localhost ~]$ systemctl status container-nginx.service
● container-nginx.service - Podman container-nginx.service
Loaded: loaded (/usr/lib/systemd/system/container-nginx.service; enabled; vendor>
Active: active (running) since Wed 2021-12-15 23:18:47 EST; 15min ago
Docs: man:podman-generate-systemd(1)
Process: 5259 ExecStart=/usr/bin/podman start nginx (code=exited, status=0/SUCCES>
Main PID: 5017 (conmon)
Tasks: 0 (limit: 49290)
Memory: 1.5M
CGroup: /system.slice/container-nginx.service
‣ 5017 /usr/bin/conmon --api-version 1 -c 508f9b3387e8a383a7c89ef920afab>
lines 1-10/10 (END)
//之前手动关闭nginx容器,已设置开机自启,查看容器是否运行
[tom@localhost ~]$ podman ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c58ca7266bdb docker.io/library/nginx:latest nginx -g daemon o... About a minute ago Up About a minute ago nginx
//服务关闭,容器自动删除
[tom@localhost ~]$ systemctl --user stop container-nginx.service
[tom@localhost ~]$ podman ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[tom@localhost ~]$ podman ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
//服务启动,容器自动创建并运行
[tom@localhost ~]$ systemctl --user start container-nginx.service
[tom@localhost ~]$ podman ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c58ca7266bdb docker.io/library/nginx:latest nginx -g daemon o... 4 seconds ago Up 3 seconds ago nginx
//查看普通用户是否有systemd的权限
[tom@localhost ~]$ loginctl user-status tom
tom (1000)
Since: Wed 2021-12-15 23:23:32 EST; 8min ago
State: active //active表示含有此权限,没有权限会显示linger
//如果普通用户没有systemd权限,可以执行以下命令开启systemd权限
loginctl enable-linger <username>