Change Docker data directory on Debian

If you’ve installed Docker with the default settings on Debian, it will be storing Docker images, containers and volumes in /var/lib/docker, which will be an issue if you have /var on its own (usually small) partition.

After using Docker for a while you may start to run out of space on the /var partition, at which point you’ll need to either add more space to that partition or relocate it to somewhere with more space.

Here are the steps to change the directory even after you’ve created Docker containers etc.
Note, you don’t need to edit docker.service or init.d files, as it will read the change from the .json file mentioned below.

Steps

  1. Edit /etc/docker/daemon.json (if it doesn’t exist, create it)
  2. Add the following
    {
      "data-root": "/new/path/to/docker-data"
    }
  3. Stop docker
    sudo systemctl stop docker
  4. Check docker has been stopped
    ps aux | grep -i docker | grep -v grep
  5. Copy the files to the new location
    Optionally you could run this inside screen if you have a large amount of data or unreliable ssh connection.

    sudo rsync -axPS /var/lib/docker/ /new/path/to/docker-data

    Options explanation, check out the man page for more info

    -a, --archive             archive mode; equals -rlptgoD (no -H,-A,-X)
    -x, --one-file-system     don't cross filesystem boundaries
    -P                        show progress during transfer
    -S, --sparse              handle sparse files efficiently
  6. Start Docker back up
    sudo systemctl start docker
  7. Check Docker has started up using the new location
    docker info | grep 'Docker Root Dir'
  8. Check everything has started up that should be running
    docker ps

Leave both copies on the server for a few days to make sure no issues arise, then feel free to delete it.

sudo rm -r /var/lib/docker

Documentation / sources

Official docs https://docs.docker.com/engine/admin/systemd/

Docker GitHub issue item https://github.com/docker/docker/issues/3127

Blog post inspired by linuxconfig

Tested and working on Debian Stretch (9.3) with Docker Docker version 17.12.0-ce

  • Andreas Huber

    Great solution. Worked fine on 18.04 Ubuntu – had to do it twice, as there was an issue with in combination with portainer.io as this is my container cockpit.
    Finally – i followed your steps again and made a new install of portainer.io interface, the same as their initial setup. The first startup of my “moved” containers start did not work, but stopping them once and restarting again made my “moved” containers working again!

    • https://adriel.co.nz Adriel

      Thanks for the feedback Andreas! I’m sure it’ll help others who use Portainer too.