笔者在使用CentOS8时安装docker失败,于是发现CentOS8自带容器podman,podman的使用方法和docker类似,命令行基本相同。同时和docker相比,podman是无守护程序容器引擎(Daemonless),不能通过守护进程去实现自动重启容器的功能,可以通过Systemd守护进程管理来开机启动容器。同时,podman也不需要sudo访问(Rootless)。
podman来运行nginx容器,命令和docker一样,而且笔者使用的是docker的官方镜像 docker.io/library/nginx。
1、podman搜索镜像,有registry.redhat.io和docker.io可以选择。
podman search nginx
2、podman拉取镜像
podman pull docker.io/library/nginx
3、查看镜像
podman images
4、运行容器,映射到宿主机8080端口。
podman run -itd --name podman-nginx -p 8080:80 docker.io/library/nginx
需要注意的是,笔者使用的是centos8,映射到小于1024端口时会提示错误:Error: failed to expose ports via rootlessport: "cannot expose privileged port 80, you might need to add \"net.ipv4.ip_unprivileged_port_start=0\" (currently 1024) to /etc/sysctl.conf, or choose a larger port number (>= 1024): listen tcp 0.0.0.0:80: bind: permission denied\n" 。
这时需要选择一个大于等于1024的端口可以方便的解决。再次运行前需要将运行失败的容器删除podman rm podman-nginx ,不然会提示错误。
5、进入容器
podman exec -it podman-nginx bash
进入到nginx的根目录:/usr/share/nginx/html,可以找到index.php文件。
6、查看容器运行成功
怎么设置nginx容器开机启动呢?可以参考文章-Systemd添加.service服务并设置开机启动。
1、在/usr/lib/systemd/system目录下新建podman_nginx.service
[Unit]
Description=Podman Nginx Service
After=network.target
[Service]
Type=simple
ExecStart=/usr/bin/podman start -a podman-nginx
ExecStop=/usr/bin/podman stop -t 10 podman-nginx
Restart=always
[Install]
WantedBy=multi-user.target
2、systemctl enable设置开机启动
systemctl enable podman_nginx.service,激活开机启动
systemctl enable --now podman_nginx.service ,可以激活开机启动并启动服务。
在/etc/systemd/system/multi-user.target.wants/目录下可以看到这个链接文件。
网友评论