7

I'm trying to change the default data folder of docker images, containers, etc to a different path. Snap installation of docker has such folder at /var/snap/docker/common/var-lib-docker. Theoretically I could change that with data-root option in deamon.json. But, if I change the daemon.json adding "data-root": "/home/user/docker" docker won't start due to a conflict with flags (which always has the previously described default path on it).

I do can start docker with my custom path if I stop it and then start it like this: sudo snap start docker.dockerd --data-root=/home/user/docker. Which is not pretty but works. Is there a way to change docker snap flags on startup or make it prefers the daemon.json options?

I've read this archived post, which treats such issue on docker version 17, but it didn't helped much the same way several other material I found online. I seems that symbolic link may be a way tho...

I'm using docker 19.03.11, snap installed on Ubuntu 20.04.

P.s.: The new path is on a second HDD mounted as my home directory. Changing the path will save space in my system SSD.

Thanks for the attention.

Rodrigo
  • 155
  • 1
  • 11

2 Answers2

3

From https://github.com/docker-snap/docker-snap/issues/3 and https://askubuntu.com/questions/550348/how-to-make-mount-bind-permanent, the not-perfect-but-working solution seems to be the bind mount between /var/snap/docker/common/var-lib-docker and /home/username/docker which is the previous docker data-root I had before installing docker with snap.

So first, clear the data-root option in daemon.json.

Then add the following at the end of /etc/fstab with the following command:

echo '/home/username/docker /var/snap/docker/common/var-lib-docker none bind' >> /etc/fstab

After reboot, your docker data root will be stored in /home/username/docker

blondelg
  • 916
  • 1
  • 8
  • 25
  • what does this do to the old /var/snap/docker/common/var-lib-docker contents? should I be concerned? – tofutim Jan 22 '22 at 19:26
  • when the target folder starts blank, I end up with ```Server: ERROR: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running? errors pretty printing info``` – tofutim Jan 22 '22 at 19:33
  • I don't think you should be concerned of the old docker directory, It's a bit like having two docker environnements. – blondelg Jan 24 '22 at 09:20
  • why not use a symbolic link? wouldn't `ln -s /newfolder /var/snap/docker/common` work? – Ben Grady Apr 26 '23 at 03:10
1

I ran out of space on an Ubuntu VirtualBox VM and had to do the following:

  1. Stop the VM and create a new Fixed Volume

  2. Start the VM and make sure the new volume was mounted

  3. Stop the docker service

    sudo systemctl stop docker.service
    sudo systemctl stop docker.socket
    
  4. Copy /var/lib/docker to new volume

    sudo rsync -aqxP /var/lib/docker/ /media/username/spare\ disk/
    
  5. Update /etc/docker/daemon.json

    {
        "data-root": "/media/username/spare disk/docker",
        "storage-driver": "overlay2"
    }
    
  6. Reload systemd and start docker service

    sudo systemctl daemon-reload
    sudo systemctl start docker
    

See: https://docs.docker.com/config/daemon/systemd/#runtime-directory-and-storage-driver