{"id":7697,"date":"2018-08-27T14:09:31","date_gmt":"2018-08-27T06:09:31","guid":{"rendered":"http:\/\/rmohan.com\/?p=7697"},"modified":"2018-08-27T14:09:31","modified_gmt":"2018-08-27T06:09:31","slug":"detailed-docker-container-common-operations","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=7697","title":{"rendered":"Detailed Docker container common operations"},"content":{"rendered":"<p><strong><span>First, start the container<\/span><\/strong><\/p>\n<p><span>There are two ways to start a container. One is to create a new container based on the image and start, and the other is to restart the container in the terminated state.\u00a0<\/span><br \/>\n<span>Because Docker&#8217;s containers are too lightweight, users often delete and recreate containers at any time.<\/span><\/p>\n<p><span>New and start<\/span><\/p>\n<p><span>For example, the following command outputs a &#8220;Hello World&#8221; and then terminates the container.<\/span><\/p>\n<p><span>$ docker run\u00a0<\/span><a title=\"Ubuntu\" href=\"https:\/\/www.linuxidc.com\/topicnews.aspx?tid=2\" target=\"_blank\" rel=\"noopener\"><span>Ubuntu<\/span><\/a><span>\u00a0:14.04 \/bin\/echo &#8216;Hello world&#8217;\u00a0<\/span><br \/>\n<span>Hello world<\/span><\/p>\n<p><span>This is almost indistinguishable from directly executing \/bin\/echo &#8216;hello world&#8217; locally.<\/span><\/p>\n<p><span>The following command launches a bash terminal that allows the user to interact.<\/span><\/p>\n<p><span>$ docker run -t -i ubuntu:14.04 \/bin\/bash\u00a0<\/span><br \/>\n<span>root@af8bae53bdd3:\/#<\/span><\/p>\n<p><span>The -t option causes Docker to assign a pseudo-tty and bind to the container&#8217;s standard input, and -i keeps the container&#8217;s standard input open.<\/span><\/p>\n<p><span>When using docker run to create containers, the standard operations that Docker runs in the background include:<\/span><\/p>\n<ul>\n<li><span>Check if the specified image exists locally. If it does not exist, download it from the public repository.<\/span><\/li>\n<li><span>Create and launch a container with an image<\/span><\/li>\n<li><span>Allocate a file system and mount a readable and writable layer outside the read-only mirror layer<\/span><\/li>\n<li><span>Bridge a virtual interface into the container from the bridge interface configured by the host host<\/span><\/li>\n<li><span>Configure an ip address from the address pool to the container<\/span><\/li>\n<li><span>Execute a user-specified application<\/span><\/li>\n<li><span>The container is terminated after execution<\/span><\/li>\n<\/ul>\n<p><span>Starting a terminated container\u00a0<\/span><br \/>\n<span>You can use the docker container start command to start a container that has been terminated.<\/span><\/p>\n<p><strong><span>Second, the guardian state runs<\/span><\/strong><\/p>\n<p><span>More often, you need to have Docker run in the background instead of directly outputting the results of the execution command under the current host.\u00a0This can be done by adding the -d parameter.<\/span><\/p>\n<p><span>$ docker run -d ubuntu \/bin\/sh -c &#8220;while true; do echo hello world; sleep 1; done&#8221;\u00a0<\/span><br \/>\n<span>cb30b87566d0550ec5f1232d148c5ffed6546c347889e58a6405579f2af73f2a<\/span><\/p>\n<p><span>A unique id is returned when started with the -d parameter.\u00a0The output can be viewed with docker logs [container ID or NAMES].\u00a0If you do not use the -d parameter.\u00a0The output result (STDOUT) will be printed on the host<\/span><\/p>\n<p><span>View container information with the docker container ls command.<\/span><\/p>\n<p><span>$ docker container ls\u00a0<\/span><br \/>\n<span>CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES\u00a0<\/span><br \/>\n<span>cb30b87566d0 ubuntu &#8220;\/bin\/sh -c &#8216;while t&#8230;&#8221; 2 minutes ago Up 2 minutes goofy_mcclintock<\/span><\/p>\n<p><span>To get the output of the container, you can use the docker container logs command.<\/span><\/p>\n<p><span>$ docker container logs goofy_mcclintock\u00a0<\/span><br \/>\n<span>hello world\u00a0<\/span><br \/>\n<span>hello world\u00a0<\/span><br \/>\n<span>hello world\u00a0<\/span><br \/>\n<span>&#8230;&#8230;<\/span><\/p>\n<p><span>Note: Whether the container will run for a long time is related to the command specified by docker run, regardless of the -d parameter.<\/span><\/p>\n<p><strong><span>Third, terminate the container<\/span><\/strong><\/p>\n<p><span>You can use the docker container stop to terminate a running container.\u00a0The format is:\u00a0<\/span><br \/>\n<span>docker container stop [options] CONTAINER [CONTAINER&#8230;]<\/span><\/p>\n<p><span>In addition, when the application specified in the Docker container is terminated, the container is also automatically terminated.\u00a0<\/span><br \/>\n<span>For example, only the container of one terminal is started. When the user exits the terminal through the exit command or Ctrl+d\u00a0<\/span><br \/>\n<span>, the created container is terminated immediately.<\/span><\/p>\n<p><span>The container for the terminated state can be seen with the docker container ls -a command.\u00a0E.g<\/span><\/p>\n<p><span>$ docker container stop goofy_mcclintock\u00a0<\/span><br \/>\n<span>goofy_mcclintock<\/span><\/p>\n<p><span>$ docker container ls -a\u00a0<\/span><br \/>\n<span>CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES\u00a0<\/span><br \/>\n<span>cb30b87566d0 ubuntu &#8220;\/bin\/sh -c &#8216;while t&#8230;&#8221; 20 minutes ago Exited (137) 23 seconds ago goofy_mcclintock<\/span><\/p>\n<p><span>The container in the terminated state is started by the docker container start command; the\u00a0<\/span><br \/>\n<span>container of a running state is terminated and restarted by the docker container restart command.<\/span><\/p>\n<p><strong><span>Fourth, enter the container<\/span><\/strong><\/p>\n<p><span>When the -d parameter is used, the container will enter the background after it starts.\u00a0<\/span><br \/>\n<span>Use the docker attach command or the docker exec command to enter the container. It is recommended to use the docker exec command for reasons explained below.<\/span><\/p>\n<p><span>The attach command\u00a0<\/span><br \/>\n<span>docker attach is a command that comes with Docker.\u00a0The following example shows how to use this command.<\/span><\/p>\n<p><span>$ docker run -dit ubuntu\u00a0<\/span><br \/>\n<span>e1ffd4f792fe0ce7f7e700147051e1f792e352f5b70929eb9376393ac20114b4<\/span><\/p>\n<p><span>$ docker container ls\u00a0<\/span><br \/>\n<span>CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES\u00a0<\/span><br \/>\n<span>e1ffd4f792fe ubuntu &#8220;\/bin\/bash&#8221; About a minute ago Up About a minute awesome_payne<\/span><\/p>\n<p><span>$ docker attach e1ff\u00a0<\/span><br \/>\n<span>root@e1ffd4f792fe:\/#<\/span><\/p>\n<p><span>Note: If exit from this stdin, it will cause the container to stop.<\/span><\/p>\n<p><span>The exec command\u00a0<\/span><br \/>\n<span>-i -t parameter\u00a0<\/span><br \/>\n<span>docker exec can be followed by multiple parameters, here mainly the -i -t parameter.\u00a0<\/span><br \/>\n<span>When only the -i parameter, since there is no allocation of pseudo-terminals, the interface is not familiar Linux command prompt, the command execution\u00a0<\/span><br \/>\n<span>line results can still be returned.\u00a0<\/span><br \/>\n<span>When the -i -t parameter is used together, you can see the Linux command prompt we are familiar with.<\/span><\/p>\n<p><span>$ docker run -dit ubuntu\u00a0<\/span><br \/>\n<span>16168d4b66b115b5afac5836db3ff93304774e98489f628ac625fff2bcd640ba<\/span><\/p>\n<p><span>$ docker container ls\u00a0<\/span><br \/>\n<span>CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES\u00a0<\/span><br \/>\n<span>16168d4b66b1 ubuntu &#8220;\/bin\/bash&#8221; 58 seconds ago Up 57 seconds happy_bardeen<\/span><\/p>\n<p><span>$ docker exec -it 16168 bash\u00a0<\/span><br \/>\n<span>root@16168d4b66b1:\/#<\/span><\/p>\n<p><span>Exit from this stdin will not cause the container to stop.\u00a0That&#8217;s why the docker exec is recommended.\u00a0<\/span><br \/>\n<span>For more parameter descriptions, please use docker exec &#8211;help to view.<\/span><\/p>\n<p><strong><span>Fifth, delete the container<\/span><\/strong><\/p>\n<p><span>Delete a container that is in a terminated state in the format:\u00a0<\/span><br \/>\n<span>docker container rm [options] CONTAINER [CONTAINER&#8230;]<\/span><\/p>\n<p><span>$ docker container rm awesome_payne\u00a0<\/span><br \/>\n<span>awesome_payne<\/span><\/p>\n<p><span>If you want to delete a running container, you can add the -f parameter.\u00a0Docker will send a SIGKILL signal to the container.<\/span><\/p>\n<p><span>Clean up all containers in the terminated state. Use the docker container ls -a command to view all the containers that have been created, including the termination status. If the number is too large, it may be cumbersome to delete them one by one. You can use the following command to clear all the termination status. Container.<\/span><\/p>\n<p><span>Prune Container Docker $\u00a0<\/span><br \/>\n<span>! This by Will the Remove All the WARNING stopped Containers.\u00a0<\/span><br \/>\n<span>Are you the Sure you want to the Continue [the y-\/ N] the y-?\u00a0<\/span><br \/>\n<span>Deleted Containers:\u00a0<\/span><br \/>\n<span>545f8f6d19286efae28307d06ed1acc034d07f109e907c01892471a6f89e772d\u00a0<\/span><br \/>\n<span>cb30b87566d0550ec5f1232d148c5ffed6546c347889e58a6405579f2af73f2a\u00a0<\/span><br \/>\n<span>&#8230;&#8230;<\/span><\/p>\n<p><strong><span>Export and import containers<\/span><\/strong><\/p>\n<p><span>Exporting a container\u00a0<\/span><br \/>\n<span>If you want to export a local container, you can use the docker export command.<\/span><\/p>\n<p><span>Container Docker LS -a $\u00a0<\/span><br \/>\n<span>CONTAINER ID PORTS the STATUS the IMAGE CREATED the COMMAND NAMES\u00a0<\/span><br \/>\n<span>16168d4b66b1 Ubuntu &#8220;\/ bin \/ the bash&#8221; 18 is happy_bardeen minutes minutes ago Member 18 is Up<\/span><\/p>\n<p><span>$ docker export 16168d4b66b1 &gt; ubuntu.tar<\/span><\/p>\n<p><span>This will export the container snapshot to a local file.<\/span><\/p>\n<p><span>Import container snapshots\u00a0<\/span><br \/>\n<span>can be imported as mirrors from the container snapshot file using docker import, for example<\/span><\/p>\n<p><span>$ cat ubuntu.tar | docker import &#8211; test\/ubuntu:v1.0\u00a0<\/span><br \/>\n<span>sha256:91b174fec9ed55d7ebc3d2556499713705f40713458e8594efa114f261d7369a<\/span><\/p>\n<p><span>$ docker image ls\u00a0<\/span><br \/>\n<span>REPOSITORY TAG IMAGE ID CREATED SIZE\u00a0<\/span><br \/>\n<span>test\/ubuntu v1.0 91b174fec9ed 10 seconds ago 69.8MB\u00a0<\/span><br \/>\n<span>ubuntu latest 735f80812f90 3 weeks ago 83.5MB<\/span><\/p>\n<p><span>Alternatively, you can import it by specifying a URL or a directory, such as\u00a0<\/span><br \/>\n<span>$ docker import http:\/\/example.com\/exampleimage.tgz example\/imagerepo<\/span><\/p>\n<p><span>Note: Users can either use the docker load to import the image storage file to the local image library, or use docker import to import a container snapshot to the local image library.\u00a0The difference between the two is that the container snapshot file will discard all history and metadata information (that is, only the snapshot state of the container at the time), and the image storage file will save the full record and be large.\u00a0In addition, metadata information such as tags can be reassigned when importing from a container snapshot file.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>First, start the container<\/p>\n<p>There are two ways to start a container. One is to create a new container based on the image and start, and the other is to restart the container in the terminated state. Because Docker&#8217;s containers are too lightweight, users often delete and recreate containers at any time.<\/p>\n<p>New and start<\/p>\n<p> [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[82],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7697"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7697"}],"version-history":[{"count":1,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7697\/revisions"}],"predecessor-version":[{"id":7698,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7697\/revisions\/7698"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7697"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7697"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7697"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}