Skip to content

Instantly share code, notes, and snippets.

@duruyao
Last active November 1, 2022 02:13
Show Gist options
  • Save duruyao/4615d4565016a641bf9e636e9e2054b7 to your computer and use it in GitHub Desktop.
Save duruyao/4615d4565016a641bf9e636e9e2054b7 to your computer and use it in GitHub Desktop.
Move data of Docker to the new directory.

Change Docker Root Directory

  1. Show docker root directory (default: /var/lib/docker).
docker_root="$(docker info -f '{{ .DockerRootDir }}')"
echo "${docker_root}"
  1. Start the docker containers and display the free size of the hard disk where the current docker root directory is located.
docker run -itd --name $USER.$RANDOM --rm -v $PWD:$PWD ubuntu:18.04 bash
docker run -itd --name $USER.$RANDOM --rm -v $PWD:$PWD ubuntu:18.04 bash
sudo df -h
  1. Stop all the docker services and docker containers.
sudo systemctl stop docker.service
sudo systemctl stop docker.socket
sudo systemctl stop containerd
  1. Create new docker root directory.
new_docker_root="/path/to/new_docker_root"
sudo mkdir -p "${new_docker_root}"
  1. Sync data from the old docker root directory to the new docker root directory, and it might take a while. Note that the slash / is required, in other words, the argument "${docker_root}"/ and the argument "${docker_root}" are not same for the command rsync.
sudo rsync -avuqP --delete "${docker_root}"/ "${new_docker_root}"
  1. Edit the file /etc/docker/daemon.json. If the file does not exist, create it and add the following information.
{
  "data-root": "/path/to/new_docker_root",
  "storage-driver": "overlay2"
}
  1. Restart all the docker services.
sudo systemctl daemon-reload
sudo systemctl start docker
  1. Check the new docker root directory and images information.
docker info -f '{{ .DockerRootDir }}'
docker images
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment