How to move docker data directory to a different mount – Ubuntu

I’ve recently been working with docker to deploy containers on Ubuntu server. By default they deploy in the standard data directory used for docker is /var/lib/docker. If you’re using a cloud platform like digital ocean the standard storage size relates to the hardware size, so in most cases if you don’t want to change the hardware allocated to your server you need to add another volume. This is how you move the docker containers to this additional storage.

1. Stop the docker daemon

sudo service docker stop

2. Create or edit the configuration at /etc/docker/daemon.json

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

3. Copy the current data directory to the new one

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

4. Rename the old docker directory

sudo mv /var/lib/docker /var/lib/docker.old

5.  Restart the docker daemon

sudo service docker start

6. Test everything is working ok. You should see no differences in using your docker containers and see they are running again as healthy.

sudo docker images
docker ps

7. When you are sure that the new directory is being used correctly by docker daemon you can delete the old data directory.

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

You May Also Like

About the Author: Phil

7 Comments

  1. Hello Phil, first of all, congratulations for such a good guide, it was very useful to me.

    I have a problem after pointing the docker daemon to /mnt/volume/docker. Although it works fine, it is waaay much slower than it was before at /var/lib/docker, containers take much longer to be created, erased, and processed. Why could this happen?

  2. Hi Alfonso, thanks for the feedback. Is it possible that your other volume uses a different storage tier?

    I recommend checking that it’s still using SSD or NVMe storage.

  3. If you are using RHEL os then just go to /etc/sysconfig/docker file and update below
    other_args=”–graph=”

    restart docker services. It will pickup from new path.

Leave a Reply

Your email address will not be published. Required fields are marked *