You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker容器通过systemd启动后随即停止的问题求助

Troubleshooting Docker Container Exiting Immediately on Systemd Start

Hey there, let's work through your Docker + systemd issue. I see you've already looked into the threads Docker and systemd - service stopping after 10 seconds and Docker containers shut down after systemd start but still can't get your container to stay up when launched via systemd. Let's break down what's going wrong with your service config.

First, let's look at the psim.service file you shared:

[Unit]
Description=My process
After=docker.service
Requires=docker.service
[Service]
ExecStart=docker run --net=host --name psim -it psim
ExecStop=/bin/docker stop psim
ExecStopPost=/bin/docker rm psim
[Install]
WantedBy=local.target

The Biggest Culprit: -it Flags

Systemd runs services in a non-interactive, non-TTY environment. When you include -it (interactive + pseudo-TTY) in your docker run command, Docker expects to attach to a terminal that doesn't exist in the systemd context. This causes the container to exit immediately after starting—this is almost certainly the main issue here.

First step: Remove the -it flags from your ExecStart line. It should become:
ExecStart=docker run --net=host --name psim psim

Hardening the Systemd Service

Next, let's add a few critical settings to the [Service] section to make systemd manage the container properly:

  • Type=simple: Tells systemd that the process started by ExecStart is the main service process (perfect for docker run, which blocks while the container runs)
  • Restart=always: Automatically restarts the container if it exits unexpectedly
  • TimeoutStartSec=0: Disables the startup timeout, useful if your container takes a moment to initialize

Your updated [Service] section will look like this:

[Service]
Type=simple
Restart=always
TimeoutStartSec=0
ExecStart=docker run --net=host --name psim psim
ExecStop=/bin/docker stop psim
ExecStopPost=/bin/docker rm psim

Verify Your Container's Foreground Process

One more check: Make sure your psim image runs a foreground process. If the entrypoint or command in the image runs a background task (like starting a daemon and exiting), the container will exit right away even without -it.

Test this manually by running:

docker run psim

If this command exits immediately, you'll need to adjust your image to run a foreground command. For testing, you can temporarily use something like tail -f /dev/null to keep the container alive, or fix your application to run in the foreground.

Apply the Fix

After updating the service file, run these commands to reload systemd and restart your service:

sudo systemctl daemon-reload
sudo systemctl restart psim.service

Check the status to confirm it's running:

sudo systemctl status psim.service

If you still run into issues, check the logs with:

journalctl -u psim.service

内容的提问来源于stack exchange,提问作者chip

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:47:05