1

I am using Docker 18.09 and I am trying to build some images for my work. The problem is that the images are always inside the root directory, precisely the /var/lib/docker/overlay2 are in /var/docker/. As there isn't enough space in my root directory, so I want to change this default directory to my other disk but none of the solutions I have looked upon the internet have worked for me.

I have gone through these already but none of them are working:

https://forums.docker.com/t/how-do-i-change-the-docker-image-installation-directory/1169

https://medium.com/@ibrahimgunduz34/how-to-change-docker-data-folder-configuration-33d372669056

How to change the docker image installation directory?

HimalayanCoder
  • 9,630
  • 6
  • 59
  • 60
  • Possible duplicate of [How to change the docker image installation directory?](https://stackoverflow.com/questions/24309526/how-to-change-the-docker-image-installation-directory) – David Maze Jul 10 '19 at 09:42

4 Answers4

2

The default directory to store docker related data (containers, images and so on) is /var/lib/docker.

To override this default location use -g option.

While starting docker deamon use -g option.

dockerd -g /mnt/path/to/docker/dir

In your case, the best option is to attach some external storage to machine at some mountpoint. And mention that mountpoint in -g option.

Hope this helps.

Update:

-g option is deprecated. Use --data-root option. Check this.

mchawre
  • 10,744
  • 4
  • 35
  • 57
1

I also faced similar issue with containerd used with rke2, I am using RKE2 with containerd and its storing all the images on /var/lib/rancher/rke2, which was causing VMs'/' partition getting full all the time. I wanted to move containerd root directory to custom directory

I changed rke agent start command in rke service file and it worked.

  1. Created a new FS /containerdata/containerd and configured rke service to point to this directory for containerd data
  2. change /usr/local/lib/systemd/system/rke2-agent.service
ExecStart=/usr/local/bin/rke2 agent --data-dir /containerdata/containerd/
  1. Reload and retart rke2-agent.service
systemctl daemon-reload
systemctl restart rke2-agent.service
  1. This may cause pods to be unstable, but system gets stable over time.
0

I used the following technique when i too ran into this problem and using a different filesystem which had a larger size was the only available option left.

  1. Stop the docker service. sudo systemctl stop docker.service

  2. Edit the docker.service file to include the new directory in the ExecStart line as below. ExecStart=/usr/bin/dockerd -g /u01/docker -H fd:// --containerd=/run/containerd/containerd.sock

Reload daemon sudo systemctl daemon reload

Restart docker sudo systemctl start docker.service

Note: the docker.service file path is shown when you ran the systemctl stop docker.service

A much simpler and straightforward way out for this problem is to simply create a symbolic link to the new path once you move the existing folder like below.

sudo systemctl stop docker.service

sudo mv /var/lib/docker/ /

sudo ln -s / /var/lib/docker

sudo systemctl start docker.service

0

The Linux way to do this is just create a symbolic link from /var/lib/docker to a new location.

Here is an example

sudo systemctl stop docker
sudo rsync -aqxP /var/lib/docker/ /mnt/docker
sudo rm -rf /var/lib/docker
sudo ln -s /mnt/docker /var/lib/docker
sudo systemctl start docker
Goddard
  • 2,863
  • 31
  • 37