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.