Ubuntu系统下Docker data-root配置重启后失效的问题求助
I see you're running Docker installed via Snap on Ubuntu 20.04, and you've hit a common quirk with Snap-managed services: when you manually switch the Docker data root to /dockerdata (via daemon.json or modifying docker.service), it sticks after a manual Docker restart—but after a full system reboot, Docker reverts to the Snap default path /var/snap/docker/common/var-lib/docker. Let's break down why this happens and fix it permanently.
Why this happens
Snap packages run in an isolated sandbox, and Snap's built-in service manager handles starting Docker on system boot. It doesn't respect system-level daemon.json or modified docker.service files you set up—instead, it loads its own default configuration unless you explicitly configure Snap-specific Docker settings. Your manual restart works because you're temporarily bypassing Snap's initial startup logic.
Permanent solutions for Snap Docker
Method 1: Use Snap's dedicated Docker config file (Recommended)
This is the simplest, most "Snap-native" way to lock in your custom data directory:
- Stop the Docker Snap service first:
sudo snap stop docker - Create the config directory for Snap's Docker instance (if it doesn't exist):
sudo mkdir -p /var/snap/docker/current/config/ - Create a
daemon.jsonin this Snap-specific directory to set your custom data root:sudo tee /var/snap/docker/current/config/daemon.json <<EOF { "data-root": "/dockerdata" } EOF - Ensure the
/dockerdatadirectory has the correct permissions (Docker requires root access):sudo chown -R root:root /dockerdata sudo chmod -R 700 /dockerdata - Start the Docker Snap service again:
sudo snap start docker - Verify the change took effect:
You should seedocker info | grep "Docker Root Dir"/dockerdatalisted. Reboot your system and run the verify command again—this time the setting will persist.
Method 2: Switch to systemd management (Optional)
If you prefer using standard systemd to manage Docker instead of Snap's service manager, you can disable the Snap-managed Docker and set up a custom systemd service:
- Stop and disable the Snap Docker service:
sudo snap stop docker sudo snap disable docker - Create a systemd service file at
/etc/systemd/system/docker.servicewith the following content (note the--data-root /dockerdataflag):[Unit] Description=Docker Application Container Engine Documentation=https://docs.docker.com After=network-online.target firewalld.service Wants=network-online.target [Service] Type=notify ExecStart=/snap/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock --data-root /dockerdata ExecReload=/bin/kill -s HUP $MAINPID TimeoutSec=0 RestartSec=2 Restart=always StartLimitBurst=3 StartLimitInterval=60s LimitNOFILE=infinity LimitNPROC=infinity LimitCORE=infinity TasksMax=infinity Delegate=yes KillMode=process [Install] WantedBy=multi-user.target - Reload systemd and enable the new service:
sudo systemctl daemon-reload sudo systemctl enable --now docker - Verify the configuration, then reboot to confirm it persists.
Context from your environment
Looking at your docker version and docker info output:
- You're running Docker Engine - Community 19.03.12, installed via Snap (evidenced by the original default path)
- After manual restart, your
Docker Root Dircorrectly shows/dockerdata—the issue is exclusively Snap overriding this setting on system boot
Method 1 is the best approach since it works within Snap's ecosystem, but Method 2 gives you more control if you're accustomed to systemd service management.
内容的提问来源于stack exchange,提问作者uruloki

