<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="../assets/xml/rss.xsl" media="all"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Diego A. Carrasco Gubernatis | Personal Website (Posts about server)</title><link>https://diegocarrasco.com/</link><description></description><atom:link href="https://diegocarrasco.com/categories/server.xml" rel="self" type="application/rss+xml"></atom:link><language>en</language><copyright>Contents © 2026 &lt;a href="mailto:hi@diegocarrasco.com"&gt;Diego Carrasco G.&lt;/a&gt; </copyright><lastBuildDate>Wed, 11 Nov 2026 09:05:12 GMT</lastBuildDate><generator>Nikola (getnikola.com)</generator><docs>http://blogs.law.harvard.edu/tech/rss</docs><item><title>How to change docker root data directory and why would you want to do that (hint: to optimize space)</title><link>https://diegocarrasco.com/change-docker-data-directory-vps-optimization/</link><dc:creator>Diego Carrasco G.</dc:creator><description>&lt;figure&gt;&lt;img src="https://diegocarrasco.com/images/social-images/change-docker-data-directory-vps-optimization.jpg"&gt;&lt;/figure&gt; &lt;h2 id="context"&gt;Context&lt;/h2&gt;
&lt;p&gt;I have a small vps for some personal projects which has 20 gb storage. I have one project there, and a few day 
ago I no longer had any available space. That meant docker services went down (and so the small project). I 
knew the project should not use that much space, and I found out that &lt;code&gt;docker&lt;/code&gt; was using around 10gb space for 
&lt;code&gt;overlay2&lt;/code&gt;. &lt;/p&gt;
&lt;p&gt;The whole &lt;code&gt;overlay2&lt;/code&gt; situation is enough for another post, but I found out (after researching and 
reading a lot of articles) that it grows pretty fast, and it is really hard to clean. &lt;code&gt;òverlay2&lt;/code&gt; is one of the ways 
docker synchronizes the filesystem, and it duplicates data. &lt;/p&gt;
&lt;p&gt;I decided to change the root data directory (by default at &lt;code&gt;/var/lib/docker&lt;/code&gt;) to another volume attached to my vps, 
which I can increase in space if needed. &lt;/p&gt;
&lt;h2 id="steps"&gt;Steps&lt;/h2&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;Important:&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Make a backup&lt;/strong&gt; of all your information (volumes, containers and everything that you cannot automatically generate 
again.)&lt;/p&gt;
&lt;h3 id="1-stop-docker-service"&gt;1. Stop docker service&lt;/h3&gt;
&lt;div class="code"&gt;&lt;pre class="code literal-block"&gt;sudo&lt;span class="w"&gt; &lt;/span&gt;systemctl&lt;span class="w"&gt; &lt;/span&gt;stop&lt;span class="w"&gt; &lt;/span&gt;docker
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;on older systems&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre class="code literal-block"&gt;sudo&lt;span class="w"&gt; &lt;/span&gt;service&lt;span class="w"&gt; &lt;/span&gt;docker&lt;span class="w"&gt; &lt;/span&gt;stop
&lt;/pre&gt;&lt;/div&gt;

&lt;h3 id="2-edit-etcdockerdaemonjson-or-create-it-if-it-does-not-exist"&gt;2. Edit  &lt;code&gt;etc/docker/daemon.json&lt;/code&gt; or create it if it does not exist&lt;/h3&gt;
&lt;div class="code"&gt;&lt;pre class="code literal-block"&gt;nano&lt;span class="w"&gt; &lt;/span&gt;/etc/docker/daemon.json
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Add the following line &lt;em&gt;if empty&lt;/em&gt;&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre class="code literal-block"&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;
&lt;span class="w"&gt;   &lt;/span&gt;&lt;span class="nt"&gt;"data-root"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"/path/to/your/new/docker/root"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;or, &lt;em&gt;if there is already something there&lt;/em&gt;, add just the key, appending a comma in the previous line&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre class="code literal-block"&gt;&lt;span class="nt"&gt;"data-root"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"/path/to/your/new/docker/root"&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;so that is looks like this:&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre class="code literal-block"&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="nt"&gt;"another-key"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"another value"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="nt"&gt;"data-root"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"/path_to_new_root/docker"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Important&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;This will &lt;strong&gt;change&lt;/strong&gt; your data directory, &lt;strong&gt;not move&lt;/strong&gt; it. Your data will not be there when you restart the docker 
  service.
  All the containers and volumen will be created from zero.&lt;/li&gt;
&lt;li&gt;This will recreate the whole docker data directory structure on the destination, as docker need it to run properly.&lt;/li&gt;
&lt;li&gt;Doing this is similar to removing all your containers, volumes and so on. (although in this case they are still on 
  the old directory until you remove them manually)&lt;/li&gt;
&lt;li&gt;If you use a volume, another hard-drive or similar as the new destination, make sure it has enough free space 
  available. I would recommend no less than 10 gb for small projects, with the possibility of increasing it. &lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="3-restart-docker"&gt;3. Restart docker&lt;/h3&gt;
&lt;div class="code"&gt;&lt;pre class="code literal-block"&gt;sudo&lt;span class="w"&gt; &lt;/span&gt;systemctl&lt;span class="w"&gt; &lt;/span&gt;restart&lt;span class="w"&gt; &lt;/span&gt;docker
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;or, on older systems&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre class="code literal-block"&gt;sudo&lt;span class="w"&gt; &lt;/span&gt;service&lt;span class="w"&gt; &lt;/span&gt;docker&lt;span class="w"&gt; &lt;/span&gt;start
&lt;/pre&gt;&lt;/div&gt;

&lt;h3 id="4-rebuild-your-projects-and-restore-backups-if-necessary"&gt;4. Rebuild your projects and restore backups if necessary&lt;/h3&gt;
&lt;p&gt;If you use docker-compose for your projects, just run &lt;code&gt;docker compose up -d --build&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;If you have database and data backups, restore them.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;With this you have now changed the root data directory from &lt;code&gt;/var/lib/docker&lt;/code&gt; to &lt;code&gt;/path_to_new_root/docker&lt;/code&gt;&lt;/p&gt;
&lt;h2 id="references"&gt;References&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="https://diegocarrasco.com/change-docker-data-directory-vps-optimization/"&gt;https://tienbm90.medium.com/how-to-change-docker-root-data-directory-89a39be1a70b&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://diegocarrasco.com/change-docker-data-directory-vps-optimization/"&gt;https://docs.docker.com/config/daemon/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://stackoverflow.com/q/36014554/624088"&gt;https://stackoverflow.com/questions/36014554/how-to-change-the-default-location-for-docker-create-volume-command&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://stackoverflow.com/q/36014554/624088"&gt;https://www.ibm.com/docs/en/z-logdata-analytics/5.1.0?topic=compose-relocating-docker-root-directory&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://stackoverflow.com/a/50726177/624088"&gt;https://stackoverflow.com/questions/32070113/how-do-i-change-the-default-docker-container-location/50726177#50726177&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</description><category>docker</category><category>docker space issues</category><category>docker-compose</category><category>linux</category><category>Overlay2</category><category>server</category><category>ubuntu</category><category>vps</category><guid>change-docker-data-directory-vps-optimization</guid><pubDate>Thu, 23 Nov 2023 09:12:35 GMT</pubDate></item></channel></rss>