Relocating Docker's Data Storage: A Step-by-Step Guide

Optimize Docker's performance with our guide to moving its storage directory. Boost efficiency and tailor your setup to your specific needs.

Relocating Docker's Data Storage: A Step-by-Step Guide

In this guide, I'm walking you through the process of relocating Docker's storage directory. Docker, by default, stores its data in the /var/lib/docker directory.

However, there may be instances where you'd prefer Docker to store its data elsewhere - perhaps for the sake of organized storage or you have a larger disk space available on a different partition. Whatever your reasons might be, it's possible to configure Docker to use a different storage location.

We'll be moving Docker's data from its default location to a new directory, /media/docker . Why? Because I personally prefer the /media directory. Do you prefer something else? Cool. Use that.

1) Prepare the new storage directory: Create the directory where you want Docker to store its data:

sudo mkdir -p /media/docker

2) Stop the Docker services: Before moving any data, make sure the Docker service and socket are stopped:

sudo systemctl stop docker.service
sudo systemctl stop docker.socket

3) Move Docker data to the new location: Copy Docker's existing data to your new location using rsync. If you don't have rsync installed, you can install it using sudo apt-get install rsync:

sudo rsync -aqxP /var/lib/docker/ /media/docker

When you check the directory contents, they should look similar to the below:

bhensel-ca@gova11y:~$ sudo ls /media/docker
buildkit  containers  engine-id  image  network  overlay2  plugins  runtimes  swarm  tmp  volumes

4) Update the Docker system config: Open the Docker systemd service file in a text editor with root privileges

sudo nano /lib/systemd/system/docker.service

5)Modify the Docker service execution command: In the Docker service configuration, change the ExecStart line from:

ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

To:

ExecStart=/usr/bin/dockerd --data-root /media/docker -H fd:// --containerd=/run/containerd/containerd.sock

6) Reload Docker configuration: After saving and closing the file, reload the systemd daemon to apply your changes:

sudo systemctl daemon-reload

7) Restart Docker

sudo systemctl start docker.service
sudo systemctl start docker.socket

8) Check the status of the Docker Service

sudo systemctl status docker.service

9) Verify the new data directory: You can confirm that Docker is now using the new directory by inspecting the command line used to start the Docker daemon:

ps aux | grep -i docker | grep -v grep

Your output should include --data-root /media/docker indicating that Docker is now using the new data directory. This completes the process of changing Docker's default storage location.

These instructions may vary depending on your os and other things. Do your own research and please don't blindly run my above sudo's