podman容器开机自启
root用户
[root@lch ~]# podman ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@lch ~]# cd /usr/lib/systemd/system
[root@lch system]# podman create --name web httpd
0cbe12e6066b49cbb232097f10b1626083a8dd60f3efacb272512cb3151a76fe
[root@lch system]# podman ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0cbe12e6066b docker.io/library/httpd:latest httpd-foreground 7 seconds ago Created web
[root@lch system]# systemctl status container-web
Warning: The unit file, source configuration file or drop-ins of container-web.service c>
● container-web.service - Podman container-web.service
Loaded: loaded (/usr/lib/systemd/system/container-web.service; enabled; vendor preset>
Active: failed (Result: exit-code) since Tue 2022-05-10 16:10:25 CST; 4h 56min ago
Docs: man:podman-generate-systemd(1)
Process: 2730 ExecStopPost=/usr/bin/podman stop -t 10 web (code=exited, status=125)
Process: 2461 ExecStop=/usr/bin/podman stop -t 10 web (code=exited, status=125)
Process: 2706 ExecStart=/usr/bin/podman start web (code=exited, status=125)
Main PID: 2261 (code=exited, status=0/SUCCESS)
[root@lch system]# systemctl enable --now container-web
[root@lch system]# systemctl status container-web
● container-web.service - Podman container-web.service
Loaded: loaded (/usr/lib/systemd/system/container-web.service; enabled; vendor preset>
Active: active (running) since Tue 2022-05-10 21:07:38 CST; 7s ago
Docs: man:podman-generate-systemd(1)
Process: 3162 ExecStart=/usr/bin/podman start web (code=exited, status=0/SUCCESS)
Main PID: 3229 (conmon)
Tasks: 3 (limit: 11201)
Memory: 3.4M
CGroup: /system.slice/container-web.service
├─3187 /usr/bin/fuse-overlayfs -o metacopy=on,lowerdir=/var/lib/containers/st>
└─3229 /usr/bin/conmon --api-version 1 -c 0cbe12e6066b49cbb232097f10b1626083a>
普通用户
请使用普通用户以SSH形式远程登录系统,否则中途容器在生成服务的时候会出现错误
[root@lch system]# echo 'lch123' | passwd --stdin tom //给普通用户设置密码
Changing password for user tom.
passwd: all authentication tokens updated successfully.
[root@lch system]# ssh tom@192.168.200.8 //ssh登录
The authenticity of host '192.168.200.8 (192.168.200.8)' can't be established.
ECDSA key fingerprint is SHA256:OLU3wa2pInbalooQidMTM33gVIfYp3Hc8iyN20M352I.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.200.8' (ECDSA) to the list of known hosts.
tom@192.168.200.8's password:
Last login: Tue May 10 21:20:32 2022
[tom@lch ~]$ podman login docker.io
Username: wjmz2
Password:
Login Succeeded!
[tom@lch ~]$ podman run -d --name httpd -p 8080:8080 docker.io/library/httpd
9826e0560efd7f7d385e39049ef2993f5ae5c3265ca12be114d76750d95e6d12
[tom@lch ~]$ podman ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9826e0560efd docker.io/library/httpd:latest httpd-foreground 10 seconds ago Up 10 seconds ago 0.0.0.0:8080->8080/tcp httpd
[tom@lch ~]$ mkdir -p ~/.config/systemd/user //必须在用户家目录创建此目录,不能更改名字
[tom@lch ~]$ cd ~/.config/systemd/user/
[tom@lch user]$ ls
//注意当前所在路径,使用podman生成系统服务(依照当前容器自动生成)
[tom@lch user]$ podman generate systemd --name httpd --files --new
/home/tom/.config/systemd/user/container-httpd.service
[tom@lch user]$ ls
container-httpd.service
[tom@lch user]$ podman stop httpd //关闭容器
httpd
[tom@lch user]$ podman ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[tom@lch user]$ systemctl --user daemon-reload //加载一下系统服务
[tom@lch user]$ systemctl --user enable container-httpd.service --now //立即启用服务
Created symlink /home/tom/.config/systemd/user/multi-user.target.wants/container-httpd.service → /home/tom/.config/systemd/user/container-httpd.service.
Created symlink /home/tom/.config/systemd/user/default.target.wants/container-httpd.service → /home/tom/.config/systemd/user/container-httpd.service.
//查看服务的状态,已经处于运行状态
[tom@lch user]$ systemctl --user status container-httpd.service
● container-httpd.service - Podman container-httpd.service
Loaded: loaded (/home/tom/.config/systemd/user/container-httpd.service; enabled; vend>
Active: active (running) since Tue 2022-05-10 21:23:20 CST; 10s ago
Docs: man:podman-generate-systemd(1)
Process: 3915 ExecStartPre=/bin/rm -f /run/user/1000/container-httpd.service.ctr-id (c>
Main PID: 3984 (conmon)
CGroup: /user.slice/user-1000.slice/user@1000.service/container-httpd.service
├─3967 /usr/bin/fuse-overlayfs -o ,lowerdir=/home/tom/.local/share/containers>
├─3970 /usr/bin/slirp4netns --disable-host-loopback --mtu=65520 --enable-sand>
├─3972 containers-rootlessport
├─3976 containers-rootlessport-child
├─3984 /usr/bin/conmon --api-version 1 -c 74cfe0d9443dbcadabc5d512af594b6060f>
├─3987 httpd -DFOREGROUND
├─3990 httpd -DFOREGROUND
├─3991 httpd -DFOREGROUND
└─3992 httpd -DFOREGROUND
//此时查看是否有容器在运行,本来我们已经手动关闭,但只要服务启动,就会自动创建新的容器,只要服务关闭,该容器就会自动删除
[tom@lch user]$ podman ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
74cfe0d9443d docker.io/library/httpd:latest httpd-foreground 49 seconds ago Up 49 seconds ago 0.0.0.0:8080->8080/tcp httpd
//服务关闭,容器自动删除,而且还可以做到开机自动创建,关机自动删除
[tom@lch user]$ systemctl --user stop container-httpd.service
[tom@lch user]$ podman ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[tom@lch user]$ systemctl --user start container-httpd.service
[tom@lch user]$ podman ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d21178450390 docker.io/library/httpd:latest httpd-foreground 3 seconds ago Up 3 seconds ago 0.0.0.0:8080->8080/tcp httpd
[tom@lch user]$