From 7104152bf6b8bb60dc74fa9863f217f706ecfb84 Mon Sep 17 00:00:00 2001 From: Max Huang Date: Sun, 14 Aug 2016 16:44:58 +0800 Subject: [PATCH] Docker Cookbook Reading --- .../ch3.txt" | 23 + mindmap/docker20160814.mm | 1924 +++++++++++++++++ 2 files changed, 1947 insertions(+) create mode 100644 mindmap/docker20160814.mm diff --git "a/books/\350\256\200\346\233\270\345\277\203\345\276\227/ch3.txt" "b/books/\350\256\200\346\233\270\345\277\203\345\276\227/ch3.txt" index ad0dcc1..b1acbff 100644 --- "a/books/\350\256\200\346\233\270\345\277\203\345\276\227/ch3.txt" +++ "b/books/\350\256\200\346\233\270\345\277\203\345\276\227/ch3.txt" @@ -4,3 +4,26 @@ Container 會把 IP 寫入 /etc/hosts 可以透過 docker inspect CONTAINER_NAME 來看相關網路資訊 -- 可以用 --format '{{ .NetworkSettings.IPAddress }}' 來直接取出 -- 可以觀察 container 內的 /etc/hosts + +3.2 Exposing a Container Port on the Host + +# 列出某個 contaoner 的 port mapping, 但是覺得用 docker ps 檢查比較快 +docker port CONTAINER + +* 觀察 port mapping +---- 可以用 iptables -L 來觀察 docker 的 port mapping,省略一下原本有的 Chain, Docker 會建立 DOCKER Chain 來進行 port mapping + + # iptables -L + + Chain DOCKER (1 references) + + target     prot opt source               destination         + + ACCEPT     tcp  --  anywhere             172.17.0.3           tcp dpt:ssh + +---- docker 會建立 proxy 來執行 port maping, 可以用 ps -ef 來觀察 + + # ps -ef | grep docker + root      4129  1335  0 16:28 ?        00:00:00 docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 32768 -container-ip 172.17.0.3 -container-port 22 + + diff --git a/mindmap/docker20160814.mm b/mindmap/docker20160814.mm new file mode 100644 index 0000000..c93a963 --- /dev/null +++ b/mindmap/docker20160814.mm @@ -0,0 +1,1924 @@ + + + + + + + + + + +

+ https://www.docker.com/ +

+ +
+
+
+ + + + + + + + + + + + + + +

+ 可以用 iptables -L 來觀察 docker 的 port mapping +

+

+ 省略一下原本有的 Chain, Docker 會建立 DOCKER Chain 來進行 port mapping +

+

+ # iptables -L +

+

+ +

+

+ Chain OUTPUT (policy ACCEPT) +

+

+ target     prot opt source               destination         +

+

+ +

+

+ Chain DOCKER (1 references) +

+

+ target     prot opt source               destination         +

+

+ ACCEPT     tcp  --  anywhere             172.17.0.3           tcp dpt:ssh +

+

+ ACCEPT     tcp  --  anywhere             172.17.0.4           tcp dpt:commplex-main +

+

+ ACCEPT     udp  --  anywhere             172.17.0.4           udp dpt:domain +

+

+ ACCEPT     tcp  --  anywhere             172.17.0.5           tcp dpt:ssh +

+ +
+
+ + + + + + +

+ docker 會建立 proxy 來執行 port maping, 可以用 ps -ef 來觀察 +

+

+ # ps -ef | grep docker +

+

+ avahi      804     1  0  5月27 ?      00:00:12 avahi-daemon: running [dockerlab.local] +

+

+ root      1335     1  0  5月27 ?      00:34:03 /usr/bin/docker daemon -H fd:// +

+

+ root      4129  1335  0 16:28 ?        00:00:00 docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 32768 -container-ip 172.17.0.3 -container-port 22 +

+ + +
+
+
+
+ + + + + + + + + +

+ Pull an image or a repository from a registry +

+

+ Usage: docker pull [OPTIONS] NAME[:TAG|@DIGEST] +

+

+ +

+

+ Pull an image or a repository from a registry +

+

+ +

+

+   -a, --all-tags=false            Download all tagged images in the repository +

+ +
+
+ + + + + + + + + +

