How to change Docker data directory in ubuntu
If you have a lot of Docker images, containers, volumes etc on your root partition and removing all unnecessary stuff with docker system prune didn’t help- you might want to move your Docker data out of the root partition to a different partition (for example, /home partition).
Here are the steps to do that.
Stop the Docker daemon if it is running
sudo service docker stop
Copy existing data to the new location
- Create the new directory if it doesn’t exist already
mkdir -p /new/path(for example,mkdir -p /home/user/docker) - Copy the data over from
/var/lib/docker(Docker creates data there by default)sudo cp -a /var/lib/docker/ /new/path
Let docker know of this new location
Create
/etc/docker/daemon.jsonif doesn’t existsudo touch /etc/docker/daemon.jsonAdd/replace
"data-root"to"/new/path"(for example:"data-root": "/home/username/docker").
Important: ~/ shorthand will not work. For example, you can not put "data-root": "~/docker"
For a newly created file, the file will look like this:
1 | { |
Remove existing data
You may want to keep a backup first:
sudo mv /var/lib/docker /var/lib/docker-backup
and delete these once you confirm docker is working finesudo rm -rf /var/lib/docker-backupOR, delete them parmanently now 🤷
sudo rm -rf /var/lib/docker
Start docker and check if everything is running like before
sudo service docker start
How to change Docker data directory in ubuntu
https://mehamasum.github.io/blog/2021/7/how-to-change-docker-data-directory-in-ubuntu/