Check out the WordPress site for the same content in a new experience!
Managing docker data
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.
<pre class="wp-block-code"><code>sudo du -sh /var/lib/docker</code></pre>
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
<pre class="wp-block-code"><code>sudo service docker stop</code></pre>
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).
<pre class="wp-block-code"><code>sudo rsync -aP /var/lib/docker/ /path/to/your/docker</code></pre>
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 -
<pre class="wp-block-code"><code>{ "data-root": "/path/to/your/docker"}</code></pre>
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.
<pre class="wp-block-code"><code>sudo service docker start</code></pre>
Delete old data directory
Now once you ensure everything is working fine, you can go ahead and delete the old data directory.