Use a Different Volume For Your Docker Images in Ubuntu
Use a Different Volume For Your Docker Images in Ubuntu

Increase your machine’s docker image capacity

Just as a warning, I take no responsibility for any damage or issues on your machine that you believe to have been caused by following the steps of this guide!

Here’s a quick guide I’ve written to expand your current docker image capacity on your machine, assuming you have another disk/volume you want to move your docker images too — or if you just quite like the idea of having them stored separately from your main volume.

My laptop has a very small SDD, which I was constantly hitting the limits of while doing docker based development— so I decided to move my docker installation to my much larger HDD.

I wrote this guide since I thought it might be useful for others. I’ve written it assuming an Ubuntu operating system, but it may be applicable to others, I just haven’t tried it anywhere else.

Sudo Up

You’ll be making some changes to some of the more hairier and riskier parts of your file system, so you’ll need to elevate your permissions:

sudo su

Note: The remainder of this guide will assume you have elevated your privileges.

Finding Your Device

Assuming you have an existing drive or a brand new drive you’ve just plugged into your machine, you can find your drive and the name of the device using Ubuntu’s Disks utility.

The drive I’ll be using for my docker installation is the 1.0TB Hard Disk and the partition is the already recently reformatted /dev/sda2 device. Note down whatever your device is called, you’ll need it for the next steps.

Note: Your Docker installation may not work out of the box with an NTFS filesystem and require further configuration (and trial and error). If your new drive is currently formatted under NTFS, I’d recommend that you reformat the device (if possible) to use a Linux filesystem such as ext4 .

The Steps

Assuming /dev/sda2 is your new disk — the following steps will stop your docker daemon, move your current docker directory, mount your new file system and then reinstate your docker installation on the new file system.

systemctl stop docker mv /var/lib/docker /var/lib/docker-backup mount /dev/sda2 /var/lib/docker cp -rf /var/lib/docker-backup/* /var/lib/docker systemctl start docker

Test out your newly expanded capacity by pulling a new docker image and running it — here’s a postgres image as an example:

docker pull postgres && docker run --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword -d postgres

If all goes well, the image will pull and run:

Using default tag: latest

latest: Pulling from library/postgres

27833a3ba0a5: Pull complete

ed00742830a6: Pull complete

dc611c2aceba: Pull complete

a61becab5279: Pull complete

8dcff41e7aea: Pull complete

820bf1bbf0d7: Pull complete

050804429905: Pull complete

782c81275334: Pull complete

bfb4aaa36ad6: Pull complete

9101c497b579: Pull complete

746ef6cad24f: Pull complete

f3d6bb76fd3b: Pull complete

32cf0a104c6f: Pull complete

de900772b4f7: Pull complete

Digest: sha256:ed0bee606e90ed40cf5ffcfafc3d27c53d7132b7950324598ac06cba75f3e4cb

Status: Downloaded newer image for postgres:latest

33a14d04702b01ba3a7470a633d8eda71352602eba617afc8fa60e6307ebc7bf

Try a few other things with your docker installation to confirm you’re happy with it.

Mount On Startup

Assuming everything is now fine, you’ll want to ensure your disk gets mounted on the /var/lib/docker directory permanently on start-up:

nano /etc/fstab

Add the following line at the end of the file, again assuming /dev/sda2 is your device and also assuming is formatted as an ext4 filesystem.

/dev/sda2 /var/lib/docker ext4 defaults 0 1

Note: If you’re unsure of the formatting of your filesystem you can confirm it on the Ubuntu Disks utility.

Rollback

If something goes wrong or for whatever reason, you want to return to your previous setup — you should be able to rollback in the following way:

systemctl stop docker umount /dev/sda2 mv /var/lib/docker-backup /var/lib/docker # Remove the additional line added to /etc/fstab (if applicable) systemctl start docker

Cleanup

If you’re happy with the increased capacity in your docker installation and all has gone to plan, you can now remove your backup:

rm -rf /var/lib/docker-backup

That’s It!

Thanks for reading, I hope you found it useful!
['Andy Macdonald']
['DevOps', 'Software Dev', 'Ubuntu', 'Docker', 'Technology']