How to move Docker's data directory from /var/lib
I recently had to move Docker’s data directory on my home NAS (from the root filesystem to the storage array). I found some incomplete and inconsistent information on the Web about how to do this, so for future reference here’s the process I used to do it successfully on an Ubuntu 22 server:
1. Disable Docker
systemctl mask docker.socket
systemctl stop docker.service
You must mask docker.socket
, or Docker may restart while you’re moving the data directory, resulting in corruption. (Ask me how I know!)
2. Copy the data
cp -RPp /var/lib/docker /mnt/storage/_var-lib-docker
mv /var/lib/docker /var/lib/docker.old
Rather than moving the data to the new location with mv
, I opted to copy it and keep the old one around as a backup until I was certain everything was working properly.
Those cp
flags are:
-R
: recursive; continue even if errors are detected-P
: do not follow symbolic links-p
: preserve modification time, access time, file flags, file mode, user ID, group ID, ACLs, and extended attributes
3. Restart Docker
systemctl unmask docker.socket
systemctl start docker.service
After this, you just need to be sure your containers come back up and are running as expected.