{"id":342228,"date":"2021-09-21T14:45:52","date_gmt":"2021-09-21T11:45:52","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-detach-from-a-docker-container-without-stopping-it-cloudsavvy-it\/"},"modified":"2021-09-21T14:45:52","modified_gmt":"2021-09-21T11:45:52","slug":"how-to-detach-from-a-docker-container-without-stopping-it-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-detach-from-a-docker-container-without-stopping-it-cloudsavvy-it\/","title":{"rendered":"#How to Detach From a Docker Container Without Stopping It \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 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-6a412bb2b0e86\" 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-6a412bb2b0e86\" 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-detach-from-a-docker-container-without-stopping-it-cloudsavvy-it\/#Detaching_Without_Stopping\" >Detaching Without Stopping<\/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-detach-from-a-docker-container-without-stopping-it-cloudsavvy-it\/#Changing_the_Detach_Keyboard_Sequence\" >Changing the Detach Keyboard Sequence<\/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-detach-from-a-docker-container-without-stopping-it-cloudsavvy-it\/#Changing_the_Sequence_on_a_Per-Container_Basis\" >Changing the Sequence on a Per-Container Basis<\/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-detach-from-a-docker-container-without-stopping-it-cloudsavvy-it\/#Detaching_When_The_Keyboard_Sequence_Wont_Work\" >Detaching When The Keyboard Sequence Won\u2019t Work<\/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-detach-from-a-docker-container-without-stopping-it-cloudsavvy-it\/#Reattaching_to_Your_Container\" >Reattaching to Your Container<\/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-detach-from-a-docker-container-without-stopping-it-cloudsavvy-it\/#Summary\" >Summary<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#How to Detach From a Docker Container Without Stopping It \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-10864\" srcset=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/04\/075c8694.jpeg?width=398&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 400w, https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/04\/075c8694.jpeg?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 1200w\" sizes=\"auto, 400w, 1200w\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/04\/075c8694.jpeg?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Illustration showing the Docker logo\" width=\"1600\" height=\"900\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Docker containers have an interactive mode that lets you attach your terminal\u2019s input and output streams to the container\u2019s process. Pressing Ctrl-C will usually terminate that<br \/>process, causing the container to stop. Here\u2019s to detach from a session without stopping the container.<\/p>\n<h2 id=\"detaching-without-stopping\"><span class=\"ez-toc-section\" id=\"Detaching_Without_Stopping\"><\/span>Detaching Without Stopping<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Docker supports a keyboard combination to gracefully detach from a container. Press Ctrl-P, followed by Ctrl-Q, to detach from your connection.<\/p>\n<p>You\u2019ll be dropped back into your shell but the previously attached process will remain alive, keeping your container running. You can check this by using <code>docker ps<\/code> to get a list of running containers.<\/p>\n<p>Pressing Ctrl-C or running the <code>exit<\/code> command will usually kill the container\u2019s foreground process unless it\u2019s been specially configured. A Docker container needs to have a running foreground process; a container without one will enter the stopped state.<\/p>\n<h2 id=\"changing-the-detach-keyboard-sequence\"><span class=\"ez-toc-section\" id=\"Changing_the_Detach_Keyboard_Sequence\"><\/span>Changing the Detach Keyboard Sequence<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>You can change the detach sequence to match your preference or avoid a conflict with keyboard shortcuts honored by your <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>lication. Add a <code>detachKeys<\/code> property to your <code>~\/.docker\/config.json<\/code> file to specify the keys you want to use.<\/p>\n<p>Docker supports the <code>a-z<\/code> characters and <code>@<\/code>, <code>^<\/code>, and <code>_<\/code>, symbols, as well as the left bracket sign (<code>[<\/code>) and two back slashes (<code>\\<\/code>). These are all used in conjunction with the <code>Ctrl-<\/code> key; letters may also be used individually, without <code>Ctrl<\/code>.<\/p>\n<p>Key sequences are expressed as a comma-separated list:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"json\">\n<pre class=\"de1\">{&#13;\n    \"detachKeys\": \"Ctrl-d,d\"&#13;\n}<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>This example would detach from the container when you press <code>Ctrl-D<\/code> im<a href=\"https:\/\/buradabiliyorum.com\/en\/category\/social-mediaa\/\" data-internallinksmanager029f6b8e52c=\"1\" title=\"Social Media\" target=\"_blank\" rel=\"noopener\">media<\/a>tely followed by the <code>d<\/code> key.<\/p>\n<h2 id=\"changing-the-sequence-on-a-per-container-basis\"><span class=\"ez-toc-section\" id=\"Changing_the_Sequence_on_a_Per-Container_Basis\"><\/span>Changing the Sequence on a Per-Container Basis<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Beyond changing your global configuration, Docker accepts <code>detachKeys<\/code> overrides on a per-container and per-attachment basis. Add the <code>--detach-keys<\/code> flag to commands which can attach to container processes to set a specific sequence.<\/p>\n<p>The commands which support this are:<\/p>\n<ul>\n<li><code>docker run<\/code><\/li>\n<li><code>docker start<\/code><\/li>\n<li><code>docker exec<\/code><\/li>\n<li><code>docker attach<\/code><\/li>\n<\/ul>\n<p>Here\u2019s how to attach to a container and then use <code>Ctrl-d<\/code>, followed by an underscore, to detach:<\/p>\n<pre>docker attach my-container --detach-keys=\"Ctrl-d,_\"<\/pre>\n<p>The <code>--detach-keys<\/code> flag uses the same key sequence format as the <code>detachKeys<\/code> config option. The flag overrides your <code>docker.json<\/code> setting; this in turn overrides Docker\u2019s default Ctrl-P\/Ctrl-Q sequence.<\/p>\n<h2 id=\"detaching-when-the-keyboard-sequence-wont-work\"><span class=\"ez-toc-section\" id=\"Detaching_When_The_Keyboard_Sequence_Wont_Work\"><\/span>Detaching When The Keyboard Sequence Won\u2019t Work<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Sometimes you might encounter a container process that refuses to detach, even when you issue the keyboard sequence. This can happen if the container\u2019s input stream isn\u2019t connected to your terminal (<code>-i<\/code> flag) or it has no pseudo-TTY allocated (<code>-t<\/code> flag). You could also come across this issue if your container\u2019s process handles the detach key sequence and you didn\u2019t override it when you attached.<\/p>\n<p>It\u2019s still possible to detach your terminal from the container under these circumstances. You need to temporarily open another shell window and use it to kill the <code>docker.attach<\/code> process that\u2019s keeping the attachment active.<\/p>\n<p>First find the process ID of the attachment process:<\/p>\n<pre>ps -ef | grep attach<\/pre>\n<p>Use the output from <code>ps<\/code> to identify the <code>docker.attach<\/code> process you need to kill. The command in the <code>CMD<\/code> column should identify the attachment you\u2019re looking for. Note down the relevant PID number and use the <code>kill<\/code> command to kill this process:<\/p>\n<pre>kill -9 &lt;PID&gt;<\/pre>\n<p>You should see your original shell detach from your Docker container and revert to a normal operating state. You can now close the second shell and continue using the original one.<\/p>\n<p>This technique works by killing the Docker CLI process that attached the terminal to the container, not the process within the container that keeps it running. Your original terminal becomes usable again and the container remains up.<\/p>\n<h2 id=\"reattaching-to-your-container\"><span class=\"ez-toc-section\" id=\"Reattaching_to_Your_Container\"><\/span>Reattaching to Your Container<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>You can reattach to containers using the <code>docker attach<\/code> command. This automatically attaches your terminal\u2019s input, output, <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.howtogeek.com\/435903\/what-are-stdin-stdout-and-stderr-on-linux\">and error streams<\/a> to the specified container:<\/p>\n<pre>docker attach my-container<\/pre>\n<p>All three streams are connected by default. You can omit the input stream by passing the <code>--no-stdin<\/code> flag. The container\u2019s output will be streamed into your terminal but you won\u2019t be able to supply any input.<\/p>\n<p>Use the keyboard sequence again to detach, or <code>Ctrl-C<\/code> to stop the process and container. If you use <code>Ctrl-C<\/code> or <code>exit<\/code>, <code>docker attach<\/code> will set the <code>$?<\/code> variable in your shell correctly so you can inspect the container\u2019s exit code.<\/p>\n<h2 id=\"summary\"><span class=\"ez-toc-section\" id=\"Summary\"><\/span>Summary<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The correct way to detach from a Docker container is a fairly obscure keyboard sequence that drops you back into your shell. You can customize this sequence to increase memorability and avoid any conflicts with your container\u2019s keyboard handling.<\/p>\n<p>Keyboard detach sequences can be ineffective in some circumstances. It\u2019s still possible to detach from your container by identifying and killing the process that\u2019s supporting the attachment. Regular Unix commands such as <code>ps<\/code> and <code>kill<\/code> should be used in this scenario.<\/p>\n<p>Finally, if you want your container to be permanently detached, start it with the <code>-d<\/code> flag (<code>docker run -d my-image:latest<\/code>). This will send the container straight into the background and emit no output to your shell. Detached containers are always visible using the <code>docker ps<\/code> command and can be stopped with <code>docker stop my-container<\/code>.\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\/14226\/how-to-detach-from-a-docker-container-without-stopping-it\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#How to Detach From a Docker Container Without Stopping It \u2013 CloudSavvy IT&#8221; Docker containers have an interactive mode that lets you attach your terminal\u2019s input and output streams to the container\u2019s process. Pressing Ctrl-C will usually terminate thatprocess, causing the container to stop. Here\u2019s to detach from a session without stopping the container. Detaching&#8230;<\/p>\n","protected":false},"author":1,"featured_media":342229,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/04\/075c8694.jpeg","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-342228","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\/342228","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=342228"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/342228\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/342229"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=342228"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=342228"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=342228"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}