Changing the Docker root directory

I run some Docker containers on my NAS, but ran into a hard drive space issue because of Docker:

sudo du -sh /var/lib/docker
30G	/var/lib/docker

My NAS has a 110Gb SSD for the OS (Ubuntu Server) and 4 drives in a ZFS array for storage.

By default, Docker runs everything from /var/lib/docker, but without doing some changes in my fstab, /var/lib/docker was on my 110Gb SSD, not on ZFS array.

I wanted to change the location of where Docker runs, which after reading some Docker documentation, was a lot easier than I had originally thought.

Changing the Docker root directory

  • Stop Docker

    sudo systemctl stop docker.service
    Warning: Stopping docker.service, but it can still be activated by:
      docker.socket
    
    sudo systemctl stop docker.socket
    
  • Verify Docker is stopped

systemctl status docker
○ docker.service - Docker Application Container Engine
     Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
     Active: inactive (dead) since Sun 2023-01-22 15:18:14 MST; 1min 20s ago
  • Create the new Docker root directory

    sudo mkdir /mnt/docker
    
  • Change the Docker root directory by creating/modifying the /etc/docker/daemon.json file.

    Specify the new Docker root directory via the data-root, and since I'm using a ZFS array as the root directory, specify the ZFS storage driver:

    {
      "data-root":"/mnt/docker",
      "storage-driver":"zfs"
    }
    
  • Copy over the Docker files to the new Docker root directory

    sudo rsync -Prvax /var/lib/docker/ /mnt/docker/
    
    ...
    
  • Modify existing Docker containers to use the new root directory

    sudo find /mnt/docker/containers/ -type f -name config.v2.json | xargs sudo sed -i.bak 's%/var/lib/docker%/mnt/docker%g'
    
  • Restart Docker

    sudo systemctl start docker
    
  • Verify the new Docker root directory

    docker info -f '{{ .DockerRootDir}}'
    /mnt/docker
    

References

Docker Docs - Manuals / Docker Engine / Storage / Storage drivers / Use the ZFS storage driver https://docs.docker.com/storage/storagedriver/zfs-driver/