๐ณ 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/fstabso 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/dockerto 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 pruneonce 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!
