本文介绍了Docker和systemd-服务在10秒后停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在systemd启动Docker容器时,我无法使其保持正常运行.当我使用 sudo docker start containername 手动启动时,它保持正常运行,但是当使用 sudo systemctl start containername 通过systemd启动时,它保持启动10秒钟,然后神秘死亡,将消息保留在syslog中,如下所示:

I'm having trouble getting a Docker container to stay up when it's started by systemd. When I start it manually with sudo docker start containername, it stays up without trouble, but when it's started via systemd with sudo systemctl start containername, it stays up for 10 seconds then mysteriously dies, leaving messages in syslog something like the following:

Mar 13 14:01:09 hostname docker[329]: time="2015-03-13T14:01:09Z" level="info" msg="POST /v1.17/containers/containername/stop?t=10"
Mar 13 14:01:09 hostname docker[329]: time="2015-03-13T14:01:09Z" level="info" msg="+job stop(containername)"

我假设它是系统杀死进程,但是我无法弄清楚它为什么会发生.systemd单元文件(/etc/systemd/system/containername.service)非常简单,如下所示:

I am making the assumption that it's systemd killing the process, but I can't work out why it might be happening. The systemd unit file (/etc/systemd/system/containername.service) is pretty simple, as follows:

[Unit]
Description=MyContainer
After=docker.service
Requires=docker.service

[Service]
ExecStart=/usr/bin/docker start containername
ExecStop=/usr/bin/docker stop containername

[Install]
WantedBy=multi-user.target

Docker可以在启动时正常启动,并且看起来甚至可以启动docker容器,但是无论是启动还是手动启动,它都会在10秒钟后退出.感谢收到的帮助!

Docker starts fine on boot, and it looks like it does even start the docker container, but no matter if on boot or manually, it then quits after exactly 10 seconds. Help gratefully received!

推荐答案

解决方案:启动命令似乎需要-a(附加)参数,如 .我认为这是因为默认情况下它会派生到后台,尽管systemd 期望守护程序 功能似乎无法解决问题.

Solution: The start command seems to need the -a (attach) parameter as described in the documentation when used in a systemd script. I assume this is because it by default forks to the background, although the systemd expect daemon feature doesn't appear to fix the issue.

来自 docker-start 联机帮助页:

-a, --attach=true|false
   Attach container's STDOUT and STDERR and forward all signals to the process. The default is false.

整个systemd脚本将变为:

The whole systemd script then becomes:

[Unit]
Description=MyContainer
After=docker.service
Requires=docker.service

[Service]
ExecStart=/usr/bin/docker start -a containername
ExecStop=/usr/bin/docker stop containername

[Install]
WantedBy=multi-user.target

这篇关于Docker和systemd-服务在10秒后停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 03:36