+ List images +

+

+ 列出 images +

+ +
+
+ + + + + + +

+ #列出 docker images +

+

+ docker  images +

+ +
+
+
+ + + + + + + +

+ Run a command in a new container +

+

+ Usage: docker  run  [OPTIONS]   IMAGE   [COMMAND]   [ARG...] +

+ +
+
+ + + + + + + + + + + + + + + +

+ 如果沒有加上 --rm +

+

+ 離開 container 之後, 用 docker  ps  -a 會發現其實還在主機上面 +

+ +
+
+
+
+ + + + + + + + + + +

+ #!/bin/bash +

+

+ for (( i=8000 ; i < 8015 ; i=i+1 )) +

+

+   do +

+

+     sudo docker run -d -p $i:22 -t sakana/sshd +

+

+   done +

+ +
+
+
+
+
+ + + + + + + +

+ flag is new and tells Docker to map any required network ports inside our container to our host. +

+

+ 自動指定Host port 來連接 +

+ +
+
+
+ + + + + + + +

+ -p, --publish=[ ]                Publish a container's port(s) to the host +

+

+ 手動指定host port 來連接 +

+ +
+
+
+ + + + + + + + + + + + + + + + + + + + +

+ attach    Attach to a running container +

+

+ 針對已經執行的container 再次進入到容器內 +

+ +
+
+ + + + + + + + + +

+ List containers +

+

+ 列出 containers +

+ +
+
+ + + + + + +

+ 列出目前執行 container +

+

+ #docker  ps +

+

+ +

+

+ 列出所有的 container +

+

+ #docker  ps  -a +

+

+ +

+

+ 列出所有的 container, 但是只顯示 ID +

+

+ #docker  ps  -a  -q +

+ +
+
+ + + + + + + + + + + + + + + + + + + + +

+ 組合技 +

+

+ 停止所有的container +

+

+ docker  stop  $( docker  ps  -aq  ) +

+ +
+
+
+ + + + + + + +

+ Return low-level information on a container or image +

+

+ Usage: docker inspect [OPTIONS] CONTAINER|IMAGE [CONTAINER|IMAGE...] +

+ +
+
+ + + + + + +

+ 查詢掛載的 volume 位置 +

+

+ # docker  inspect  -f  {{.Mounts}}   CONTAINERID +

+ +
+
+ + + + + + +

+ 檢查網路 IP +

+

+ # docker   inspect   --format   '{{ .NetworkSettings.IPAddress }}'  CONATINERID +

+ + +
+
+
+ + + + + + + + +

+ 組合技 +

+

+ 移除所有的container, 但是要先停止 +

+

+ docker  rm  $( docker  ps  -aq  ) +

+ +
+
+ + + + + + + + + + + + + + + +

+ Remove one or more images +

+

+ 移除 docker images 但是要先移除所有相關 container +

+ +
+
+ + + + + + + + + +

+ Copy files/folders between a container and the local filesystem +

+

+ 在 container 與 host 複製檔案 +

+

+ +

+

+ Usage: docker cp [OPTIONS] CONTAINER:PATH LOCALPATH|- +

+

+ docker cp [OPTIONS] LOCALPATH|- CONTAINER:PATH +

+ +
+
+ + + + + + +

+ 從 host 複製 host.txt 到名稱為testcopy 容器內 +

+

+ #docker  cp  host.txt   testcopy:/root/host.txt +

+

+ +

+

+ 從名稱為 testcopy 容器/root/file.txt 複製到目前的目錄 +

+

+ # docker  cp   testcopy:/root/file.txt   . +

+ +
+
+
+ + + + + + + +

+ Run a command in a running container +

+

+ +

+

+ Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...] +

+ +
+
+
+ + + + + + + +

+ Create a new image from a container's changes +

+

+ +

+

+ Usage: docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]] +

+ +
+
+
+ + + + + + + +

+ Inspect changes on a container's filesystem +

+

+ +

+

+ Usage: docker diff [OPTIONS] CONTAINER +

+ +
+
+ + + + + + +

+ # docker   diff   b7d075f34daf +

+

+ C /var +

+

+ A /var/lib/apt/lists/archive.ubuntu.com_ubuntu_dists_trusty-updates_InRelease +

