How to Move Docker Data Directory to Another Location on Ubuntu
Update 05-08-2022: Graph has been deprecated and replaced to data-root.
Docker is a popular container management platform that can dramatically speed up your development workflow. It is available as a package on major Linux distributions, including Ubuntu.
The standard data directory used for docker is
/var/lib/docker. There are various reasons why you may want to change docker's default directory from which the most obvious could be ran out of disk space. This make sense because this directory will store all your images, volumes, etc. it can become quite large in a relative small amount of time. This is also what's happened with me, so I decided to dig out how to get this done and share it here.
If you want to move the docker data directory to another location you can follow the following simple steps.
Stop Docker Service
It is important here that you have completely stopped Docker service.
$ sudo systemctl stop docker
Next, verify if above command succeeded. The following command will yield no output only if Docker service is stopped:
$ sudo ps aux | grep -i docker | grep -v grep
Update Docker Configuration
To change the data directory location, update docker configuration file to tell the docker daemon where is the new location of the data directory.
Create this file
/etc/docker/daemon.json and write these into:
Off course you'll need to adjust the
/path/to/your/new/docker/data/directory with your own.
Copy Existing Docker Data
If you're like me, ran out of disk space while working on Docker image, then you'll need to keep your existing Docker data. Thankfully, this could be done easily with the help from
$ sudo rsync -aP /var/lib/docker/ /path/to/your/new/docker/data/directory
It could take a while depends on your existing docker data size. Please wait until it finished.
As a good practice, we should verify whether we've successfully moved our Docker data directory to new location or not. First, let's rename old Docker data directory.
$ sudo mv /var/lib/docker /var/lib/docker.orig
Start our docker service:
$ sudo systemctl start docker
Verify with docker info:
$ docker info | grep -i root
You should see something like this:
Docker Root Dir: /path/to/your/new/docker/data/directory
If that's the case, we can safely remove
/var/lib/docker directory now.
$ sudo rm -fr /var/lib/docker.orig
Starting from v17.05
graph flag has been deprecated. It has been replaced with the more descriptive
data-rootflag. Our new
/etc/docker/daemon.json file should now be like this:
I hope that you now know how to move Docker data directory to another location on Ubuntu. If you run into any issues or have any feedback feel free to drop a comment below.