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

Docker自动启动容器相关问题咨询:原因、配置及修改方法

Let’s break down your questions one by one, since they all revolve around Docker’s auto-start behavior (first, a quick clarification: dockerd auto-starts containers, not images—images are just static templates for creating containers):

1. Which containers does dockerd auto-start?

dockerd will automatically start containers that have been configured with a restart policy. These policies are set when you create or update a container, and include:

  • always: The container starts automatically when dockerd starts, even if it was stopped before the daemon restarted.
  • unless-stopped: The container starts automatically when dockerd starts, unless you manually stopped it before the daemon restart (it won’t restart in that case).
  • on-failure:n: The container restarts only if it exited with a non-zero status code, up to n times.

Containers without any restart policy (the default no setting) won’t auto-start when dockerd launches or restarts.

2. Why did unexpected containers start after service docker restart?

This happens because those "unexpected" containers have a restart policy set (like always or unless-stopped). When you restart the Docker daemon, it scans all existing containers and starts any that match their configured restart rules.

3. Where is auto-start (restart policy) information stored?

Restart policy details are stored in Docker’s local data directory, which defaults to /var/lib/docker. Specifically:

  • Each container’s metadata lives in /var/lib/docker/containers/<container-UUID>/config.v2.json—look for the RestartPolicy field in this JSON file.
  • You don’t need to dig into raw files to check this, though: run docker inspect <container-name-or-id> and look for the RestartPolicy section under HostConfig.
4. Why only 8 out of ~50 images auto-start when dockerd launches?

Remember: images don’t auto-start—containers do. That means:

  • The 8 containers that start are ones you’ve already created from images, and those containers have a restart policy enabled.
  • The other 42 images either:
    • Haven’t been used to create any containers yet, or
    • Were used to create containers, but those containers have the default no restart policy (so they don’t auto-start), or
    • Were used to create containers that were deleted.
5. How to modify this auto-start behavior?

You have a few options depending on what you need:

Disable auto-start for existing containers

  • For a single container:
    docker update --restart=no <container-name-or-id>
    
  • For all running containers (or filter with docker ps flags to target specific ones):
    docker ps -q | xargs docker update --restart=no
    

Set restart policies when creating new containers

  • Use the --restart flag with docker run:
    # Start automatically unless manually stopped (recommended for most use cases)
    docker run --restart=unless-stopped <image-name>
    # Start every time dockerd launches, even if manually stopped before
    docker run --restart=always <image-name>
    # Restart only if the container fails (exits with non-zero code), up to 3 times
    docker run --restart=on-failure:3 <image-name>
    

Prevent all containers from auto-starting during dockerd restart

If you want the Docker daemon to restart without launching any containers, enable the live-restore feature in /etc/docker/daemon.json (create the file if it doesn’t exist):

{
  "live-restore": true
}

Then restart the daemon to apply the change:

service docker restart

Note: live-restore keeps running containers alive during the daemon restart, but won’t start any stopped containers (even those with always policy).


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 08:33:33