Note
This is a quick reference guide on how to set up your Docker usage such that docker images are stored on an external drive.
If, like me, you are constantly having to worry about limited disk space on your host device where you doing your development, it would likely benefit you to have the images stored separately.
Warning
This guide makes a loose assumption that your external drive will be mounted on the following:
/home/$USER/Mounts
Modify the above file path based on where you have actually mounted your external drive. This guide aims to shift docker storage onto external drive via the following 2 directories:
/home/$USER/Mounts/docker/home/$USER/Mounts/containerd
Check mount:
df -h
mountConfirm external path exists:
ls /home/$USER/MountsStop everything:
sudo systemctl stop docker.socket
sudo systemctl stop docker.service
sudo systemctl stop containerd
Kill leftovers:
sudo pkill -9 dockerd
sudo pkill -9 containerd
Verify nothing remains:
ps aux | egrep "docker|containerd"
Expected:
Only grep process visible
sudo rm -rf /run/containerd/*
Why:
containerd caches runtime state avoids βstill using old rootβ issues
Example:
sudo mkdir -p /home/$USER/Mounts/docker
sudo mkdir -p /home/$USER/Mounts/containerd
Set permissions:
sudo chown root:root /home/$USER/Mounts/docker
sudo chown root:root /home/$USER/Mounts/containerd
sudo chmod 711 /home/$USER/Mounts/docker
sudo chmod 711 /home/$USER/Mounts/containerd
If migrating Docker root:
sudo rsync -aHAX --numeric-ids /var/lib/docker/ /home/$USER/Mounts/docker/
If old Docker root already elsewhere:
Adjust source accordingly
sudo rsync -aHAX --numeric-ids /var/lib/containerd/ /home/$USER/Mounts/containerd/
Verify sizes:
sudo du -sh /var/lib/containerd
sudo du -sh /home/user/Mounts/containerdThey should closely match.
Edit:
sudo nano /etc/docker/daemon.json
Example:
{
"data-root": "/home/$USER/Mounts/docker"
}Optional NVIDIA runtime example:
{
"data-root": "/home/$USER/Mounts/docker",
"runtimes": {
"nvidia": {
"args": [],
"path": "nvidia-container-runtime"
}
}
}Edit:
sudo nano /etc/containerd/config.toml
Find:
root = "/var/lib/containerd"
Change to:
root = "/home/$USER/Mounts/containerd"
Leave state unchanged:
state = "/run/containerd"
Reload systemd:
sudo systemctl daemon-reexec
sudo systemctl daemon-reload
Start containerd FIRST:
sudo systemctl start containerd
Then Docker:
sudo systemctl start docker
Check Docker:
docker images
docker ps -a
Verify image(s) still exists.
Check Docker root:
docker info | grep "Docker Root Dir"
Expected:
/home/$USER/Mounts/docker
Check containerd root:
containerd config dump | grep root
Expected:
/home/$USER/Mounts/containerd
Check for active use:
sudo lsof +D /var/lib/containerd
sudo lsof +D /var/lib/docker
If no output:
Safe to remove old directories
Example:
sudo rm -rf /var/lib/containerd
sudo rm -rf /var/lib/docker
ONLY if:
- Everything works.
- Images visible.
- Containers running successfully.
π₯ Looking To Undo The Above? β¬οΈ
Instructions π
sudo rm -rf /run/containerd/*Remove the
data-rootvariable from/etc/docker/daemon.json:Comment out the previously-added
rootandstatevariables:Verify βοΈ