How to move docker data directory to another location on Linux

Docker is very popular and easy to use container management that can speed up your deployment of your application with few lines of configuration. I always prefer docker-compose.yml to deploy and manage multiple containers of the application. For basic example of docker-compose.yml you can follow this link.

Why we need to change data-location of Docker?

Default data location of the docker is:

  • Ubuntu: /var/lib/docker/
  • Fedora: /var/lib/docker/
  • Debian: /var/lib/docker/
  • Windows: C:\ProgramData\DockerDesktop
  • MacOS: ~/Library/Containers/com.docker.docker/Data/vms/0/

This location hold data for containers, volumes, builds, networks, images and clusters. So something memory issue can be occur on this location, due to this location used by other system application as well.

So we need to move docker data location to another path.

You can check docker default location by command: docker info

Stop the docker daemon

sudo service docker stop

Add a configuration file to tell the docker daemon what is the location of the data directory

Using your preferred text editor add a file named daemon.json under the directory /etc/docker. The file should have this content:

{ 
   "data-root": "/path/to/your/docker" 
}

Copy the current data directory to the new one

sudo rsync -aP /var/lib/docker/ /path/to/your/docker

Rename the old docker directory

sudo mv /var/lib/docker /var/lib/docker.bkp

Restart the docker daemon

sudo service docker start

Test

You can remove .bkp if everything working fine for you.

sudo rm -rf /var/lib/docker.old

How to move docker data directory to another location on Linux
Show Buttons
Hide Buttons