Skip to content

Instantly share code, notes, and snippets.

@zeagord
Last active April 17, 2017 12:45
Show Gist options
  • Select an option

  • Save zeagord/ca2556926680556d017edc2f6dc58497 to your computer and use it in GitHub Desktop.

Select an option

Save zeagord/ca2556926680556d017edc2f6dc58497 to your computer and use it in GitHub Desktop.
How to change the default docker image location in a virtual machine

PROBLEM

We ran in to a lot of space issues with storing docker images in Azure cloud VMs. Because by default docker images, layers and containers are stored in the

/var/lib/docker

It is the same with the Amazon AWS as well.

The problem with this is most of the virtual machines have a very limited space in the OS Disk and the space can run out very soon. It is wise to save the docker images and attach your data containers to point a mount volume.

SOLUTION 1

So if you ever run in to space problems. Do the following steps:

  1. sudo service docker stop
  2. sudo rm -rf /var/lib/docker/
  3. sudo service docker start

If you do the above steps, you will be able to reclaim the spaces. But it is not ideal to remove the containers and images frequently.

SOLUTION 2

So i started mounting my images in the data disks instead of OS disks which you can frequently schedule for a backup as well.

  1. Stop the Docker service if it is already running
sudo service docker stop
  1. Make a back up of the existing docker directory (Change your mount directory-> /mnt/resource)
sudo tar -zcC /var/lib docker > /mnt/resource/var_lib_docker-backup-$(date +%s).tar.gz
  1. Move the /var/lib/docker directory to your new partition
sudo mv /var/lib/docker /mnt/pd0/docker
  1. Make a symlink to the new mount point
sudo ln -nfs /mnt/resource/docker /var/lib/docker
  1. Take a look at the directory to see and verify whether the symbolic link has been created
sudo ls -l /var/lib/docker
  1. Start docker service again
sudo service docker start
  1. Restart your containers

  2. Check the mounted volume size using

df -h

Credits:

Referred and Modified from: https://forums.docker.com/t/how-do-i-change-the-docker-image-installation-directory/1169

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment