Skip to content

Instantly share code, notes, and snippets.

@emanuelhfarias
Last active May 19, 2020 22:43
Show Gist options
  • Save emanuelhfarias/fe415d722174604dc5c8b833029ae09d to your computer and use it in GitHub Desktop.
Save emanuelhfarias/fe415d722174604dc5c8b833029ae09d to your computer and use it in GitHub Desktop.
How to change the Docker Images directory
#!/bin/bash
# How to change the Docker images directory (default dir=/var/lib/docker)
# The simplest method is to create a symbolic link that maps your destination partition and the original docker images directory
# see REF: https://forums.docker.com/t/how-do-i-change-the-docker-image-installation-directory/1169
# remove all containers
docker ps -aq | xargs docker rm
# stop docker daemon
systemctl stop docker
# move all images from /var/lib/docker to the destination partition/dir (mount it first)
mv /var/lib/docker $destination
# create a symbolic link that maps your destination and the original docker images directory
ln -s $destination /var/lib/docker # the symbolic link must be in /var/lib/ with the name: 'docker' pointing to $destination
# start docker daemon
systemctl start docker
@emanuelhfarias
Copy link
Author

emanuelhfarias commented May 18, 2020

The preferred way nowadays is to create /etc/docker/daemon.json file and add:

{
    "data-root": "/new/docker/root"
}

Storage Driver tips:
EXT4 -> use overlay2
NTFS -> use devicemapper

To change the Storage Driver that Docker uses, add to /etc/docker/daemon.json file:

{
  "storage-driver": "devicemapper"
}

to automount partitions, add to /etc/fstab:
/dev/disk/by-uuid/xxx-xxx-xxx /mnt/store ext4 rw,suid,dev,exec,auto,async 0 0

On fedora, prefer /run/media/store

If system asks password to open store, just change ownership:
sudo chown user:user /mnt/store

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