{"id":118502,"date":"2024-05-26T12:34:58","date_gmt":"2024-05-26T12:34:58","guid":{"rendered":"https:\/\/linuxiac.com\/?p=118502"},"modified":"2024-05-26T16:00:32","modified_gmt":"2024-05-26T16:00:32","slug":"how-to-change-docker-data-directory","status":"publish","type":"post","link":"https:\/\/linuxiac.com\/how-to-change-docker-data-directory\/","title":{"rendered":"How to Change Docker&#8217;s Default Data Directory"},"content":{"rendered":"\n<p>Running out of disk space is one of the most common issues users face. When using containerized Docker services, reaching that point means that your deployments will stop working. This happens for one simple reason\u2014your Linux system becomes unable to execute any further write operations on the disk.<\/p>\n\n\n\n<p>Fortunately, there&#8217;s a practical solution. This article will guide you through the steps to relocate Docker&#8217;s storage path for images, volumes, containers, etc., ensuring that your projects continue smoothly without the hassle of disk space limitations.<\/p>\n\n\n\n<p>To clarify things, consider this: our disk is 99% full, dangerously close to rendering our entire Linux system inoperable. We&#8217;ve added a new 100GB disk to handle this unpleasant situation and mounted it as the &#8220;<em>\/data<\/em>&#8221; directory in our filesystem.<\/p>\n\n\n\n<p>We plan to move all Docker resources to this location, which frees up significant space on the &#8220;<em>\/<\/em>&#8221; root partition.&nbsp;This&nbsp;will ensure that not only Docker but also our Linux system as a whole continues to operate without any hiccups.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/linuxiac.com\/wp-content\/uploads\/2024\/05\/change-docker-data-dir-01.jpg\"><img decoding=\"async\" width=\"930\" height=\"296\" src=\"https:\/\/linuxiac.com\/wp-content\/uploads\/2024\/05\/change-docker-data-dir-01.jpg\" alt=\"The disk is running out of space.\" class=\"wp-image-118538\" srcset=\"https:\/\/linuxiac.com\/wp-content\/uploads\/2024\/05\/change-docker-data-dir-01.jpg 930w, https:\/\/linuxiac.com\/wp-content\/uploads\/2024\/05\/change-docker-data-dir-01-380x121.jpg 380w, https:\/\/linuxiac.com\/wp-content\/uploads\/2024\/05\/change-docker-data-dir-01-768x244.jpg 768w\" sizes=\"(max-width: 930px) 100vw, 930px\" \/><\/a><figcaption class=\"wp-element-caption\">The disk is running out of space.<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Step 1: Stop Docker Service<\/h2>\n\n\n\n<p>Before making any changes, ensure all your containers&nbsp;are stopped, and Docker is not running. You can do this using the following commands:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Bash\" data-shcb-language-slug=\"bash\"><span><code class=\"hljs language-bash\">sudo systemctl stop docker.service\nsudo systemctl stop docker.socket<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Bash<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">bash<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Of course, remember that this will cause all your Docker containers to shut down, so downtime is inevitable.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 2: Move the Docker&#8217;s Data Directory<\/h2>\n\n\n\n<p>Docker stores all its data in a default directory, including images, containers, volumes, and networks. On most Linux systems, this is typically &#8220;<em>\/var\/lib\/docker<\/em>.&#8221;&nbsp;While this works well for initial setups, as the number of projects grows, so does the data.<\/p>\n\n\n\n<p>Because of this, the next step is to move the existing Docker directory to a new location with more disk space, which, in our case, is the &#8220;<em>\/data<\/em>&#8221; directory. Let&#8217;s first create a subdirectory for it, for example, &#8220;<em>docker<\/em>,&#8221; where we plan to relocate and store our Docker data going forward.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Bash\" data-shcb-language-slug=\"bash\"><span><code class=\"hljs language-bash\">sudo mkdir \/data\/docker<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Bash<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">bash<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>It&#8217;s time to get to action. While you could use the recursive <code>cp<\/code> command or even <code>mv<\/code> to transfer data, we advise against this. Instead, the safest and most reliable method is the <code>rsync<\/code> command. If it isn&#8217;t already available on your Linux distro, you can easily add it by installing the &#8220;<em>rsync<\/em>&#8221;&nbsp;package through your package manager.<\/p>\n\n\n\n<p>To copy all data from the &#8220;<em>\/var\/lib\/docker<\/em>&#8221;&nbsp;to the &#8220;<em>\/data\/docker<\/em>&#8221;&nbsp;directory, we execute the command below, as the first argument is the source directory, and the second is the destination.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Bash\" data-shcb-language-slug=\"bash\"><span><code class=\"hljs language-bash\">sudo rsync -aP \/var\/lib\/docker\/ \/data\/docker\/<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Bash<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">bash<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/linuxiac.com\/wp-content\/uploads\/2024\/05\/change-docker-data-dir-02.jpg\"><img decoding=\"async\" width=\"930\" height=\"670\" src=\"https:\/\/linuxiac.com\/wp-content\/uploads\/2024\/05\/change-docker-data-dir-02.jpg\" alt=\"Move the Docker data directory to a new location.\" class=\"wp-image-118598\" srcset=\"https:\/\/linuxiac.com\/wp-content\/uploads\/2024\/05\/change-docker-data-dir-02.jpg 930w, https:\/\/linuxiac.com\/wp-content\/uploads\/2024\/05\/change-docker-data-dir-02-380x274.jpg 380w, https:\/\/linuxiac.com\/wp-content\/uploads\/2024\/05\/change-docker-data-dir-02-768x553.jpg 768w\" sizes=\"(max-width: 930px) 100vw, 930px\" \/><\/a><figcaption class=\"wp-element-caption\">Move the Docker data directory to a new location.<\/figcaption><\/figure>\n\n\n\n<p>Okay, what does this command do? The &#8220;<em>-a<\/em>&#8221;&nbsp;(archive mode) option permits recursive file copying while preserving all symbolic links, file permissions, user and group ownership, and timestamps. The &#8220;<em>-P<\/em>&#8221;&nbsp;option keeps partially transferred files if the transfer interrupts. It also shows the transfer progress for each file being copied.<\/p>\n\n\n\n<p>For more in-depth information about the <code>rsync<\/code> command, visit our comprehensive guide, &#8220;<a href=\"https:\/\/linuxiac.com\/rsync-command-in-linux-with-example\/\">Rsync Command in Linux with Practical Examples<\/a>.&#8221;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 3: Update the Docker Configuration<\/h2>\n\n\n\n<p>After copying the data, you must tell Docker where to find its new home. This can be done by editing the special Docker configuration file, &#8220;<em>daemon.json<\/em>,&#8221; in the &#8220;<em>\/etc\/docker<\/em>&#8221; directory.<\/p>\n\n\n\n<p>Note that a Docker installation doesn&#8217;t automatically create this file, so if you have not done any manual configurations before, it&#8217;s normal for the file to be missing. If that&#8217;s the case, create it and put it in the snippet below. Of course, remember to replace &#8220;<em>\/new\/docker\/root<\/em>&#8221; with the appropriate filesystem path for your setup.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Bash\" data-shcb-language-slug=\"bash\"><span><code class=\"hljs language-bash\">sudo nano \/etc\/docker\/daemon.json<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Bash<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">bash<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"JSON \/ JSON with Comments\" data-shcb-language-slug=\"json\"><span><code class=\"hljs language-json\">{ \n   <span class=\"hljs-attr\">\"data-root\"<\/span>: <span class=\"hljs-string\">\"\/new\/docker\/root\"<\/span>\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JSON \/ JSON with Comments<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">json<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/linuxiac.com\/wp-content\/uploads\/2024\/05\/change-docker-data-dir-03.jpg\"><img decoding=\"async\" width=\"930\" height=\"274\" src=\"https:\/\/linuxiac.com\/wp-content\/uploads\/2024\/05\/change-docker-data-dir-03.jpg\" alt=\"Change the default directory for Docker data storage.\" class=\"wp-image-118619\" srcset=\"https:\/\/linuxiac.com\/wp-content\/uploads\/2024\/05\/change-docker-data-dir-03.jpg 930w, https:\/\/linuxiac.com\/wp-content\/uploads\/2024\/05\/change-docker-data-dir-03-380x112.jpg 380w, https:\/\/linuxiac.com\/wp-content\/uploads\/2024\/05\/change-docker-data-dir-03-768x226.jpg 768w\" sizes=\"(max-width: 930px) 100vw, 930px\" \/><\/a><figcaption class=\"wp-element-caption\">Change the default directory for Docker data storage.<\/figcaption><\/figure>\n\n\n\n<p>Finally, save the file and exit.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 4: Start Docker &amp; <strong>Verify the New Setup<\/strong><\/h2>\n\n\n\n<p>Okay, the moment of truth. Start the Docker service:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Bash\" data-shcb-language-slug=\"bash\"><span><code class=\"hljs language-bash\">sudo systemctl start docker.socket\nsudo systemctl start docker.service<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Bash<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">bash<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Make sure the Docker service has started up smoothly and is fully operational.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"Bash\" data-shcb-language-slug=\"bash\"><span><code class=\"hljs language-bash\">sudo systemctl status docker<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Bash<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">bash<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/linuxiac.com\/wp-content\/uploads\/2024\/05\/change-docker-data-dir-06.jpg\"><img decoding=\"async\" width=\"928\" height=\"565\" src=\"https:\/\/linuxiac.com\/wp-content\/uploads\/2024\/05\/change-docker-data-dir-06.jpg\" alt=\"Check Docker's systemd service.\" class=\"wp-image-118646\" srcset=\"https:\/\/linuxiac.com\/wp-content\/uploads\/2024\/05\/change-docker-data-dir-06.jpg 928w, https:\/\/linuxiac.com\/wp-content\/uploads\/2024\/05\/change-docker-data-dir-06-380x231.jpg 380w, https:\/\/linuxiac.com\/wp-content\/uploads\/2024\/05\/change-docker-data-dir-06-768x468.jpg 768w\" sizes=\"(max-width: 928px) 100vw, 928px\" \/><\/a><figcaption class=\"wp-element-caption\">Check Docker&#8217;s systemd service.<\/figcaption><\/figure>\n\n\n\n<p>Finally, ensure that Docker is using the directory we specified for data storage as its default:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"Bash\" data-shcb-language-slug=\"bash\"><span><code class=\"hljs language-bash\">sudo docker info | grep <span class=\"hljs-string\">\"Docker Root Dir\"<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Bash<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">bash<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/linuxiac.com\/wp-content\/uploads\/2024\/05\/change-docker-data-dir-04.jpg\"><img decoding=\"async\" width=\"930\" height=\"99\" src=\"https:\/\/linuxiac.com\/wp-content\/uploads\/2024\/05\/change-docker-data-dir-04.jpg\" alt=\"Check Docker's default data storage directory.\" class=\"wp-image-118634\" srcset=\"https:\/\/linuxiac.com\/wp-content\/uploads\/2024\/05\/change-docker-data-dir-04.jpg 930w, https:\/\/linuxiac.com\/wp-content\/uploads\/2024\/05\/change-docker-data-dir-04-380x40.jpg 380w, https:\/\/linuxiac.com\/wp-content\/uploads\/2024\/05\/change-docker-data-dir-04-768x82.jpg 768w\" sizes=\"(max-width: 930px) 100vw, 930px\" \/><\/a><figcaption class=\"wp-element-caption\">Check Docker&#8217;s default data storage directory.<\/figcaption><\/figure>\n\n\n\n<p>This should display the new path, confirming that the relocation was successful.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Post-installation Steps<\/h2>\n\n\n\n<p>Ensure that all your Docker containers are functioning as expected. If they are not set to start automatically, run them manually one at a time and check for any irregularities in their operation.<\/p>\n\n\n\n<p><strong>Only after you have verified that everything is running smoothly<\/strong> can you proceed to free up disk space by removing the directory previously used by Docker to store data.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"Bash\" data-shcb-language-slug=\"bash\"><span><code class=\"hljs language-bash\">sudo rm -rf \/var\/lib\/docker\/<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Bash<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">bash<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Bottom Line<\/h2>\n\n\n\n<p>Relocating Docker&#8217;s storage path is a straightforward process that can significantly alleviate disk space constraints, allowing you to scale your projects.&nbsp;This&nbsp;ensures the continued smooth operation of your Dockerized services and prevents your Linux system from becoming inoperable due to a lack of disk space.<\/p>\n\n\n\n<p>We also want to mention that you can modify&nbsp;systemd&#8217;s&nbsp;Docker service to achieve the same result instead of changing the &#8220;<em>daemon.json<\/em>&#8221; file. However, we consider this approach a workaround rather than a proper solution, and we don&#8217;t recommend it, so we don&#8217;t cover it in our guide.<\/p>\n\n\n\n<p>Refer to the <a href=\"https:\/\/docs.docker.com\/config\/daemon\/\" target=\"_blank\" rel=\"noreferrer noopener\">official documentation<\/a> for more detailed information on configuring Docker using the &#8220;<em>daemon.json<\/em>&#8221;&nbsp;file.<\/p>\n\n\n\n<p>We&#8217;re grateful for your use of this&nbsp;guide and hope it has made the process&nbsp;clear and manageable. We value&nbsp;your feedback and comments, so please feel free to share them in the section below. Happy deploying!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>No more room for your deployments? Here&#8217;s a quick guide to relocating Docker&#8217;s storage path for images, volumes, containers, etc.<\/p>\n","protected":false},"author":10,"featured_media":118505,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,4417],"tags":[1439],"class_list":["post-118502","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","category-linux-knowledge","tag-docker"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/linuxiac.com\/wp-json\/wp\/v2\/posts\/118502","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxiac.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxiac.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxiac.com\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxiac.com\/wp-json\/wp\/v2\/comments?post=118502"}],"version-history":[{"count":0,"href":"https:\/\/linuxiac.com\/wp-json\/wp\/v2\/posts\/118502\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxiac.com\/wp-json\/wp\/v2\/media\/118505"}],"wp:attachment":[{"href":"https:\/\/linuxiac.com\/wp-json\/wp\/v2\/media?parent=118502"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxiac.com\/wp-json\/wp\/v2\/categories?post=118502"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxiac.com\/wp-json\/wp\/v2\/tags?post=118502"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}