{"id":362470,"date":"2021-11-04T14:45:39","date_gmt":"2021-11-04T11:45:39","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-run-a-command-on-a-running-docker-container-cloudsavvy-it\/"},"modified":"2021-11-04T14:45:39","modified_gmt":"2021-11-04T11:45:39","slug":"how-to-run-a-command-on-a-running-docker-container-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-run-a-command-on-a-running-docker-container-cloudsavvy-it\/","title":{"rendered":"#How to Run a Command on a Running Docker Container \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-6a3f8f469f997\" 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-6a3f8f469f997\" 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-run-a-command-on-a-running-docker-container-cloudsavvy-it\/#Running_Commands_In_Containers\" >Running Commands In Containers<\/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-run-a-command-on-a-running-docker-container-cloudsavvy-it\/#SSHing_Into_a_Container\" >SSHing Into a 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-run-a-command-on-a-running-docker-container-cloudsavvy-it\/#Copying_Files_To_And_From_Containers\" >Copying Files To And From Containers<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#How to Run a Command on a Running 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\" 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\" 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\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Usually, you want Docker <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>lications to be fully contained, but sometimes for development or automation purposes it\u2019s useful to be able to work with Docker containers as if there were Linux VMs. Docker provides tools for running commands and even entire shells inside containers.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Running_Commands_In_Containers\"><\/span>Running Commands In Containers<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>To run a command in a container, you\u2019ll needs its container ID, unless you\u2019ve set up a specific name for that container. This is a long hex string which you can find from the Docker process listing:<\/p>\n<pre>docker ps<\/pre>\n<p>Then, you can use the <code>exec -it<\/code>\u00a0command to run inside the container. For example, viewing the output of a log file:<\/p>\n<pre>docker exec -it containerID tail \/var\/log\/nginx\/access.log<\/pre>\n<p>You can also run scripts inside containers:<\/p>\n<pre>docker exec -it containerID script.sh<\/pre>\n<p>The <code>-it<\/code>\u00a0flags are for \u201cinteractive mode\u201d and TTY respectively, and are used almost all the time. There are a few other flags you can use:<\/p>\n<ul>\n<li><code>--workdir<\/code>\u00a0or <code>-w<\/code>\u00a0changes the current directory before the command.<\/li>\n<li><code>--detach<\/code>, or <code>-d<\/code>, runs the command in the background.<\/li>\n<li><code>--env<\/code>, or <code>-e<\/code>, sets environment variables before running.<\/li>\n<li><code>--env-file<\/code>\u00a0does the same, but is more secure for handling secrets.<\/li>\n<li><code>--privileged<\/code>\u00a0runs the command with extended permissions.<\/li>\n<li><code>--user<\/code>\u00a0runs as a different user<\/li>\n<\/ul>\n<p>Of course, this only works on a running container. If you wanted to pause a container to do maintenance, you\u2019ll either need to deploy updates through a new image version, or do changes to volume mounted data from the host OS.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"SSHing_Into_a_Container\"><\/span>SSHing Into a Container<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>You aren\u2019t limited to simple commands, you can actually open a shell by running <code>\/bin\/bash<\/code>\u00a0as the command.\u00a0You might be limited in the tools available\u2014most containers feature a fairly barebones Linux install\u2014but it makes running many commands a lot easier.<\/p>\n<pre>docker exec -it containerID \/bin\/bash<\/pre>\n<p>This is simply a shell acccessible from the host, which works well in most case. But, if you want, you can set up your containers to be completely available over SSH like a VPS. You can read more about setting that up in our guide to running an SSH service in a Docker container.<\/p>\n<p><strong>RELATED:<\/strong> <strong><em>How to SSH Into a Docker Container<\/em><\/strong><\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Copying_Files_To_And_From_Containers\"><\/span>Copying Files To And From Containers<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Running commands with <code>exec -it<\/code>\u00a0works, but there\u2019s still a layer between the host and container that prevents easy scripting. For one, while it\u2019s easy to send commands to a container, it\u2019s harder to get output out from the filesystem.<\/p>\n<p>While you can pipe the STDOUT of <code>exec -it<\/code>\u00a0to other services on the host OS, you can also copy files to and from the container\u2019s filesystem. For example, pulling a log file out and pasting it on the host:<\/p>\n<pre>docker cp container:\/var\/log\/nginx\/example.log example.log<\/pre>\n<p>Or pulling out entire directories:<\/p>\n<pre>docker cp nginx:\/etc\/nginx\/ nginxconfig\/<\/pre>\n<p>If you\u2019re regularly doing this though, you may want to consider using a bind or volume mount to make the data directly accessible from the host.<\/p>\n<p><strong>RELATED:<\/strong> <strong><em>How to Use Docker Cp to Copy Files Between Host and Containers<\/em><\/strong>\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\/14541\/how-to-run-a-command-on-a-running-docker-container\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#How to Run a Command on a Running Docker Container \u2013 CloudSavvy IT&#8221; Usually, you want Docker applications to be fully contained, but sometimes for development or automation purposes it\u2019s useful to be able to work with Docker containers as if there were Linux VMs. Docker provides tools for running commands and even entire shells&#8230;<\/p>\n","protected":false},"author":1,"featured_media":362471,"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-362470","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\/362470","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=362470"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/362470\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/362471"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=362470"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=362470"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=362470"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}