Back to all posts

๐Ÿณ Moving Docker Storage to External Drive

Quick and straightforward guide to relocate Docker data directory to external storage using symlinks for expanded capacity and better performance.

๐Ÿณ Moving Docker Storage to External Drive

๐Ÿณ Moving Docker Storage to External Drive

Running out of disk space because of Docker? Been there! Docker loves to eat up storage with all those images, containers, and volumes. Here's how I usually move Docker's data to an external drive when my main disk starts crying for space.

๐Ÿ“ What We're Doing

Simply moving all Docker stuff (images, volumes, containers, etc.) from the default /var/lib/docker to an external HDD, like /mnt/hdd/docker. Nothing fancy, just more space!

โš ๏ธ Before We Start

Make sure your external HDD is ready:

  • โœ… Formatted to EXT4 (you know the drill)
  • โœ… Already mounted somewhere like /mnt/hdd
  • โœ… Set to auto-mount via /etc/fstab so it's there after reboot

Haven't done this yet? Check out my mounting guide first - it's pretty straightforward.

Let's Do This

๐Ÿ›‘ Step 1: Stop Docker

First things first, gotta stop Docker:

sudo systemctl stop docker

Double-check it's actually stopped:

sudo systemctl status docker

๐Ÿ“‚ Step 2: Move Everything to External Drive

Now the fun part - moving all that Docker data:

# Just move the whole thing
sudo mv /var/lib/docker /mnt/hdd/docker

Quick check to see if it worked:

ls -la /mnt/hdd/docker/

๐Ÿ”— Step 3: Create the Magic Symlink

This is the clever bit - create a symlink so Docker thinks nothing changed:

# Create the link back to where Docker expects it
sudo ln -s /mnt/hdd/docker /var/lib/docker

Check if the symlink looks right:

ls -la /var/lib/docker

โ–ถ๏ธ Step 4: Fire Up Docker Again

Time to start Docker back up:

sudo systemctl start docker

โœ… Step 5: Make Sure Everything's Working

Let's verify Docker is happy with the new setup:

docker info | grep "Docker Root Dir"

Should show:

Docker Root Dir: /var/lib/docker

Docker still thinks it's in the old location (thanks to our symlink), but physically it's on your external drive now.

Quick test to make sure everything works:

# Try pulling something small
docker run hello-world

# Check your existing stuff is still there
docker images
docker ps -a

๐Ÿงช Troubleshooting Tips

If Docker Fails to Start

Check Docker service logs:

journalctl -u docker.service -xe

More Tips & Tricks

Keep an Eye on Permissions

Sometimes Docker gets picky about file ownership:

# If things get weird, fix ownership
sudo chown -R root:root /mnt/hdd/docker

# And permissions
sudo chmod -R 711 /mnt/hdd/docker

Double-Check Your Symlink

Make sure the link looks right:

ls -la /var/lib/docker
# Should show: /var/lib/docker -> /mnt/hdd/docker

Watch Your Space

Keep tabs on how much room you have left:

df -h /mnt/hdd

Another Way: Skip the Symlink

Don't like symlinks? No problem! Here's another approach:

Tell Docker Where to Live

# Make Docker's config folder
sudo mkdir -p /etc/docker

# Create the config file
sudo nano /etc/docker/daemon.json

Put this in the file:

{
  "data-root": "/mnt/hdd/docker"
}

Move and Restart

# Stop Docker
sudo systemctl stop docker

# Set up the new home
sudo mkdir -p /mnt/hdd/docker
sudo mv /var/lib/docker/* /mnt/hdd/docker/

# Fire it back up
sudo systemctl start docker

Why This Is Awesome

  • ๐Ÿš€ Way More Space: No more "disk full" nightmares when Docker goes crazy with images
  • โšก Better Performance: Your system drive can breathe while Docker does its thing elsewhere
  • ๐Ÿ”ง Easier to Manage: All your Docker stuff in one place
  • ๐Ÿ’พ Save Your SSD: Less wear and tear on your main drive
  • ๐Ÿ’ฐ Cheaper: External storage costs way less than upgrading your system drive

Keeping Things Tidy

See What's Taking Up Space

docker system df

Clean House Regularly

# Get rid of all the junk Docker accumulates
docker system prune -a

# Just clean up old volumes
docker volume prune

Check Your Drive's Health

sudo smartctl -a /dev/sda

All Done!

That's it! You've successfully moved Docker to your external drive without breaking anything. Here's what we just did:

๐ŸŽฏ What We Got Done:

  • โœ… Moved all Docker stuff from /var/lib/docker to your external drive
  • โœ… Set up a symlink so Docker doesn't even know we moved anything
  • โœ… Tested everything to make sure it all still works
  • โœ… Got tons more space for all those Docker images

๐Ÿš€ Why This Rocks:

  • No More Space Issues: Pull all the images you want without worry
  • Your System Drive is Happy: Less stress on your main drive
  • Everything in One Place: All Docker stuff organized on the external drive
  • Easy to Upgrade: Just swap out the external drive when you need more space

๐Ÿ’ก Quick Cheat Sheet:

# The whole process in a nutshell:
sudo systemctl stop docker
sudo mv /var/lib/docker /mnt/hdd/docker
sudo ln -s /mnt/hdd/docker /var/lib/docker
sudo systemctl start docker
docker info | grep "Docker Root Dir"

โš ๏ธ Don't Forget:

  • Make sure your external drive is always mounted when you start Docker
  • Keep backups of anything important (you know, just in case)
  • Run docker system prune once in a while to clean up the junk
  • If something seems off, check the Docker logs

Now go forth and containerize everything without worrying about storage!