podman 生成systemd文件及配置启动

说到podman 启动容器,我们可能需要知道systemd了,podman也是推荐大家用systemd管理容器。

问题来了,如何正确的姿势来管理容器的开机启动呢?

我们之前有介绍过sctl这款管理systemd的工具,我们就介绍如何用它来管理我们的podman 容器。

比如我们启动一个名为nignx的应用

然后我们先创建一个工作目录,比如~/workspace/private/nginx-deploy

进入到目录下面

我们先创建一个nginx的容器实例

sudo podman run -d --name nginx -p 8070:80 nginx

然后我们生成systemd 文件,例如名叫nginx.service

sudo podman generate systemd > nginx.service

然后我们用sctl 启用它

$ sctl enable nginx
/home/nomore/workspace/private/nginx-deploy
enable service
+ [ -f /home/nomore/workspace/private/nginx-deploy/nginx.service ]
+ sudo cp -f /home/nomore/workspace/private/nginx-deploy/nginx.service /etc/systemd/system/nginx.service
+ sudo systemctl enable nginx.service
+ sudo systemctl daemon-reload
+ sudo systemctl restart nginx.service

然后我们看我们的应用是否启动了

$ sctl status nginx
/home/nomore/workspace/private/nginx-deploy
status of service
+ sudo systemctl status nginx.service
● nginx.service - Podman container-41e245e25b2d3e2f1bdce43187031175d9f68e4c6abb80fd1ea6c26ced5aba30.service
     Loaded: loaded (/etc/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2022-05-07 11:06:12 CST; 11s ago
       Docs: man:podman-generate-systemd(1)
    Process: 6514 ExecStart=/usr/bin/podman start 41e245e25b2d3e2f1bdce43187031175d9f68e4c6abb80fd1ea6c26ced5aba30 (code=exited, status=0/SUCCESS)
   Main PID: 6371 (conmon)
      Tasks: 0 (limit: 28585)
     Memory: 60.0K
        CPU: 52ms
     CGroup: /system.slice/nginx.service
             ‣ 6371 /usr/bin/conmon --api-version 1 -c 41e245e25b2d3e2f1bdce43187031175d9f68e4c6abb80fd1ea6c26ced5aba30 -u 41e245e25b2d3e2f1bdce43187031175d9f68e4c6abb80fd1ea6c26ced5aba30 -r /usr/bin/crun -b /var/lib/containers>

5月 07 11:06:12 wsk systemd[1]: Starting Podman container-41e245e25b2d3e2f1bdce43187031175d9f68e4c6abb80fd1ea6c26ced5aba30.service...
5月 07 11:06:12 wsk systemd[1]: Started Podman container-41e245e25b2d3e2f1bdce43187031175d9f68e4c6abb80fd1ea6c26ced5aba30.service.

我们看看8070端口是不是通的

$ curl localhost:8070
<!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>

到这里就ok了。

 

分类: 默认 标签: 发布于: 2022-05-07 11:03:56, 更新于: 2022-05-07 11:09:02