Docker容器在systemd启动后关闭 [英] Docker containers shut down after systemd start

查看:323
本文介绍了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启动后关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