+

+ D /var/cache/apt/pkgcache.bin +

+ +
+ + + + + + +

+ A - Add +

+

+ C - Change made +

+

+ D - deleted +

+ +
+
+
+
+ + + + + + + +

+ Export a container's filesystem as a tar archive +

+

+ 將 container 匯出為 tar 檔案 +

+

+ Usage: docker export [OPTIONS] CONTAINER +

+ +
+
+ + + + + + +

+ # docker   export   b7d075f34daf   >   update.tar +

+ +
+
+
+ + + + + + + +

+ Import the contents from a tarball to create a filesystem image +

+

+ +

+

+ Usage: docker  import  [OPTIONS]  file|URL|-   [REPOSITORY[:TAG]] +

+ +
+
+ + + + + + +

+ 將 update.tar 匯入為 test:update +

+

+ #docker  import  update.tar  test:update +

+

+ +

+

+ 將 update.tar 匯入為 test:update, 這邊使用 input redirection 方式, 所以要加 - +

+

+ #docker  import  -  test:update  <  update.tar +

+ +
+
+
+ + + + + + + +

+ Save an image(s) to a tar archive +

+

+ 將 image 匯出為 tar 檔案 +

+

+ Usage: docker save [OPTIONS] IMAGE [IMAGE...] +

+ +
+
+
+ + + + + + + +

+ Load an image from a tar archive or STDIN +

+

+ Usage: docker load [OPTIONS] +

+ +
+
+ + + + + + +

+ 將 update1.tar 匯入為 image, 如果不是用 input redirection 就要加上 -i +

+

+ # docker  load  -i  update1.tar +

+

+ +

+

+ 使用 input redirection 方式 +

+

+ # docker load < update1.tar +

+ +
+
+ + + + + + +

+ 跟 docker import 方式不同的是 +

+

+ docker load 會遵循之前 image 的 repo與tag +

+

+ 但是 docker import 則可以自訂 repo:tag +

+ +
+
+
+ + + + + + + +

+ Build an image from a Dockerfile +

+

+ 透過 Dockerfile 建立 image +

+

+ Usage: docker   build   [OPTIONS]    PATH | URL | - +

+ +
+
+ + + + + + + + + + + + + + +

+ Tag an image into a repository +

+

+ Usage: docker  tag  [OPTIONS]   IMAGE[:TAG]   [REGISTRYHOST/]  [USERNAME/]NAME[:TAG] +

+ +
+
+ + + + + + +

+ # docker  tag  ubuntu:14.04 foobar:cookbook +

+ +
+
+
+ + + + + + + +

+ Register or log in to a Docker registry +

+

+ Usage: docker  login  [OPTIONS]  [SERVER] +

+

+ +

+

+ Register or log in to a Docker registry. +

+

+ If no server is specified "https://index.docker.io/v1/" is the default. +

+ +
+
+ + + + + + +

+ 成功登入後相關 token 存放在 +

+

+ ~/.docker/config.json +

+ +
+
+
+ + + + + + + +

+ Search the Docker Hub for images +

+

+ Usage: docker search [OPTIONS] TERM +

+ +
+
+
+ + + + + + + +

+ Usage: docker port [OPTIONS] CONTAINER [PRIVATE_PORT[/PROTO]] +

+

+ +

+

+ List port mappings or a specific mapping for the CONTAINER +

+ + +
+
+ + +
+
+ + + + + + + +

+ 透過Dockerfile 建立 image +

+

+ 可以 #man  Dockerfile +

+ +
+
+ + + + + + + + + + + + + +

+ FROM  image@digest +

+ +
+
+
+ + + + + + + + + + +

+ 有兩種 form +

+

+ +

+

+ # the command is run in a shell - /bin/sh -c +

+

+ RUN <command> +

+

+ +

+

+ # Executable form +

+

+ RUN ["executable", "param1", "param2"] +

+ +
+
+ + + + + + +

+ Do not confuse RUN with CMD. RUN runs a command and commits the result. +

+

+ CMD executes nothing at build time, but specifies the intended command for the image. +

+ +
+
+
+ + + + + + + +

+ 有 3 種 forms +

+

+ +

+

+ # Executable form +

+

