viernes, 5 de marzo de 2021

How to change docker root directory to a new location

 


Recently had notifications about lower space in / so, that space was used by docker images I've downloaded recently.

Because I have partitioned the disk in /, /home and other partitions, obviously /home has much more space than /.

Searching some information about this I used two solutions I've read. One for save new images in a new location and the other one to move the current downloaded images.

To change the place where new images will be downloaded:

We need to know where currently are saved, we have to run:

docker info


This can be changed editing the /lib/systemd/system/docker.service file.

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

Edit the line starting with ExecStart and add this -g /path/to/your/new/location

For example, if it's something like this:

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

Change it to (for example in my personal case):

ExecStart=/usr/bin/dockerd -g /home/gamliel/containers -H fd:// --containerd=/run/containerd/containerd.sock

Save the file and restart the daemon:

sudo service docker restart

When I restarted the service showed me a warning:

Warning: The unit file, source configuration file or drop-ins of docker.service changed on disk. Run 'systemctl daemon-reload' to reload units.

But it provides the solution, so I ran:

sudo systemctl daemon-reload

And everything came fine... I decide restart again the service to confirm zero errors/warnings and was fine.

sudo service docker restart

Now, if I run docker info command this is the output regarding to the location where docker images will be downloaded:


Well, everything is fine changing the Docker Root Dir to a new location, now it's time to move the current images to the new location to give more free space to /.

Step 1. Stop docker daemon: sudo /etc/init.d/docker stop

Step 2. Make sure that there are no docker related processes: ps aux | grep docker

Step 3. Move the contents of /var/lib/docker to your new location: sudo mv /var/lib/docker /home/gamliel/containers

Step 4. Start docker daemon: sudo /etc/init.d/docker start


Where I read about moving the content to the new location says something to create a symlink but I skip that step because previously I changed where the new images will be downloaded.

That's all, thanks for reading my personal notes about what worked for me.

Sources where I found both solutions:
1. https://hsadanuwan.medium.com/how-to-change-docker-default-data-directory-f884dac76c1f
2. https://www.crybit.com/change-default-data-image-directory-docker/

Bai.

UPDATE 20210307: After moving docker root directory to another one and changing the location in order to appear the right one when I run docker info everything was fine, 2 days after when I wanted to download other docker images (CodeIgniter) and listed the images with docker images just the CodeIgniter image appeared in the list, so I searched how to fix this and found another articles but this one https://www.guguweb.com/2019/02/07/how-to-move-docker-data-directory-to-another-location-on-ubuntu/ fixed the issue. Kudos to Augusto. (Si vienes por aqui, Augusto... Gracias compadre! :D)

The solution was:

1. Stop docker service with: sudo service docker stop

2. Create a file named daemon.json inside of /etc/docker with: sudo nano /etc/docker/daemon.json

3. Paste this inside that new added file:

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

where /path/to/your/docker is the directory where you will move the content of /var/lib/docker

4. In the cited article says this way how to copy the current data to the desired new location:

sudo rsync -aP /var/lib/docker/ /path/to/your/docker

Despite that in the last cited article (the step that made all work fine again) says that you have to rename the old docker directory, in the below steps I did before I moved the content so it wasn't necessary for me.

5. Restart the docker daemon with: sudo service docker restart

6. Test. I tested trying to list the images with docker images and with docker ps -a and everything is fine.

Hope the update saves time to anyone that could stumble with this blogpost. I've not posted how to install docker on Debian 10 because there are plenty of good articles over there that is easy to follow, but decided to post about this because I faced the issue that no enough space on disk ("/").




0 comentarios:

 

Copyright © El igloo de Tux Design by O Pregador | Blogger Theme by Blogger Template de luxo | Powered by Blogger