{"id":761,"date":"2019-01-14T11:40:08","date_gmt":"2019-01-14T11:40:08","guid":{"rendered":"http:\/\/shijuvarghese.com\/?p=761"},"modified":"2024-07-17T09:39:53","modified_gmt":"2024-07-17T09:39:53","slug":"docker-running-docker-commands","status":"publish","type":"post","link":"http:\/\/shijuvarghese.com\/?p=761","title":{"rendered":"Docker: Running Docker commands"},"content":{"rendered":"<p>This tutorial is a continuation of a previous post\u00a0<a href=\"http:\/\/shijuvarghese.com\/?p=722\">Docker: Installation and basic<\/a>s<\/p>\n<p>The command to run a container from an image is:<\/p>\n<p><strong>[root@gw20-lap-doc1 ~]# docker run &lt;image name&gt;<\/strong><\/p>\n<p>The command <strong>docker run image<\/strong>\u00a0is same as these two commands executed one after the other.<\/p>\n<ul>\n<li><strong>docker create &lt;image name&gt;<\/strong><\/li>\n<li><strong>docker start &lt;container ID&gt;<\/strong><\/li>\n<\/ul>\n<p>Now let us create a new container from the image\u00a0<em>centos<\/em> which we downloaded in the previous post<\/p>\n<p><strong>[root@gw20-lap-doc1 ~]#\u00a0docker create centos echo &#8220;I am good&#8221;<\/strong><\/p>\n<p>This should return a <strong>&lt;container ID&gt;<\/strong><\/p>\n<p>Now let us see the status of all containers in the host<\/p>\n<p><strong>[root@gw20-lap-doc1 ~]# docker ps -all<\/strong><\/p>\n<p>You will notice that the container is listed, and the\u00a0<strong>STATUS<\/strong> will be\u00a0<strong>Created<\/strong>.<\/p>\n<p>To start the container issue the following command:<\/p>\n<p><strong>[root@gw20-lap-doc1 ~]# docker start\u00a0&lt;Container ID&gt;<\/strong><\/p>\n<p>By this container\u00a0<strong>started<\/strong>, ran the command\u00a0<strong>echo &#8220;I am good&#8221; <\/strong>and exited. However you would not have seen the output of the\u00a0<strong>echo<\/strong> command on the screen.<\/p>\n<p>Ensure the container existed. Check the status by running the following command.<\/p>\n<p><strong>[root@gw20-lap-doc1 ~]# docker ps -all<\/strong><\/p>\n<p>Now let us get the actual output of the command run by the container by running the following command:<\/p>\n<p><strong>[root@gw20-lap-doc1 ~]# docker start <\/strong>-a<strong> e32e4a84ee7d<\/strong><\/p>\n<p>The switch<span style=\"color: #ff6600;\">\u00a0<strong>-a<\/strong> <\/span>did the trick, which showed the output on the screen.<\/p>\n<p>In case we want to see all outputs created when a container ran after the docker existed, we can run the\u00a0<strong>logs<\/strong> command:<\/p>\n<p><strong>[root@gw20-lap-doc1 ~]# docker logs<\/strong><strong>\u00a0&lt;container ID&gt;<\/strong><\/p>\n<p>Though the containers are stopped, they may still reside in the server taking space. The was to get rid of them is:<\/p>\n<p><strong>[root@gw20-lap-doc1 ~]# docker system prune<\/strong><\/p>\n<p>If you note, when running the above command will remove\u00a0<strong>all build cache<\/strong>. This are the images downloaded from Docker Hub.<\/p>\n<p>If we have a\u00a0<em>running\u00a0<\/em>container and we want to <em>stop<\/em> it, we can run the following command:<\/p>\n<p><strong>[root@gw20-lap-doc1 ~]# docker stop\u00a0&lt;container ID&gt;<\/strong><\/p>\n<p>The\u00a0<strong>stop<\/strong> command send a signal to the container to do a graceful shutdown. However there is another command called\u00a0<strong>kill<\/strong> that send an immediate command to shutdown the container, without any proper housekeeping.<\/p>\n<p>Let us try to execute a command in a running container. For this let us run a centos container that will keep pinging google in the background.<\/p>\n<p><strong>[root@gw20-lap-doc1 ~]# docker create centos ping google.com<\/strong><\/p>\n<p><strong>[root@gw20-lap-doc1 ~]# docker start &lt;container ID&gt;<\/strong><\/p>\n<p><strong>[root@gw20-lap-doc1 ~]# docker ps -all<\/strong><\/p>\n<p>Ensure the container is\u00a0<strong>Up<\/strong> in the\u00a0<strong>STATUS<\/strong>.<\/p>\n<p>Now let us\u00a0<em>execute\u00a0<\/em>another command in the container by running the\u00a0<strong>exec<\/strong> command<\/p>\n<p><strong>[root@gw20-lap-doc1 ~]# docker exec -it c5c3403885d2 \/bin\/bash<\/strong><\/p>\n<p>This will open a bash application and the\u00a0<strong>-it<\/strong> switch (or <strong>-i -t<\/strong>) will show the output on the docker&#8217;s terminal window. The\u00a0<strong>-i<\/strong> enables us to connect to the\u00a0<em>Standard\u00a0<\/em><em>input<\/em> channel of the container, and the\u00a0<strong>-t<\/strong> switch formats the input and output view.<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Running commands in the background:<\/strong><\/span><\/p>\n<p>At times we may want to start a container by running the commands in the background so that we can get the terminal to do other operations.<\/p>\n<p>When we run the following command, the ping command will start and the output will be displayed in on the terminal window, and you will not be able to use that terminal to do any thing. Also you we will need to keep the terminal opened.<\/p>\n<p><strong>[root@gw20-lap-doc1 ~]# docker run centos ping google.com<\/strong><\/p>\n<p>However the following command will run the container, pinging google in the background.<\/p>\n<p><strong>[root@gw20-lap-doc1 ~]# docker\u00a0run <span style=\"color: #ff0000;\">-d<\/span> centos ping google.com<\/strong><\/p>\n<p>You may verify the same by running the <strong>exec\u00a0<\/strong>command, and taking a list of the running command.<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>This tutorial is a continuation of a previous post\u00a0Docker: Installation and basics The command to run a container from an image is: [root@gw20-lap-doc1 ~]# docker <a class=\"mh-excerpt-more\" href=\"http:\/\/shijuvarghese.com\/?p=761\" title=\"Docker: Running Docker commands\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":730,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21,25],"tags":[],"class_list":["post-761","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops","category-docker"],"_links":{"self":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/761","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=761"}],"version-history":[{"count":15,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/761\/revisions"}],"predecessor-version":[{"id":1620,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/761\/revisions\/1620"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/media\/730"}],"wp:attachment":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=761"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=761"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=761"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}