1.拉取测试镜像
[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 5cc84ad355aa done
Copying config beae173cca done
Writing manifest to image destination
Storing signatures
beae173ccac6ad749f76713cf4440fe3d21d1043fe616dfbe30775815d1d0f6a
2.配置加速器
[root@localhost ~]# cd /etc/containers/
[root@localhost containers]# ls
certs.d policy.json registries.conf.d storage.conf
oci registries.conf registries.d
[root@localhost containers]# vim registries.conf
unqualified-search-registries = ["docker.io"]
[[registry]]
prefix = "docker.io"
location = "onmth88j.mirror.aliyuncs.com" //直接到我们自己的阿里云账号上寻找地址,并且不需要http:// 直接写入地址
3.创建运行一个容器
[root@localhost ~]# podman run --name test -dit busybox
[root@localhost ~]# podman ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fb25aff33803 docker.io/library/busybox:latest sh 8 seconds ago Up 7 seconds ago test
4.创建容器自启动文件
//如果想每次重启都会删除旧容器启动一个新的容器,则可以使用--new选项
[root@localhost ~]# podman generate systemd --files --name test --new
/root/container-test.service
5.将生成的文件放到/usr/lib/systemd/system/目录中,并且重启使其生效
[root@localhost ~]# ls
anaconda-ks.cfg container-test.service
[root@localhost ~]# mv container-test.service /usr/lib/systemd/system/
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# setenforce 0
6.修改selinux并且设置开机自启
[root@localhost ~]# vim /etc/selinux/config
SELINUX=disabled
[root@localhost ~]# systemctl enable --now container-test.service
Created symlink /etc/systemd/system/multi-user.target.wants/container-test.service → /usr/lib/systemd/system/container-test.service.
Created symlink /etc/systemd/system/default.target.wants/container-test.service → /usr/lib/systemd/system/container-test.service.
7.重启测试效果
//重启后容器依旧处于启动状态,则设置开机自启成功
[root@localhost ~]# reboot
连接断开
连接成功
Last login: Tue May 10 14:44:03 2022 from 192.168.12.1
[root@localhost ~]# podman ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ef6663d675b8 docker.io/library/busybox:latest sh 58 seconds ago Up 55 seconds ago test