{"id":392654,"date":"2022-01-11T22:40:14","date_gmt":"2022-01-11T19:40:14","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-modify-the-configuration-of-running-docker-containers-cloudsavvy-it\/"},"modified":"2022-01-11T22:40:14","modified_gmt":"2022-01-11T19:40:14","slug":"how-to-modify-the-configuration-of-running-docker-containers-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-modify-the-configuration-of-running-docker-containers-cloudsavvy-it\/","title":{"rendered":"#How to Modify the Configuration of Running Docker Containers \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-6a2f5f761d9da\" 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-6a2f5f761d9da\" 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-modify-the-configuration-of-running-docker-containers-cloudsavvy-it\/#Renaming_a_Container\" >Renaming a 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-modify-the-configuration-of-running-docker-containers-cloudsavvy-it\/#Changing_the_Restart_Policy\" >Changing the Restart Policy<\/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-modify-the-configuration-of-running-docker-containers-cloudsavvy-it\/#Changing_Hardware_Resource_Limits\" >Changing Hardware Resource Limits<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-modify-the-configuration-of-running-docker-containers-cloudsavvy-it\/#When_Not_To_Use_These_Commands\" >When Not To Use These Commands?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-modify-the-configuration-of-running-docker-containers-cloudsavvy-it\/#What_About_Other_Properties_ImagePortsVolumes\" >What About Other Properties (Image\/Ports\/Volumes)?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-modify-the-configuration-of-running-docker-containers-cloudsavvy-it\/#Danger_Zone_Changing_Other_Container_Properties\" >Danger Zone: Changing Other Container Properties<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-modify-the-configuration-of-running-docker-containers-cloudsavvy-it\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#How to Modify the Configuration of Running Docker Containers \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage aligncenter 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>Docker containers are usually treated as immutable once they\u2019ve started running. You can update some configuration parameters dynamically though, such as the container\u2019s name and its hardware resource limits.<\/p>\n<p>In this guide, we\u2019ll show you how to use built-in Docker commands to modify selected parameters of running containers. We\u2019ll also look at what you shouldn\u2019t change and a workaround you can use if you believe you must.<\/p>\n<h2 id=\"renaming-a-container\"><span class=\"ez-toc-section\" id=\"Renaming_a_Container\"><\/span>Renaming a Container<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The simplest modification is renaming a created container. Names are assigned via the <code>--name<\/code> flag for <code>docker run<\/code>. When no name is supplied, the Docker daemon assigns a random one. You can use names to reference containers in Docker CLI commands; choosing an <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\">app<\/a>ropriate memorable one avoids running <code>docker ps<\/code> to find a container\u2019s auto-assigned name or ID.<\/p>\n<p>The <code>docker rename<\/code> command is used to change container names after creation. It takes two arguments, the target container\u2019s ID or current name, and the new name to assign:<\/p>\n<pre><code># docker rename &lt;target ID or name&gt; &lt;new name&gt;&#13;\ndocker rename old_name new_name<\/code><\/pre>\n<h2 id=\"changing-the-restart-policy\"><span class=\"ez-toc-section\" id=\"Changing_the_Restart_Policy\"><\/span>Changing the Restart Policy<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Restart policies determine whether containers should start automatically after your host reboots or the Docker daemon launches. The four <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/docs.docker.com\/config\/containers\/start-containers-automatically\">available policies<\/a> let you force the container to start, make it stay stopped, or conditionally start based on the container\u2019s previous exit code or running state.<\/p>\n<p>Docker supports changing restart policies on-the-fly. This is useful if you\u2019re planning to reboot your host or the Docker daemon and want a certain container to stay stopped \u2013 or automatically start \u2013 after the specific event.<\/p>\n<pre><code>docker update --restart unless-stopped demo_container<\/code><\/pre>\n<p>The above example changes the restart policy of <code>demo_container<\/code> to <code>unless-stopped<\/code>. This policy makes the container start with the daemon unless it was stopped manually before the daemon last exited.<\/p>\n<h2 id=\"changing-hardware-resource-limits\"><span class=\"ez-toc-section\" id=\"Changing_Hardware_Resource_Limits\"><\/span>Changing Hardware Resource Limits<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The <code>docker update<\/code> command can also be used to alter the resource limits applied to containers. You must pass one or more container IDs or names, along with a list of flags defining the limits to set on those containers.<\/p>\n<p>Flags are available for all the resource limits supported by <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/docs.docker.com\/engine\/reference\/commandline\/run\"><code>docker run<\/code><\/a>. Here\u2019s a condensed list of the options you can use:<\/p>\n<ul>\n<li><strong><code>--blkio-weight<\/code><\/strong> \u2013 Change the container\u2019s Block IO relative weight.<\/li>\n<li><strong><code>--cpus<\/code><\/strong> \u2013 Set the number of CPUs available to the container.<\/li>\n<li><strong><code>--cpu-shares<\/code><\/strong> \u2013 Set the CPU share relative weight.<\/li>\n<li><strong><code>--memory<\/code><\/strong> \u2013 Change the container\u2019s memory limit (e.g.\u00a0<code>1024M<\/code>).<\/li>\n<li><strong><code>--memory-swap<\/code><\/strong> \u2013 Configure the amount of memory the container can swap to disk; use a size such as <code>1024M<\/code> to set a specific limit, or <code>-1<\/code> for unlimited swap.<\/li>\n<li><strong><code>--kernel-memory<\/code><\/strong> \u2013 Change the container\u2019s kernel memory limit. Containers starved of kernel memory <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/docs.docker.com\/config\/containers\/resource_constraints\">can negatively impact<\/a> other workloads on the host machine.<\/li>\n<li><strong><code>--pids-limit<\/code><\/strong> \u2013 Configure the maximum number of process IDs allowed inside the container, restricting the number of processes that can be started.<\/li>\n<\/ul>\n<p>Here\u2019s an example of using <code>docker update<\/code> to change the memory limit and CPU count for two of your containers:<\/p>\n<pre><code>docker update --cpus 4 --memory 1024M first_container second_container<\/code><\/pre>\n<p>All of the available flags except <code>--kernel-memory<\/code> can be used with running Linux containers. To change the kernel memory limit, you must stop the container with <code>docker stop<\/code> first.<\/p>\n<p>Beware that none of these flags are currently supported for Windows-based containers. They will work with Linux containers running on a Windows host machine though.<\/p>\n<h2 id=\"when-not-to-use-these-commands\"><span class=\"ez-toc-section\" id=\"When_Not_To_Use_These_Commands\"><\/span>When Not To Use These Commands?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The <code>docker update<\/code> and <code>docker rename<\/code> commands should be used with containers you\u2019ve manually created via <code>docker run<\/code>. Be wary of using them with containers that originate from other tools such as <code>docker-compose<\/code>.<\/p>\n<p>Changing a container\u2019s name could leave it undetectable by the source tool, potentially breaking other components of your stack. In addition, if you\u2019re declaratively defining resource limits in a <code>docker-compose.yml<\/code> file, running the <code>docker-compose up<\/code> command again will re-apply those original limits to your container.<\/p>\n<p>Therefore you should stick to your existing container management solution if you\u2019re using one. For Compose, that means changing container names and resource limits in your <code>docker-compose.yml<\/code> file, then running <code>docker-compose up -d<\/code> to automatically apply the change. This ensures you won\u2019t unintentionally orphan containers or cause unintended side effects.<\/p>\n<h2 id=\"can-you-change-a-running-containers-image\"><span class=\"ez-toc-section\" id=\"What_About_Other_Properties_ImagePortsVolumes\"><\/span>What About Other Properties (Image\/Ports\/Volumes)?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Hardware limits, resource policies, and container names are the only config parameters the Docker CLI lets you change. You can\u2019t modify the image of a running container; neither can you easily alter other options such as port bindings and volumes.<\/p>\n<p>You should create another container if these values become outdated. Destroy your current instance and use <code>docker run<\/code> to start a replacement with your new image and corrected settings.<\/p>\n<p>As containers are meant to be stateless and ephemeral, you should be able to replace them at any time. Use volumes to store persistent container data; this mechanism lets you reattach stateful files to the new container by repeating the <code>-v<\/code> flags passed to the original <code>docker run<\/code> command:<\/p>\n<pre><code>docker run -v config-volume:\/usr\/lib\/config --name demo example-image:v1&#13;\n&#13;\ndocker rm demo&#13;\n&#13;\n# Existing data in \/usr\/lib\/config retained&#13;\ndocker run -v config-volume:\/usr\/lib\/config --name demo2 example-image:v2<\/code><\/pre>\n<h2 id=\"danger-zone-changing-other-container-properties\"><span class=\"ez-toc-section\" id=\"Danger_Zone_Changing_Other_Container_Properties\"><\/span>Danger Zone: Changing Other Container Properties<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Although you should aim to replace containers wherever possible, it is possible to modify the properties of existing ones by directly editing Docker\u2019s config files. Take care when using this method: it\u2019s entirely unsupported and a misplaced change could break your container.<\/p>\n<p>While this option provides a way to arbitarily edit existing containers, it won\u2019t work while they\u2019re running. Use the <code>docker stop my-container<\/code> command to stop the container you want to edit, then continue to make your changes.<\/p>\n<p>Container config files have the following path on your host:<\/p>\n<pre><code>\/var\/lib\/docker\/containers\/&lt;container id&gt;\/config.v2.json<\/code><\/pre>\n<p>You need to know the container\u2019s full ID, not the truncated version shown by <code>docker ps<\/code>. You can use the <code>docker inspect<\/code> command to get this:<\/p>\n<pre><code>docker inspect &lt;short id or name&gt; | jq | grep Id<\/code><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-15259\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2022\/01\/c484bc4a.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"681\" height=\"47\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Once you\u2019ve got to a container\u2019s <code>config.v2.json<\/code>, you can open it in a text editor to make any necessary changes. The JSON stores the container configuration created when you ran <code>docker run<\/code>. You can modify the contents to alter properties such as port bindings, environment variables, volumes, and the container entrypoint and command.<\/p>\n<p>To add a port binding, find the <code>PortBindings<\/code> key in the file, then insert a new item into the object:<\/p>\n<pre class=\"json\"><code>{&#13;\n    \"PortBindings\": {&#13;\n        \"80\/tcp\": {&#13;\n            \"HostIp\": \"\",&#13;\n            \"HostPort\": \"8080\"&#13;\n        }&#13;\n    }&#13;\n}<\/code><\/pre>\n<p>Here port 80 in the container is bound to port 8080 on the host. It\u2019s similarly straightforward to add environment variables \u2013 find the <code>Env<\/code> key, then insert new items into the array:<\/p>\n<pre class=\"json\"><code>{&#13;\n    \"Env\": [&#13;\n        \"FOO=bar\",&#13;\n        \"CUSTOM_VARIABLE=example\"&#13;\n    ]&#13;\n}<\/code><\/pre>\n<p>Once you\u2019re done editing, restart the Docker service and your container:<\/p>\n<pre><code>sudo service docker restart&#13;\n&#13;\ndocker start my-container<\/code><\/pre>\n<p>The container will now run with its updated configuration.<\/p>\n<h2 id=\"conclusion\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Docker containers are meant to be ephemeral units which you replace when their configuration becomes outdated. Despite this intention, there are scenarios where it\u2019s necessary to modify an existing container. Docker handles the most common use cases \u2013 name changes and real-time resource limit adjustments \u2013 via built-in CLI commands such as <code>docker update<\/code>.<\/p>\n<p>When you want to change another property, always try to replace the container as your first course of action. This minimizes the risk of breaking things and is in keeping with Docker\u2019s model of immutability. If you do end up in a situation where an existing container needs to be edited, you can manually alter Docker\u2019s internal config files as outlined above.<\/p>\n<p>Finally, remember that containers managed by other ecosystem tools like Docker Compose should be modified using those mechanisms. Otherwise you might find containers are unexpectedly orphaned or overwritten if the tool\u2019s unaware of the changes you\u2019ve made.\n<\/p><\/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\/15254\/how-to-modify-the-configuration-of-running-docker-containers\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#How to Modify the Configuration of Running Docker Containers \u2013 CloudSavvy IT&#8221; Docker containers are usually treated as immutable once they\u2019ve started running. You can update some configuration parameters dynamically though, such as the container\u2019s name and its hardware resource limits. In this guide, we\u2019ll show you how to use built-in Docker commands to modify&#8230;<\/p>\n","protected":false},"author":1,"featured_media":392655,"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-392654","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\/392654","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=392654"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/392654\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/392655"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=392654"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=392654"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=392654"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}