+ CMD ["executable", "param1", "param2"]` +

+

+ +

+

+ # Provide default arguments to ENTRYPOINT +

+

+ CMD ["param1", "param2"]` +

+

+ +

+

+ # the command is run in a shell - /bin/sh -c +

+

+ CMD command param1 param2 +

+ +
+
+ + + + + + + + + + +

+ The EXPOSE instruction informs Docker that the container listens on the specified network ports at runtime. +

+

+ 設定 container 預設 listen port +

+

+ +

+

+ EXPOSE <port> [<port>...] +

+ +
+
+
+ + + + + + + +

+ The ENV instruction sets the environment variable <key> to  the value <value>. This value is passed to all future  RUN, ENTRYPOINT, and CMD instructions. +

+

+ +

+

+ 設定變數 +

+

+ +

+

+ ENV <key> <value> +

+ +
+
+
+ + + + + + + +

+ ADD has two forms: +

+

+ ADD <src> <dest> +

+

+ +

+

+ # Required for paths with whitespace +

+

+ ADD ["<src>",... "<dest>"] +

+ +
+ + + + + + + + + + + + + +

+ COPY +

+

+          -- COPY has two forms: +

+

+                 COPY <src> <dest> +

+ + +
+ + +
+ + + + + + + +

+ ENTRYPOINT has two forms: +

+

+ +

+

+ # executable form +

+

