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):
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 tontimes.
Containers without any restart policy (the default no setting) won’t auto-start when dockerd launches or restarts.
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.
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 theRestartPolicyfield 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 theRestartPolicysection underHostConfig.
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
norestart policy (so they don’t auto-start), or - Were used to create containers that were deleted.
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 psflags to target specific ones):docker ps -q | xargs docker update --restart=no
Set restart policies when creating new containers
- Use the
--restartflag withdocker 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

