Skip to content

Instantly share code, notes, and snippets.

@cardboardcode
Last active May 20, 2026 05:20
Show Gist options
  • Select an option

  • Save cardboardcode/1f0f23232f0a925315c2a1361470d5b8 to your computer and use it in GitHub Desktop.

Select an option

Save cardboardcode/1f0f23232f0a925315c2a1361470d5b8 to your computer and use it in GitHub Desktop.
For People In A Hurry: How To Mount Docker Storage On An External Drive

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.

Why Do This?

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

Instructions πŸ“˜

πŸ”΅ STEP 1 β€” Verify External Drive

Check mount:

df -h
mount

Confirm external path exists:

ls /home/$USER/Mounts

πŸ”΅ STEP 2 β€” Stop Docker + containerd COMPLETELY

Stop 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

πŸ”΅ STEP 3 β€” Clear Runtime State

sudo rm -rf /run/containerd/*

Why:

containerd caches runtime state avoids β€œstill using old root” issues

πŸ”΅ STEP 4 β€” Create New Directories

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

πŸ”΅ STEP 5 β€” Copy Existing Docker Data (Optional)

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

πŸ”΅ STEP 6 β€” Copy Existing containerd Data

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/containerd

They should closely match.

πŸ”΅ STEP 7 β€” Configure Docker Root

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"
        }
    }
}

πŸ”΅ STEP 8 β€” Configure containerd Root

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"

πŸ”΅ STEP 9 β€” Restart Services CLEANLY

Reload systemd:

sudo systemctl daemon-reexec
sudo systemctl daemon-reload

Start containerd FIRST:

sudo systemctl start containerd

Then Docker:

sudo systemctl start docker

πŸ”΅ STEP 10 β€” Verify Migration

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

πŸ”΅ STEP 11 β€” Confirm Old Paths Are NOT Used

Check for active use:

sudo lsof +D /var/lib/containerd
sudo lsof +D /var/lib/docker

If no output:

Safe to remove old directories

πŸ”΅ STEP 12 β€” Remove Old Storage (ONLY AFTER VALIDATION)

Example:

sudo rm -rf /var/lib/containerd
sudo rm -rf /var/lib/docker

ONLY if:

  • Everything works.
  • Images visible.
  • Containers running successfully.
@cardboardcode

cardboardcode commented May 16, 2026

Copy link
Copy Markdown
Author

πŸŸ₯ Looking To Undo The Above? ⬇️

Instructions πŸ“˜

sudo systemctl stop docker.socket
sudo systemctl stop docker.service
sudo systemctl stop containerd
sudo pkill -9 dockerd
sudo pkill -9 containerd
sudo rm -rf /run/containerd/*

Remove the data-root variable from /etc/docker/daemon.json:

sudo nano /etc/docker/daemon.json

Comment out the previously-added root and state variables:

sudo nano /etc/containerd/config.toml
sudo systemctl daemon-reexec
sudo systemctl daemon-reload
sudo systemctl start containerd
sudo systemctl start docker

Verify βœ”οΈ

docker images
docker ps -a

@cardboardcode

cardboardcode commented May 16, 2026

Copy link
Copy Markdown
Author

πŸŸ₯ Unable to Build Docker Images (Post-Migration) ?

It could potentially be due to corrupted BuildKit bbolt database in data-root.

To fix this issue, refer to the steps below:

sudo systemctl stop docker
sudo rm -rf /home/$USER/Mounts/docker/buildkit
sudo systemctl start docker

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