{"id":380603,"date":"2021-12-13T19:07:49","date_gmt":"2021-12-13T16:07:49","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-add-a-volume-to-an-existing-docker-container-cloudsavvy-it\/"},"modified":"2021-12-13T19:07:49","modified_gmt":"2021-12-13T16:07:49","slug":"how-to-add-a-volume-to-an-existing-docker-container-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-add-a-volume-to-an-existing-docker-container-cloudsavvy-it\/","title":{"rendered":"#How to Add a Volume to an Existing Docker Container \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a2c27b236421\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a2c27b236421\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-add-a-volume-to-an-existing-docker-container-cloudsavvy-it\/#Adding_a_Volume_To_a_Running_Docker_Container\" >Adding a Volume To a Running Docker Container<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-add-a-volume-to-an-existing-docker-container-cloudsavvy-it\/#Cloning_From_An_Existing_Container\" >Cloning From An Existing Container<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-add-a-volume-to-an-existing-docker-container-cloudsavvy-it\/#The_Hacky_Solution\" >The Hacky Solution<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#How to Add a Volume to an Existing Docker Container \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage alignnone size-full wp-image-14169\" data-pagespeed-lazy-srcset=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/09\/993634a1.png?width=398&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 400w, https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/09\/993634a1.png?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 1200w\" sizes=\"auto, 400w, 1200w\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/09\/993634a1.png?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"1200\" height=\"675\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>While Docker is a useful tool for packaging and managing applications, it also presents many unique challenges, such as dealing with stored data. Usually, you add volumes to containers in the creation <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">script<\/a>, but what if you need to make new ones?<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Adding_a_Volume_To_a_Running_Docker_Container\"><\/span>Adding a Volume To a Running Docker Container<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Unfortunately it\u2019s not as simple as just adding a new volume. Containers must have their volumes configured on startup, which means to add a new volume, you must restart the container. While there is a hacky solution (more on that below), it\u2019s highly recommended that a container restart should be done anyway.<\/p>\n<p>This is for a few reasons. Restarting the container is fairly easy, and\u00a0most code updates require a restart of the service anyway. Having updates tracked in Git is another big factor, especially if you\u2019re using Docker Compose, and editing the launch script is far better than manually adding the volume to a running container.<\/p>\n<p>If your service is big enough that you are concerned about a few minutes (at most) of potential scheduled downtime to restart the container, you should probably be running a scaling deployment with multiple instances that can be updated independently. Modern auto-scaling systems should be designed to handle that, as code deployments happen often.<\/p>\n<p>If you want to add a volume, you\u2019ll need to stop the running container:<\/p>\n<pre> docker stop my_container<\/pre>\n<p>Create a new volume if you need to:<\/p>\n<pre>docker volume create nginx-config<\/pre>\n<p>And then run it with an updated launch command, adding the <code>--mount<\/code>\u00a0flag to configure the source volume and target destination.<\/p>\n<pre>docker run -d &#13;\n  --name devtest &#13;\n  --mount source=nginx-config,target=\/etc\/nginx &#13;\n  nginx:latest<\/pre>\n<p>If you\u2019re using Docker Compose, you can automate and track this process more easily, since the volume configuration is handled through a settings file. you\u2019ll need to add the volume to the <code>docker-compose.yml<\/code>\u00a0file:<\/p>\n<pre>version:\u00a0\"3.0\"&#13;\nservices:&#13;\n  web:&#13;\n    image: nginx:latest&#13;\n    ports:&#13;\n      -\u00a0\"80:80\"&#13;\n    volumes:&#13;\n      - \/docker\/nginx-config\/:\/etc\/nginx\/<\/pre>\n<p>Then, you can restart Docker Compose\u2019s services. Compose has a \u201crestart\u201d command, but it\u2019s actually to just refresh the running service without any config changes. If you want to update the images, you\u2019ll need to run <code>docker-compose up<\/code>\u00a0with the <code>--build<\/code>\u00a0flag:<\/p>\n<pre>docker-compose up -d --build<\/pre>\n<p>You can also manually run <code>docker-compose down<\/code>\u00a0beforehand to stop the services, but it isn\u2019t necessary in most cases unless you want to run it with the <code>-v<\/code>\u00a0flag to destroy existing volumes.<\/p>\n<p><strong>RELATED:<\/strong> <strong><em>What Is Docker Compose, and How Do You Use It?<\/em><\/strong><\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Cloning_From_An_Existing_Container\"><\/span>Cloning From An Existing Container<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>In almost all cases, you shouldn\u2019t be depending on a container\u2019s current running state. Anything that you care about, such as application data, should be stored in a volume so that it can be\u00a0persisted across container restarts and rebuilds.<\/p>\n<p>This method probably isn\u2019t a good idea for most people, as it requires you to make a new image every time you want to do this, and it still requires downtime. But, if you do need to add a volume to a running container, you can use <code>docker commit<\/code>\u00a0to make a new image based on that container, and then clone it with the new volume.<\/p>\n<p>Grab the container ID from <code>docker ps<\/code>:<\/p>\n<pre>docker ps<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-14974\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/11\/3b46739c.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"595\" height=\"127\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>And then, clone it with <code>commit<\/code>:<\/p>\n<pre>docker commit f88f33c918d2 imagename<\/pre>\n<p>Then, you can run the new image, replacing the old image with the cloned one.<\/p>\n<pre>docker run -d &#13;\n  --name devtest &#13;\n  --mount source=nginx-config,target=\/etc\/nginx &#13;\n  imagename<\/pre>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"The_Hacky_Solution\"><\/span>The Hacky Solution<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Docker volumes are really just trickery that the Docker runtime uses to expose host directories to containers, and it all depends on configuration. Because of this, you can actually modify that configuration directly, and restart the entire Docker daemon for your system to apply those changes without restarting at all.<\/p>\n<p>Of course, this is far more complicated than just dealing with a container restart, so if you can manage a minute of downtime, we highly recommend doing that instead.<\/p>\n<p>You\u2019ll need to navigate to Docker\u2019s storage directory:<\/p>\n<pre>cd \/var\/lib\/docker\/containers<\/pre>\n<p>In here will be many folders cooresponding to Docker container IDs, which you can find with <code>docker ps<\/code>. Open up the one for the container you want to modify.<\/p>\n<p>The config file is <code>config.v2.json<\/code>, but it\u2019s in a compact format and hard to edit. You can install <code>jq<\/code>\u00a0to pretty-print JSON on the command line, and pipe this to a new file for editing:<\/p>\n<pre>jq . config.v2.json &gt; tmp.json<\/pre>\n<p>You\u2019ll need to scroll down to find \u201cMountPoints,\u201d which contains the configuration for all the volumes and bind mounts. You can add a new one here, which should be in the following format:<\/p>\n<pre>\u00a0 \"MountPoints\": {&#13;\n    \"\/home\/container\": {&#13;\n      \"Source\": \"\/var\/lib\/pterodactyl\/volumes\/c7fb3a04-e540-48a7-9704-13987f52e933\",&#13;\n      \"Destination\": \"\/home\/container\",&#13;\n      \"RW\": true,&#13;\n      \"Name\": \"\",&#13;\n      \"Driver\": \"\",&#13;\n      \"Type\": \"bind\",&#13;\n      \"Propagation\": \"rprivate\",&#13;\n      \"Spec\": {&#13;\n        \"Type\": \"bind\",&#13;\n        \"Source\": \"\/var\/lib\/pterodactyl\/volumes\/c7fb3a04-e540-48a7-9704-13987f52e933\",&#13;\n        \"Target\": \"\/home\/container\"&#13;\n      },&#13;\n      \"SkipMountpointCreation\": true&#13;\n    }&#13;\n  },<\/pre>\n<p>Then, once it\u2019s done, you can minify the JSON again back into the config file:<\/p>\n<pre>jq -c . tmp.json &gt; config.v2.json<\/pre>\n<p><code>jq<\/code>\u00a0is a powerful utility, so if you wanted to automate this process entirely, you could do that.<\/p>\n<p><strong>RELATED:<\/strong> <strong><em>How to Work With JSON On the Command Line<\/em><\/strong><\/p>\n<p>Then, simply restart the Docker service to apply the changes:<\/p>\n<pre>sudo service docker restart<\/pre>\n<\/div>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/14973\/how-to-add-a-volume-to-an-existing-docker-container\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#How to Add a Volume to an Existing Docker Container \u2013 CloudSavvy IT&#8221; While Docker is a useful tool for packaging and managing applications, it also presents many unique challenges, such as dealing with stored data. Usually, you add volumes to containers in the creation script, but what if you need to make new ones?&#8230;<\/p>\n","protected":false},"author":1,"featured_media":380604,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/09\/993634a1.png","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-380603","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/380603","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=380603"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/380603\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/380604"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=380603"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=380603"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=380603"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}