Docker容器在systemd启动后关闭

编程入门 行业动态 更新时间:2024-10-23 05:42:16
本文介绍了Docker容器在systemd启动后关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

由于某些原因,当使用systemd单位文件时,我的docker容器启动但立即关闭。我尝试查找日志,但看不到任何有关为什么发生这种情况的迹象。

For some reason when using systemd unit files my docker containers start but get shut down instantly. I have tried finding logs but can not see any indication on why this is happening. Is there someone that knows how to solve this / find the logs that show what is happening?

注意:在使用docker start containername引导后手动启动它们时,它可以工作(同样在使用systemctl start nginx时)

Note: When starting them manually after boot with docker start containername then it works (also when using systemctl start nginx)

经过更多挖掘后,我发现此错误:找不到udev设备:没有这样的设备可能与此有关?

After some more digging I found this error: could not find udev device: No such device it could have something to do with this?

单元服务文件:

[Unit] Description=nginx-container Requires=docker.service After=docker.service [Service] Restart=always RestartSec=2 StartLimitInterval=3600 StartLimitBurst=5 TimeoutStartSec=5 ExecStartPre=-/usr/bin/docker kill nginx ExecStartPre=-/usr/bin/docker rm nginx ExecStart=/usr/bin/docker run -i -d -t --restart=no --name nginx -p 80:80 -v /projects/frontend/data/nginx/:/var/www -v /projects/frontend: nginx ExecStop=/usr/bin/docker stop -t 2 nginx [Install] WantedBy=multi-user.target

Journalctl输出:

Journalctl output:

May 28 11:18:15 frontend dockerd[462]: time="2015-05-28T11:18:15Z" level=info msg="-job start(d757f83d4a13f876140ae008da943e8c5c3a0765c1fe5bc4a4e2599b70c30626) = OK (0)" May 28 11:18:15 frontend dockerd[462]: time="2015-05-28T11:18:15Z" level=info msg="POST /v1.18/containers/nginx/stop?t=2" May 28 11:18:15 frontend dockerd[462]: time="2015-05-28T11:18:15Z" level=info msg="+job stop(nginx)"

Docker日志:空(docker日志nginx)

Docker logs: empty (docker logs nginx)

Systemctl输出:(系统ctl状态为nginx,nginx.service)

Systemctl output: (systemctl status nginx, nginx.service)

● nginx.service - nginx-container Loaded: loaded (/etc/systemd/system/multi-user.target.wants/nginx.service) Active: failed (Result: start-limit) since Thu 2015-05-28 11:18:20 UTC; 12min ago Process: 3378 ExecStop=/usr/bin/docker stop -t 2 nginx (code=exited, status=0/SUCCESS) Process: 3281 ExecStart=/usr/bin/docker run -i -d -t --restart=no --name nginx -p 80:80 -v /projects/frontend/data/nginx/:/var/www -v /projects/frontend:/nginx (code=exited, status=0/SUCCESS) Process: 3258 ExecStartPre=/usr/bin/docker rm nginx (code=exited, status=0/SUCCESS) Process: 3246 ExecStartPre=/usr/bin/docker kill nginx (code=exited, status=0/SUCCESS) Main PID: 3281 (code=exited, status=0/SUCCESS) May 28 11:18:20,frontend systemd[1]: nginx.service holdoff time over, scheduling restart. May 28 11:18:20 frontend systemd[1]: start request repeated too quickly for nginx.service May 28 11:18:20 frontend systemd[1]: Failed to start nginx-container. May 28 11:18:20 frontend systemd[1]: Unit nginx.service entered failed state. May 28 11:18:20 frontend systemd[1]: nginx.service failed.

推荐答案

因为您未指定在您的systemd单位文件中键入,systemd使用默认值 simple 。来自 systemd.service :

Because you have not specified a Type in your systemd unit file, systemd is using the default, simple. From systemd.service:

如果设置为simple(如果未指定Type =或BusName =,但未指定 ExecStart =,则为默认值),预期进程配置为ExecStart =的是服务的主要过程。

If set to simple (the default if neither Type= nor BusName=, but ExecStart= are specified), it is expected that the process configured with ExecStart= is the main process of the service.

这意味着如果该过程由 ExecStart启动退出,systemd 将假定您的服务已退出并清理所有内容。

This means that if the process started by ExecStart exits, systemd will assume your service has exited and will clean everything up.

因为您正在使用 -d ,它会立即退出 ...因此,systemd会清理服务。

Because you are running the docker client with -d, it exits immediately...thus, systemd cleans up the service.

通常,当使用systemd启动容器时,您将不使用 -d 标志。这意味着客户端将继续运行,并且将允许systemd收集您的应用程序产生的任何输出。

Typically, when starting containers with systemd, you would not use the -d flag. This means that the client will continue running, and will allow systemd to collect any output produced by your application.

那是说,启动时存在一些基本问题带有systemd的Docker容器。由于Docker的运行方式,systemd实际上没有办法监视您的容器状态。它真正能做的就是跟踪docker client 的状态,这不是同一件事(客户端可以退出/崩溃/等而不影响您的容器)。这不仅与systemd有关; 任何类型的流程主管(新贵,runit,主管等)都会遇到相同的问题。

That said, there are fundamental problems in starting Docker containers with systemd. Because of the way Docker operates, there really is no way for systemd to monitor the status of your container. All it can really do is track the status of the docker client, which is not the same thing (the client can exit/crash/etc without impacting your container). This isn't just relevant to systemd; any sort of process supervisor (upstart, runit, supervisor, etc) will have the same problem.

更多推荐

Docker容器在systemd启动后关闭

本文发布于:2023-11-17 04:41:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1608864.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:容器   Docker   systemd

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!