If you’re running a lot of containers or have pulled a lot of images, Docker can get a little bit greedy with the disk space it is using. By default Docker will use /var/lib/docker for storage. What happens if you’re running out of space and want to move this to another location?

In this article we show you how to easily move /var/lib/docker to another location on your Docker host.

Try some house-keeping first

Before you go ahead and decide to move your /var/lib/docker directory, it is worth doing some basic house-keeping first. Namely we can:

  • Clean-up any dangling volumes
  • Clean-up any dangling images
  • Clean-up any unused containers

Depending on how you’ve been using your Docker installation, this can return many gigabytes of space to you and may get you out of having to move the directory. For the sake of this article we’ll assume that you know what dangling images and volumes are.

Try the following:

That hasn’t helped. I’m still running low on space

If after performing house-keeping you’re still running low on space, then you can consider moving the /var/lib/docker to another disk location.

Before you begin this process, make sure that you do the following:

  • Backup any critical volumes that are associated with your Docker usage
  • Backup any critical containers that are associated with your Docker usage

Whilst the move should be just fine, we always want to be able to rollback if we have to.

Preparing your new directory

So first thing first lets create the new location for your Docker data directory. We’ll assume you’re moving to /bigdisk/docker (obviously substitute this for wherever you’re actually going to move your Docker data directory to).

Use the following to create the directory and set the correct permissions:

Stop all containers and the Docker daemon

Next stop all running containers. If you can, then it is probably cleanest to remove all containers as well, but obviously this may not be possible if you want the containers to be re-started after you’ve done the move.

Once all containers are down stop, the Docker daemon. How you do this exactly will depend on the flavour of Unix you’re running. In the example here I carried out the instructions on a RHEL 7 host:

Create a config file for the Docker daemon

Next you’ll need to create a config file for the Docker daemon that tells it to use your new directory as storage. Again the exact location and format of the config file will depend on the flavour of Unix you’re running, but the upshot of it is that you want to start the Docker daemon with the -g flag.

Using the -g flag followed by your new directory location tells Docker to use that for data storage.

The following config file was for RHEL 7:

Make sure all Docker filesystems are unmounted:

Hopefully by stopping (and removing all containers) none of the filesystems used by Docker will be mounted. However it is worth double-checking as you won’t be able to move them if there are. Use the following to unmount all the filesystems:

Move the Docker filesystem and re-start the daemon

Once everything is unmounted, now simply move the existing filesystem, link the old filesystem and then re-start the Docker daemon:

Re-start your containers

Now that you’ve moved the directory, using “docker info” from the command line will show you that the daemon has a lot more free space to play with.

If it doesn’t, go back through the above steps to make sure you’ve done everything correctly.

You are now free to re-start your containers as needs be, knowing that you’ve got plenty of space to play with.

Founder and principal consultant of Rob Blake Associates. Over 5 years hands-on experience building and deploying production web applications on AWS. 10 years professional experience as a Java developer. Previously technical co-founder of The Law Wizard which was sold in June 2014.

12 thoughts on “Moving /var/lib/docker to another directory”

  1. I got into problems with docker starting by following your instructions exactly.
    Upon troubleshooting, I like to share.

    -> Unmount all docker mount

    # cd /var/lib/docker
    # umount ./*
    # mount | grep docker

    It’s important to do # rm -rf /var/lib/docker else linking will be directly mapped /var/lib/docker . It would be like /var/lib/docker/big-disk

    -> Kill all processes that mapped to docker mounts if rm -rf /var/lib/docker threw error (“Device or resource busy”)

    # find /proc/*/mounts | xargs grep [string] where [strings] /var/lib/docker/overlay/XXXX

    # cat /etc/systemd/system/docker.service.d/docker.conf
    ExecStart=/bin/dockerd –data-root /big-disk

    Good luck in moving docker path!

  2. Thanks for your article. There is a easier way to do it (It worked on my ubuntu 16.04) :

    cp /var/lib/docker /home/programs

    vim /etc/docker/daemon.json
    # add => “graph”: “/home/programs/docker” or { “graph”: “/home/programs/docker” } if empty

    reboot docker, then check with docker info (line Docker Root Dir: …… )

Leave a Reply

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