{"id":23624,"date":"2022-10-03T14:05:19","date_gmt":"2022-10-03T14:05:19","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=23624"},"modified":"2025-07-12T05:40:19","modified_gmt":"2025-07-12T05:40:19","slug":"difference-between-docker-attach-and-docker-exec","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/difference-between-docker-attach-and-docker-exec\/","title":{"rendered":"Docker Tutorials: Difference between docker attach and docker exec"},"content":{"rendered":"\n<p><strong>Attach<\/strong><\/p>\n\n\n\n<p>The docker attach command allows you to attach to a running container using the container\u2019s ID or name, either to view its ongoing output or to control it interactively. You can attach to the same contained process multiple times simultaneously, screen sharing style, or quickly view the progress of your detached process.<\/p>\n\n\n\n<p>The command docker attach is for attaching to the existing process. So when you exit, you exit the existing process.<\/p>\n\n\n\n<p>If we use docker attach, we can use only one instance of shell. So if we want open new terminal with new instance of container&#8217;s shell, we just need run docker exec<\/p>\n\n\n\n<p>If the docker container was started using \/bin\/bash command, you can access it using attach, if not then you need to execute the command to create a bash instance inside the container using exec. Attach isn&#8217;t for running an extra thing in a container, it&#8217;s for attaching to the running process.<\/p>\n\n\n\n<p>To stop a container, use CTRL-c. This key sequence sends SIGKILL to the container. If &#8211;sig-proxy is true (the default),CTRL-c sends a SIGINT to the container. You can detach from a container and leave it running using the CTRL-p CTRL-q key sequence.<\/p>\n\n\n\n<p><strong>exec<\/strong><\/p>\n\n\n\n<p>&#8220;docker exec&#8221; is specifically for running new things in a already started container, be it a shell or some other process. The docker exec command runs a new command in a running container.<\/p>\n\n\n\n<p>The command started using docker exec only runs while the container\u2019s primary process (PID 1) is running, and it is not restarted if the container is restarted.<\/p>\n\n\n\n<p>exec command works only on already running container. If the container is currently stopped, you need to first run it. So now you can run any command in running container just knowing its ID (or name):<\/p>\n\n\n\n<p>docker exec &lt;container_id_or_name&gt; echo &#8220;Hello from container!&#8221;<\/p>\n\n\n\n<p>docker run -it -d shykes\/pybuilder \/bin\/bash<br>The most important here is the -d option, which stands for detached. It means that the command you initially provided to the container (\/bin\/bash) will be run in background and the container will not stop immediately.<\/p>\n\n\n<div class=\"epyt-gallery\" data-currpage=\"1\" id=\"epyt_gallery_87886\"><iframe loading=\"lazy\"  id=\"_ytid_11610\"  width=\"760\" height=\"427\"  data-origwidth=\"760\" data-origheight=\"427\" src=\"https:\/\/www.youtube.com\/embed\/?enablejsapi=1&#038;autoplay=0&#038;cc_load_policy=0&#038;cc_lang_pref=&#038;iv_load_policy=1&#038;loop=0&#038;rel=1&#038;fs=1&#038;playsinline=0&#038;autohide=2&#038;theme=dark&#038;color=red&#038;controls=1&#038;disablekb=0&#038;\" class=\"__youtube_prefs__  no-lazyload\" title=\"YouTube player\"  data-epytgalleryid=\"epyt_gallery_87886\"  allow=\"fullscreen; accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen data-no-lazy=\"1\" data-skipgform_ajax_framebjll=\"\"><\/iframe><div class=\"epyt-gallery-list\"><div>Sorry, there was a YouTube error.<\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>Attach The docker attach command allows you to attach to a running container using the container\u2019s ID or name, either to view its ongoing output or to control it interactively&#8230;. <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[4862],"tags":[],"class_list":["post-23624","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/23624","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=23624"}],"version-history":[{"count":3,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/23624\/revisions"}],"predecessor-version":[{"id":31414,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/23624\/revisions\/31414"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=23624"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=23624"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=23624"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}