{"id":536,"date":"2019-07-13T15:51:53","date_gmt":"2019-07-13T22:51:53","guid":{"rendered":"http:\/\/35.243.195.209\/?page_id=536"},"modified":"2021-04-19T19:25:30","modified_gmt":"2021-04-20T02:25:30","slug":"docker","status":"publish","type":"page","link":"https:\/\/nanzhou.cc\/index.php\/snippets\/docker\/","title":{"rendered":"Docker"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">1. Image<\/h3>\n\n\n\n<p>Show all images. <\/p>\n\n\n\n<pre class=\"wp-block-code bash\"><code lang=\"bash\" class=\"language-bash\">(base) fighternan@fighternan:~$ sudo docker images\nREPOSITORY                            TAG                         IMAGE ID               CREATED             SIZE fighternan\/ubuntu-images    pure-sudo-ubuntu    f279d4808a3d        About an hour ago   115MB <\/code><\/pre>\n\n\n\n<p>Delete an image. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">(base) fighternan@fighternan:~$ sudo docker image rm fighternan\/ubuntu-images:pure-sudo-ubuntu\nUntagged: fighternan\/ubuntu-images:pure-sudo-ubuntu\nUntagged: fighternan\/ubuntu-images@sha256:feeb413b7c4972111ceff1aedc99e6bc3c8a8cd33d975dc0280540b7a0482215\nDeleted: sha256:f279d4808a3d861a5e2516cd23a86952b023f5348cad3aa97d35affa173df6db\nDeleted: sha256:0d1f7f7d3080fb95c5c8df968f38d845b840febf1ccae02525fee8d5900937e2\n(base) fighternan@fighternan:~$ sudo docker images\nREPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE<\/code><\/pre>\n\n\n\n<p>Run a container from an image foreground without clean up (&#8211;rm).<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">(base) fighternan@fighternan:~$ sudo docker run -it --name my_ubuntu ubuntu\nroot@47d83458b7f0:\/#<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. Container<\/h3>\n\n\n\n<p>Show all containers. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">(base) fighternan@fighternan:~$ sudo docker container ls -a\nCONTAINER ID        IMAGE                                   COMMAND                  CREATED             STATUS                     PORTS               NAMES\n47d83458b7f0        ubuntu                                      \"\/bin\/bash\"              2 minutes ago       Exited (0) 2 seconds ago                       my_ubuntu<\/code><\/pre>\n\n\n\n<p>Delete a container.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">(base) fighternan@fighternan:~$ sudo docker container rm 152a75e1acb5<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. Commit &amp; Push<\/h3>\n\n\n\n<p>We want to set up a new environment in the container from the latest pure ubuntu.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">(base) fighternan@fighternan:~$ sudo docker pull ubuntu\nUsing default tag: latest\nlatest: Pulling from library\/ubuntu\nDigest: sha256:017eef0b616011647b269b5c65826e2e2ebddbe5d1f8c1e56b3599fb14fabec8\nStatus: Downloaded newer image for ubuntu:latest<\/code><\/pre>\n\n\n\n<p>Then we create a container and install some packages. Say we install the <code>sudo<\/code> command. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">(base) fighternan@fighternan:~$ sudo docker run -it --name my_ubuntu ubuntu\nroot@c9961f450ef1:\/# apt-get update &amp;&amp; \\\n>       apt-get -y install sudo<\/code><\/pre>\n\n\n\n<p>We exit the container and commit it to an image. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">(base) fighternan@fighternan:~$ sudo docker ps -a\nCONTAINER ID        IMAGE                                       COMMAND                  CREATED             STATUS                            PORTS               NAMES\nc9961f450ef1        ubuntu                                      \"\/bin\/bash\"              10 minutes ago      Exited (130) About a minute ago                       my_ubuntu\n\n(base) fighternan@fighternan:~$ sudo docker commit --author \"fighternan@gmail.com\" -m \"add sudo\" my_ubuntu fighternan\/ubuntu-images:pure-sudo-ubuntu\n\n(base) fighternan@fighternan:~$ sudo docker images\nREPOSITORY                          TAG                          IMAGE ID            CREATED             SIZE\nfighternan\/ubuntu-images    pure-sudo-ubuntu    f279d4808a3d        4 seconds ago       115MB<\/code><\/pre>\n\n\n\n<p>We finally push the image to our private docker repository so that we can use it anywhere. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">(base) fighternan@fighternan:~$ sudo docker push fighternan\/ubuntu-images:pure-sudo-ubuntu\nThe push refers to repository [docker.io\/fighternan\/ubuntu-images]\n7ebf7c42b39e: Pushed \nb57c79f4a9f3: Layer already exists \nd60e01b37e74: Layer already exists \ne45cfbc98a50: Layer already exists \n762d8e1a6054: Layer already exists <\/code><\/pre>\n\n\n\n<p>Now we can see it in our docker hub. <\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"http:\/\/35.243.195.209\/wp-content\/uploads\/2019\/03\/Screenshot-from-2019-03-24-11-22-01.png\" alt=\"\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">4. Copy Files<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-\ndocker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1. Image Show all images. Delete an image. Run a container from an image foreground without clean up (&#8211;rm). 2. Container Show all containers. Delete a container. 3. Commit &amp; Push We want to set up a new environment in the container from the latest pure ubuntu. Then we create a container and install some&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":452,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-536","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/pages\/536","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/comments?post=536"}],"version-history":[{"count":6,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/pages\/536\/revisions"}],"predecessor-version":[{"id":1590,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/pages\/536\/revisions\/1590"}],"up":[{"embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/pages\/452"}],"wp:attachment":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/media?parent=536"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}