+ ENTRYPOINT ["executable", "param1", "param2"]` +

+

+ +

+

+ # run command in a shell - /bin/sh -c +

+

+ ENTRYPOINT command param1 param2 +

+ +
+
+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + +

+ 方式 1 上網搜尋 +

+

+ https://hub.docker.com/ +

+ +
+
+ + + + + + +

+ 方式 2 使用指令 +

+

+ 使用 docker  search 來搜尋 +

+ +
+
+
+ + + + + + + +

+ 使用  docker  pull 來下載 image +

+

+ > docker  pull  opensuse:13.2 +

+ +
+
+
+ + + + + + + +

+ 方式 1 修改現有的映像檔 +

+ +
+ + + + + + +

+  下載 training/sinatra Repository映像檔 ( 可以注意到下載了所有 TAG 的映像檔, 從 training/sinatra ) +

+

+ +

+

+  > docker   pull   training/sinatra +

+

+ +

+

+  觀察 images +

+

+ > docker images +

+

+ +

+

+ 執行之前先列出目前執行的 Container +

+

+ docker  ps 可以列出 container +

+

+ -a 是列出所有的 container +

+

+ 如果沒有加上 -a 則是列出目前有執行指令的 container +

+

+ +

+

+  > docker  ps -a +

+

+ +

+

+ 執行 docker image 到  container ( 這次不加上 --rm, 也就是說 exit 之後不刪除 ) +

+

+ +

+

+  > docker    run    -i    -t    training/sinatra  /bin/bash +

+

+ +

+

+ 輸入 exit 離開 container +

+

+ root@6764848c94c4:/# exit +

+

+ +

+

+ 再次觀察 container +

+

+ > docker  ps -a +

+

+ +

+

+ 跟上面比對就會發現, 剛剛登入 shell 的時候 root@後面那串就是 container ID +

+

+ +

+

+ 再次執行一個新的 container +

+

+ +

+

+  > docker  run  -i  -t  training/sinatra  /bin/bash +

+

+ root@ddc3cdc142f3:/# +

+

+ +

+

+  這邊就會發現 container ID 是不一樣, 因為是另外一個 +

+

+ +

+

+  我們基於這個來修改成我們的映像檔 +

+

+ 安裝 json 套件來作出差異 +

+

+ root@ddc3cdc142f3:/# gem  install json +

+

+ +

+

+ 離開 container +

+

+ +

+

+ 這樣這個映像檔就跟之前不一樣了 +

+

+ 接下來透過 docker  commit 來建立新的 +

+

+ +

+

+ +

+

+ > docker   commit   -m "Add json"  -a "sakana"   ddc3cdc142f3    testrepo/testimage:vTEST +

+

+ 220501802d62b8aca6ed82aad9ea99c2dc9a12d195638b6bdf47f20aa8e92860 +

+

+ +

+

+  commit   提交一個新的 image +

+

+ -m 訊息Commit message +

+

+ -a  作者 Author (e.g., "John Hannibal Smith <hannibal@a-team.com>") +

+

+ ddc3cdc142f3 是container ID +

+

+ testrepo/testimage:vTEST 是 Repository名稱/映像檔名稱:TAG +

+

+ +

+

+  觀察 images +

+

+ > docker   images +

+

+ +

+

+ 這個時候就會知道, 如果我們日後不上傳到 Docker Hub, 其實 Repository名稱是可以自己取的, 沒有影響, 但是如果要上傳, 就是要註冊 Docker Hub的帳號, 符合上面的規則才可以. +

+ +
+
+
+ + + + + + + +

+ 我們可以透過 docker  build 配合 Dockerfile 來建立映像檔 +

+

+ +

+

+ +

+

+ 首先觀察目前的路徑 +

+

+ > pwd +

+

+ /home/max +

+

+ +

+

+  建立目錄 +

+

+ > mkdir  sakana +

+

+ +

+

+  進入該目錄 +

+

+ > cd   sakana/ +

+

+ +

+

+  建立 Dockerfile +

+

+ > vi   Dockerfile +

+

+ # 用於註解 +

+

+ # 後續的流程, 前面都要大寫 +

+

+ # FROM 基於那個 image +

+

+ FROM ubuntu:14.04 +

+

+ # MAINTAINER 維護作者 +

+

+ MAINTAINER Max Huang < sakana@study-area.org > +

+

+ # RUN 要執行的指令 +

+

+ RUN apt-get update && apt-get install -y ruby ruby-dev +

+

+ RUN gem install sinatra +

+

+ +

+

+  > docker    build    -t    sakanatest/testimage    /home/max/sakana +

+

+ +

+

+ > docker  images +

+ +
+
+
+
+ + + + + + + +

+ 可以透過  docker tag 來設定 映像檔的 TAG +

+

+ +

+

+  > docker  images +

+

+ REPOSITORY             TAG                 IMAGE ID            CREATED             VIRTUAL SIZE +

+

+ sakanatest/testimage   latest              dd38d73f4e20        2 minutes ago       318.7 MB +

+

+ testrepo/testimage     vTEST               220501802d62        37 minutes ago      452.1 MB +

+

+ ubuntu                 14.04               91e54dfb1179        13 days ago         188.3 MB +

+

+ ubuntu                 12.04               57bca5139a13        13 days ago         134.8 MB +

+

+ opensuse               13.2                d6b241b32a2d        5 weeks ago         93.99 MB +

+

+ opensuse               latest              d6b241b32a2d        5 weeks ago         93.99 MB +

+

+ training/sinatra       latest              f0f4ab557f95        15 months ago       447 MB +

+

+ +

+

+  > docker    tag    dd38d73f4e20    sakanatest/testimage:tagTest +

+ +
+
+
+ + + + + + + +

+ 先觀察 container +

+

+ > docker    ps     -a +

+

+ CONTAINER ID        IMAGE                      COMMAND             CREATED             STATUS                         PORTS               NAMES +

+

+ acfed6e70946        testrepo/testimage:vTEST   "/bin/bash"         About an hour ago   Exited (0) About an hour ago                       furious_pare        +

+

+ ddc3cdc142f3        training/sinatra           "/bin/bash"         About an hour ago   Exited (0) About an hour ago                       serene_tesla        +

+

+ 6764848c94c4        training/sinatra           "/bin/bash"         About an hour ago   Exited (0) About an hour ago                       elegant_lalande    +

+

+ +

+

+  觀察映像檔 +

+

+ 這邊可以觀察到 sakanatest/testimage 沒有container使用中 +

+

+ testrepo/testimage 以及 trainging/sinatra 有 container 使用中 +

+

+ +

+

+  > docker    images +

+

+ REPOSITORY             TAG                 IMAGE ID            CREATED             VIRTUAL SIZE +

+

+ sakanatest/testimage   latest              dd38d73f4e20        About an hour ago   318.7 MB +

+

+ sakanatest/testimage   tagTest             dd38d73f4e20        About an hour ago   318.7 MB +

+

+ testrepo/testimage     vTEST               220501802d62        About an hour ago   452.1 MB +

+

+ ubuntu                 14.04               91e54dfb1179        13 days ago         188.3 MB +

+

+ ubuntu                 12.04               57bca5139a13        13 days ago         134.8 MB +

+

+ opensuse               13.2                d6b241b32a2d        5 weeks ago         93.99 MB +

+

+ opensuse               latest              d6b241b32a2d        5 weeks ago         93.99 MB +

+

+ training/sinatra       latest              f0f4ab557f95        15 months ago       447 MB +

+

+ +

+

+ +

+

+ 我們可以使用 docker  rmi 來移除 沒有 container 使用中的 image +

+

+ +

+

+ 首先針對沒有 container 運作的  sakanatest/testimage 來刪除 +

+

+ > docker   rmi   sakanatest/testimage:tagTest +

+

+ Untagged: sakanatest/testimage:tagTest +

+

+ Deleted: dd38d73f4e2021c81c52530e99eb1cb98f87b9031d50c6342e7d47b8302f78a7 +

+

+ Deleted: b987f587ddb48e0006250cc77835530f605c32647d5cbe0432ea8a5fd3769017 +

+

+ Deleted: adbe1f76147105dc554a86f026dab8fa8b2019892bfb5d10cfe9e3df5bafa9b9 +

+

+ +

+

+  刪除不會有問題, 使用  docker  rmi 刪除所有 sakanatest/testimage 上面 image +

+ +
+
+
+ + + + + + +

+ 映像檔存放路徑 +

+ +
+ + +
+ + + + + + + + +

+ 觀察一次 +

+

+ > docker  ps  -a +

+

+ CONTAINER ID        IMAGE                      COMMAND             CREATED             STATUS                         PORTS               NAMES +

+

+ acfed6e70946        testrepo/testimage:vTEST   "/bin/bash"         About an hour ago   Exited (0) About an hour ago                       furious_pare        +

+

+ ddc3cdc142f3        training/sinatra           "/bin/bash"         About an hour ago   Exited (0) About an hour ago                       serene_tesla        +

+

+ 6764848c94c4        training/sinatra           "/bin/bash"         2 hours ago         Exited (0) 2 hours ago                             elegant_lalande +

+

+ +

+

+  移除 container 可以使用  docker  rm 指令來完成 +

+

+ +

+

+  > docker    rm    acfed6e70946 +

+

+ acfed6e70946 +

+

+ +

+

+  確認已經刪除 +

+

+ > docker  ps  -a +

+

+ CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                   PORTS               NAMES +

+

+ ddc3cdc142f3        training/sinatra    "/bin/bash"         2 hours ago         Exited (0) 2 hours ago                       serene_tesla        +

+

+ 6764848c94c4        training/sinatra    "/bin/bash"         2 hours ago         Exited (0) 2 hours ago                       elegant_lalande  +

+ +
+
+ + + + + + + +

+ docker  ps 指令加上 -q 只會列出 container ID +

+

+ > docker  ps  -a  -q +

+

+ ddc3cdc142f3 +

+

+ 6764848c94c4 +

+

+ +

+

+  但是做這件事的時候要三思而後行 +

+

+ > docker  rm  $( docker  ps  -a  -q ) +

+ +
+
+
+
+
+ + + + + + + + + + + + + + + + + +

+ Private Registry +

+

+ 私有倉庫 +

+ +
+ + + + + + +

+ 可以參考 +

+

+ https://philipzheng.gitbooks.io/docker_practice/content/repository/local_repo.html +

+ +
+ + + + + + + + + +

+ #docker  pull  registry:2 +

+

+ +

+

+ # docker   run   -d   -p  5000:5000   registry:2 +

+ +
+
+ + + + + + + + + + + + + + + +

+ 查詢私有倉庫 repo +

+

+ # curl -i http://localhost:5000/v2/_catalog +

+ +
+
+ + + + + + +

+ 查詢某個 repo 相關資訊, busy1 為 reponame, test2 為tag +

+

+ # curl -i http://localhost:5000/v2/busy1/manifests/test2 +

+ +
+
+ + + + + + +

+ 列出某個 repo 的 tag +

+

+ # curl -i http://localhost:5000/v2/busy1/tags/list +

+ +
+
+
+
+
+
+