podman容器开机自启
管理员开机自启
#拉取busybox镜像作测试
[root@localhost ~]# podman pull busybox
Resolved "busybox" as an alias (/etc/containers/registries.conf.d/000-shortnames.conf)
Trying to pull docker.io/library/busybox:latest...
Getting image source signatures
Copying blob 50e8d59317eb done
Copying config 1a80408de7 done
Writing manifest to image destination
Storing signatures
1a80408de790c0b1075d0a7e23ff7da78b311f85f36ea10098e4a6184c200964
#查看镜像
[root@localhost ~]# podman images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/library/busybox latest 1a80408de790 3 weeks ago 1.46 MB
[root@localhost ~]#
#用busybox镜像创建一个测试容器名叫web
[root@localhost ~]# podman run -itd --name web docker.io/library/busybox:latest
ee721d918ef4c898583ee99a2c408b8ae9939a49c5aee185c88b4849c3a8fa9a
#创建容器后启动service文件
#如果希望每次启动容器都会删除旧的容器,启动一个全新的容器,则使用--new选项
[root@localhost ~]# podman generate systemd --files --name web --new
/root/container-web.service
#将生成的文件放到/usr/lib/systemd/system/目录中
[root@localhost ~]# ls
anaconda-ks.cfg container-web.service
[root@localhost ~]# mv container-web.service /usr/lib/systemd/system
#重新加载让其生效
[root@localhost ~]# systemctl daemon-reload
#修改selinux
[root@localhost ~]# setenforce 0
[root@localhost ~]# vim /etc/selinux/config
SELINUX=disabled #改为disabled
#设置开机自启
[root@localhost ~]# systemctl enable --now container-web.service
Created symlink /etc/systemd/system/multi-user.target.wants/container-web.service → /usr/lib/systemd/system/container-web.service.
Created symlink /etc/systemd/system/default.target.wants/container-web.service → /usr/lib/systemd/system/container-web.service.
#查看上一条命令是否执行成功,非0表示失败
[root@localhost ~]# echo $?
0
#重启
[root@localhost ~]# reboot
#真机重启后查看是开机开启的
[root@localhost ~]# systemctl status container-web.service
● container-web.service - Podman container-web.service
Loaded: loaded (/usr/lib/systemd/system/container-web.service; e>
Active: active (running) since Tue 2022-05-10 21:40:19 CST; 20s >
Docs: man:podman-generate-systemd(1)
....省略
#容器也是开机自启的
[root@localhost ~]# podman ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
04b4b7f7fdab docker.io/library/busybox:latest sh 37 seconds ago Up 36 seconds ago web
[root@localhost ~]#
普通用户开机自启
#拉取httpd镜像作测试
[root@localhost ~]# podman pull httpd
#用httpd开启一个容器
[root@localhost ~]# podman run -d --name web httpd
9a06cf447b64b8cbed992d31d9faaca263e28c48b4cd4d3078c4d417db22f2be
[root@localhost ~]# podman ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9a06cf447b64 docker.io/library/httpd:latest httpd-foreground 5 seconds ago Up 6 seconds ago web
[root@localhost ~]#
#创建 ~/.config/systemd/user/⽬录。
[root@localhost ~]# mkdir -p ~/.config/systemd/user
#使⽤podman generate systemd命令,从正在运⾏的容器中创建systemd单元⽂件。
[root@localhost ~]# cd ~/.config/systemd/user/
[root@localhost user]# podman generate systemd --files --name web --new
/root/.config/systemd/user/container-web.service
[root@localhost user]# ls
container-web.service #要生成这个文件
#停⽌然后删除web容器。
[root@localhost user]# podman stop web
[root@localhost user]# podman rm web
#指示systemd重新加载其配置,然后启⽤并启动container-web服务。
[root@localhost user]# systemctl --user daemon-reload
[root@localhost user]# systemctl --user enable --now container-web.service
#确认容器正在运⾏。
[root@localhost user]# podman ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9a06cf447b64 docker.io/library/httpd:latest httpd-foreground 7 minutes ago Up 3 seconds ago web
#运⾏loginctl enable-linger命令,使⽤户服务在服务器启动时⾃动启动。
[root@localhost user]# loginctl enable-linger
#重启后查看容器是否开机自启
[root@localhost ~]# reboot
#查看容器是否开机自启
[root@localhost ~]# podman ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9a06cf447b64 docker.io/library/httpd:latest httpd-foreground 11 minutes ago Up 28 seconds ago web
[root@localhost ~]#