Change Docker Default Root Data Directory

Calvine Otieno
2 min readJun 7, 2023
Image source https://logos-world.net/wp-content/uploads/2021/02/Docker-Logo-700x394.png

Docker has become the de facto standard for most applications when it comes to container implementation.

When you install docker with default settings, especially on Debian, it will be storing Docker images, containers and volumes in /var/lib/docker. You can verify this by running this in your terminal:

docker info | grep 'Docker Root Dir

And the output will be:

check the docker root directory

This directory doesn't have enough space which will be an issue in the long run. With time if you are using Docker, you will start to run out of space on this /var partition. At this point, you will need to add more space to that partition or do a cleanup. I used to face this issue too, especially with EC2 instances running self-hosted Gitlab Runners with docker-executor with all of a sudden no space left on the device error.

Steps to change the directory even after you have your containers running

Edit the docker daemon json file located at /etc/docker/daemon.json. This file exists by default but if it doesn’t create it. Your new file should look something like this:

{
"data-root": "/path/to/new/docker-data"
}

Please change "/path/to/new/docker-data" accordingly.

Stop docker by:

sudo systemctl stop docker

Verify docker has been stopped:

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

Copy the files to your new docker directory.

sudo rsync -axPS /var/lib/docker/ /path/to/new/docker-data

This can take long depending on the amount of data you have.

Bring docker back up

sudo systemctl start docker

Verify docker is up and is using the new configured location

docker info | grep 'Docker Root Dir

Check your containers has started and running as expected

docker ps

For safety purposes, leave copies on the server for a few days as you watch and make sure no issue with the new setup then you can delete it.

sudo rm -r /var/lib/docker

References

This is all for now. I hope you have learnt something and enjoyed reading the article.

If you have any questions or consultation work on DevOps and DevSecOps drop me a private message.

Follow me on GitHub for more about DevOps and DevSecOps.

Thanks for reading. Let’s connect on Twitter and LinkedIn 😁.

--

--