Managing docker data

Posted by Souptik Datta on 2023-06-17 @ 13:45:23
Hello 👋! Are you currently utilizing Docker and experiencing a nearly full disk space? If so, this presents a favorable occasion to assess the overall memory usage attributed to the data stored by Docker for its seamless operation.
Note that there might be other reasons also for which the disk space is filling up. But here he will take up a potential cause i.e Docker.
When comparing Docker to a local native setup, it becomes evident that Docker requires significantly more storage space and computational resources. As a dedicated Docker user, I typically have three to four sites running simultaneously, with an additional four to five sites that may be temporarily stopped but still retain their data. Consequently, it's understandable that Docker consumes a substantial amount of data storage with the cumulative data of all the containers.
First, let's find the location where Docker stores all its data. By default docker stores all its data in /var/lib/docker folder. Let's now find out how much space is occupied by the folder.
sudo du -sh /var/lib/docker
For me, the output of this command was 37G 😱.
I had an additional disk partition with a significant amount of available memory. Therefore, my objective was to transfer the data from the root partition to this alternate partition in order to conserve space on the main root partition. If you don't have available space within your system, you can achieve the same outcome by utilizing an external drive or storage device.
So, let's do some transfer 🧹.

Stop docker daemon

sudo service docker stop

Copy the docker data directory

Copy your docker data directory to somewhere you like to (that is to some other disk that has extra space).
sudo rsync -aP /var/lib/docker/ /path/to/your/docker

Add configuration to tell docker about the data directory path

Now inside the folder, /etc/docker there will be a file called daemon.json. If that is not present then you can create that. It is a simple JSON configuration file. Now inside that file you just have to put one extra configuration -
{ "data-root": "/path/to/your/docker" }
If other configurations are present then keep them as it is.

Start docker daemon

Before starting the docker daemon, just rename the old docker data directory, just to ensure that docker is still not using the old data directory.
sudo service docker start

Delete old data directory

Now once you ensure everything is working fine, you can go ahead and delete the old data directory.
Should we send you a notification when new blogs are published?👇
Built with 💻 by Souptik