Docker 初探,實驗室中的運貨鯨

Docker 初探,實驗室中的運貨鯨

http://blog.fntsr.tw/articles/2015/05/28/docker-talks-on-ncu/

學校實驗室總會有些需求,架設各種服務,例如:Redmine、MySQL、Wiki、GitLab ...
以往我們都會一股腦將這些服務架設在同一主機,或是各開一台虛擬機器,放在裡面,
然後就一屆傳一屆,只要不要掛掉就繼續使用。

但到了某天,發現舊版有漏洞,或是新版有更好用的功能,
才又挖出更新說明,辛辛苦苦的將各種服務一一更新上線。
又或是老闆要求架新服務,但才發現因為虛擬機器開太多 IP 不夠用了,
只好再度將各個服務都塞在同一台電腦裡,失去原本一台機器一個服務的用意。

有沒有什麼辦法去改善這種窘境?
或是有沒有更快的方式能方便我們部署我們需要的服務?
我想,Docker 或許就是你想要的答案。

「Build, Ship, Run Any App, Anywhere」—— Docker 如是說。

有別於虛擬機器的沈重,輕量的虛擬化技術 Container 正夯,
本講座將帶聽眾認識 Docker —— 一個方便操作 Container 的工具,
讓你以更方便、更快速的方式,管理你實驗室上的各個服務。

C6f3d51bca936e1d0ab0ee9e091545f9?s=128

Ruoshi Ling

May 28, 2015
Tweet

Transcript

  1. &QEMGT⓾㚃 ⻇洸⺅ℎ䥥挬徉滉 嗆夼 HPVUTNKMG 1

  2. 铪 5RGCMGT 雊侌➃矦㋲荈䧮➝稲♧♴ 2

  3. ꡠ倴䧮糵 :6&)6 ˖ 暟⟝㼬ぢ㻜뀿㹔螷ꂋ欰 ˖ 玑䒭鏤鎙柩焺鴍 ˖ 鮿넓䊨玑 ˖ 娜〷㷸

    ༨ჯਹ ͍ឝ 剢廪
  4. ꡠ倴䧮糵 Ⲵ蕰贡 3604)*-*/( ˖ 暟⟝㼬ぢ㻜뀿㹔螷ꂋ欰 ˖ 玑䒭鏤鎙柩焺鴍 ˖ 鮿넓䊨玑 ˖

    娜〷㷸 ଉᔛ ߰ൈ
  5. ꡠ倴䧮糵 GOUTSMJLF ˖ 暟⟝㼬ぢ㻜뀿㹔螷ꂋ欰 ˖ 玑䒭鏤鎙柩焺鴍 ˖ 鮿넓䊨玑 ˖ 娜〷㷸

    ϥึ ᗇ׼ 卌剅箰
  6. ꡠ倴䧮糵 넞♧ ✳겳穉 ♲겳穉 娜〷㷸禺 鮿넓䊨玑 灇瑖䨾 ↛㡨珮䰟䲲 䛧テ 䠀䕊

       ℎ 哩 Ⰸ ⹙ 㡨 縠 涹 ℎ ℎⰏ Ⰸ⹙  䛟⨉∮剏
  7. ꡠ倴䧮糵 NCHU EECSeC 哩
 Ⰸ
 ⹙
 䠀
 㧤
 羝
 峫


    揉 哩
 Ⰸ
 槜 羝 ⛭ 痙 䰟 劅 䥝群↛ 㥲㨀⻇劳䠀 ⒁憮楫⇮
  8. ꡠ倴䧮糵 㥶⡦欽%PDLFS 鿈縭1)1㼠呪 麌⹛ꨣ畮 %PDLFSⴲ䱳 㻜뀿㹔⚥涸麌顊댄 鰋ꆀ秹贡亼⻋䪮遯 %PDLFSⰅꟌ䒸㼬 瘞䖊鍑ꓳ źPX

    瘞䖊鍑ꓳ
  9. 陏 &QEMGT 矦㋲➝稲%PDLFS屡ꬠ 9

  10. ꨾宠

  11. 錚 %QPEGRV %PDLFS湱ꡠ錚䙂 11

  12. None
  13. 卺 +PUVCNN 㸞酤%PDLFS矦鶤 13

  14. %JFSFODFCFUXFFO04 .KPWZ 15:9KPFQYU

  15. *OTUBMM%PDLFSPO6CVOUV 4BVDZ 5SVTUZ -54 1SFDJTF -54 ,FSOFM *OTUBMM QSFSFRVJTJUFT &YFDVUF

    DPNNBOE $  wget  -­‐qO-­‐  https://get.docker.com/  |  sh 5WRRQTV 8GTUKQP
  16. *OUBMM%PDLFSPO.BD049 )7+ %.+ Kitematic 瞗宾㵵 JQOGDTGY

  17. *OUBMM%PDLFSPO.BD049

  18. ⟂ %QOOCPF %PDLFS䭸⟂♧錒莅➝稲 18

  19. None
  20. 㛇燊䭸⟂ #BTJD$PNNBOE $  [sudo]  docker  [command]  [flags]  [arguments]

  21. $ docker Commands: attach Attach to a running container build

    Build an image from a Dockerfile commit Create a new image from a container's changes $  docker 㛇燊䭸⟂ #BTJD$PNNBOE
  22. $ docker version Client version: 1.62 Client API version: 1.18

    Go version (client): go1.4.1 Git commit (client): a8a31ef OS/Arch (client): darwin/amd64 Server version: 1.6.2 Server API version: 1.18 Go version (server): go1.4.2 Git commit (server): 4749651 $  docker  version 㛇燊䭸⟂ #BTJD$PNNBOE 嗆5GTXGT羝峫㽳ⓛ䛟 孉䰛㽳㧪桬界珮聏倱㓱▀
  23. $ docker info Containers: 3 Images: 65 Storage Driver: aufs

    Root Dir: /mnt/sda1/var/lib/docker/aufs … Execution Driver: native-0.2 Kernel Version: 4.0.3-boot2docker Operating System: Boot2Docker 1.6.2 (TCL 5.4); …… $  docker  info 㛇燊䭸⟂ #BTJD$PNNBOE
  24. $ docker run --help Usage: docker run [OPTIONS] IMAGE [COMMAND]

    [ARG...] Run a command in a new container …… $  docker  command  -­‐-­‐help 㛇燊䭸⟂ #BTJD$PNNBOE
  25. 䖰%PDLFS)VCꟚ㨥 (FUUJOH4UBSUFEXJUI%PDLFS)VC IUUQTIVCEPDLFSDPN

  26. 痧♧佅%PDLFSJ[F䥰欽玑䒭 %PDLFSJ[JOH"QQMJDBUJPOT")FMMPXPSME $  docker  run  image  execute  [argument,  …]

  27. $ docker run ubuntu:14.04 /bin/echo 'Hello world' Hello world 痧♧佅%PDLFSJ[F䥰欽玑䒭

    %PDLFSJ[JOH"QQMJDBUJPOT")FMMPXPSME $  docker  run  image  execute  [argument,  …]
  28. $ docker run -it ubuntu:14.04 /bin/bash root@af8bae53bdd3:/# 痧♧佅%PDLFSJ[F䥰欽玑䒭 %PDLFSJ[JOH"QQMJDBUJPOT")FMMPXPSME $

     docker  run  -­‐it  image  execute  [argument,  …] #P+PVGTCEVKXG%QPVCKPGT
  29. $ docker run -d ubuntu:14.04 /bin/echo \ 
 -c "while

    true; do echo hello world; sleep 1; done" 1e5535038e285177d5214659a068137486f96ee5c2e85a4ac52dc83f2ebe4147 痧♧佅%PDLFSJ[F䥰欽玑䒭 %PDLFSJ[JOH"QQMJDBUJPOT")FMMPXPSME $  docker  run  -­‐d  image  execute  [argument,  …] #&CGOQPK\GF*GNNQYQTNF
  30. $ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS NAMES

    1e5535038e28 ubuntu:14.04 /bin/sh… 2 minutes ago Up 1 minute insane_babbage 痧♧佅%PDLFSJ[F䥰欽玑䒭 %PDLFSJ[JOH"QQMJDBUJPOT")FMMPXPSME $  docker  ps #&CGOQPK\GF*GNNQYQTNF
  31. $ docker logs 1e5535038e28 hello world hello world hello world

    痧♧佅%PDLFSJ[F䥰欽玑䒭 %PDLFSJ[JOH"QQMJDBUJPOT")FMMPXPSME $  docker  logs  [container  ID/Name] #&CGOQPK\GF*GNNQYQTNF
  32. $ docker logs insane_babbage hello world hello world hello world

    痧♧佅%PDLFSJ[F䥰欽玑䒭 %PDLFSJ[JOH"QQMJDBUJPOT")FMMPXPSME $  docker  logs  [container  ID/Name] #&CGOQPK\GF*GNNQYQTNF
  33. 㺂㐼 8PSLJOHXJUI$POUBJOFST $  docker  run  -­‐P|-­‐p  … 2QTV

  34. $ docker run -d -P nginx:latest Unable to find image

    'nginx:latest' locally latest: Pulling from nginx 39bb80489af7: Pull complete …… 72d73c46937a: Pull complete Status: Downloaded newer image for nginx:latest 23e601370946fc9fbdfd6204e1bf70e88e6c18078a0c6ee8640acd2e6694b645 㺂㐼 8PSLJOHXJUI$POUBJOFST $  docker  run  -­‐d  -­‐P  image  execute  [argument,  …] 2QTV
  35. $ docker ps CONTAINER ID IMAGE COMMAND STATUS PORTS 23e601370946

    nginx:latest "nginx … Up 1 minute 0.0.0.0:32768->443/tcp
 0.0.0.0:32769->80/tcp $  docker  ps 㺂㐼 8PSLJOHXJUI$POUBJOFST 2QTV
  36. $ boot2socker ip 192.168.59.103 $  boot2ocker  ip 㺂㐼 8PSLJOHXJUI$POUBJOFST 2QTV

  37. 㺂㐼 8PSLJOHXJUI$POUBJOFST IUUQ 2QTV

  38. $ docker run -d -p 80 nginx:latest 40dd6835e35843fdc1853e92b9922f6406f874793c085e1f71cb3967e1157cf3 㺂㐼 8PSLJOHXJUI$POUBJOFST

    $  docker  run  -­‐d  -­‐p  container_port  … 2QTV
  39. $ docker ps CONTAINER ID IMAGE COMMAND STATUS PORTS 40dd6835e358

    nginx:latest "nginx … Up 1 minute 443/tcp
 0.0.0.0:32770->80/tcp $  docker  ps 㺂㐼 8PSLJOHXJUI$POUBJOFST 2QTV
  40. $ docker run -d -p 10080:80 nginx:latest 40dd6835e35843fdc1853e92b9922f6406f874793c085e1f71cb3967e1157cf3 㺂㐼 8PSLJOHXJUI$POUBJOFST

    $  docker  run  -­‐d  -­‐p  host_port:container_port  … 2QTV
  41. $ docker ps CONTAINER ID IMAGE COMMAND STATUS PORTS 40dd6835e358

    nginx:latest "nginx … Up 1 minute 443/tcp
 0.0.0.0:10080->80/tcp $  docker  ps 㺂㐼 8PSLJOHXJUI$POUBJOFST 2QTV
  42. 㺂㐼 8PSLJOHXJUI$POUBJOFST $  docker  run  -­‐P|-­‐p  [argument,  …] 2QTV

  43. $ docker run -d -P nginx:latest Unable to find image

    'nginx:latest' locally latest: Pulling from nginx 39bb80489af7: Pull complete …… 72d73c46937a: Pull complete Status: Downloaded newer image for nginx:latest 23e601370946fc9fbdfd6204e1bf70e88e6c18078a0c6ee8640acd2e6694b645 㺂㐼 8PSLJOHXJUI$POUBJOFST $  docker  run  -­‐d  -­‐P  image  execute  [argument,  …] 2QTV
  44. $ docker ps CONTAINER ID IMAGE COMMAND STATUS PORTS 23e601370946

    nginx:latest "nginx … Up 1 minute 0.0.0.0:32768->443/tcp
 0.0.0.0:32769->80/tcp $  docker  ps 㺂㐼 8PSLJOHXJUI$POUBJOFST 2QTV
  45. $ boot2socker ip 192.168.59.103 $  boot2ocker  ip 㺂㐼 8PSLJOHXJUI$POUBJOFST 2QTV

  46. 㺂㐼 8PSLJOHXJUI$POUBJOFST IUUQ 2QTV

  47. $ docker run -d -p 80 nginx:latest 40dd6835e35843fdc1853e92b9922f6406f874793c085e1f71cb3967e1157cf3 㺂㐼 8PSLJOHXJUI$POUBJOFST

    $  docker  run  -­‐d  -­‐p  container_port  … 2QTV
  48. $ docker ps CONTAINER ID IMAGE COMMAND STATUS PORTS 40dd6835e358

    nginx:latest "nginx … Up 1 minute 443/tcp
 0.0.0.0:32770->80/tcp $  docker  ps 㺂㐼 8PSLJOHXJUI$POUBJOFST 2QTV
  49. $ docker run -d -p 10080:80 nginx:latest 40dd6835e35843fdc1853e92b9922f6406f874793c085e1f71cb3967e1157cf3 㺂㐼 8PSLJOHXJUI$POUBJOFST

    $  docker  run  -­‐d  -­‐p  host_port:container_port  … 2QTV
  50. $ docker ps CONTAINER ID IMAGE COMMAND STATUS PORTS 40dd6835e358

    nginx:latest "nginx … Up 1 minute 443/tcp
 0.0.0.0:10080->80/tcp $  docker  ps 㺂㐼 8PSLJOHXJUI$POUBJOFST 2QTV
  51. 㺂㐼 8PSLJOHXJUI$POUBJOFST $  docker  start|restart|stop|kill  contanter 5VCTVTGUVCTVUVQRMKNN

  52. $ docker stop nginx_container nginx_container $  docker  stop  [container  ID/Name]

    㺂㐼 8PSLJOHXJUI$POUBJOFST 5VCTVTGUVCTVUVQRMKNN
  53. $ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS NAMES

    $  docker  ps 㺂㐼 8PSLJOHXJUI$POUBJOFST 5VCTVTGUVCTVUVQRMKNN
  54. $ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS

    NAMES 23e601370946 nginx:latest "nginx … 2 minutes ago EXITED(0) nginx_container $  docker  ps  -­‐a 㺂㐼 8PSLJOHXJUI$POUBJOFST 5VCTVTGUVCTVUVQRMKNN
  55. $ docker start nginx_container nginx_container $  docker  start  [container  ID/Name]

    㺂㐼 8PSLJOHXJUI$POUBJOFST 5VCTVTGUVCTVUVQRMKNN
  56. $ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS NAMES

    23e601370946 nginx:latest "nginx … 10 minutes ago Up 1 minute nginx_container 痧♧佅%PDLFSJ[F䥰欽玑䒭 %PDLFSJ[JOH"QQMJDBUJPOT")FMMPXPSME $  docker  ps
  57. $ docker restart nginx_container nginx_container $  docker  restart  [container  ID/Name]

    㺂㐼 8PSLJOHXJUI$POUBJOFST 5VCTVTGUVCTVUVQRMKNN
  58. $ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS NAMES

    23e601370946 nginx:latest "nginx … 15 minutes ago Up 1 minute insane_babbage 痧♧佅%PDLFSJ[F䥰欽玑䒭 %PDLFSJ[JOH"QQMJDBUJPOT")FMMPXPSME $  docker  ps
  59. $ docker stop nginx_container nginx_container $ docker kill nginx_container nginx_container

    $  docker  kill  [container  ID/Name] 㺂㐼 8PSLJOHXJUI$POUBJOFST 5VCTVTGUVCTVUVQRMKNN
  60. $ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS

    NAMES $  docker  ps  -­‐a 㺂㐼 8PSLJOHXJUI$POUBJOFST 5VCTVTGUVCTVUVQRMKNN
  61. $  docker  images|pull|rmi 僦⫹墂 8PSLJOHXJUI%PDLFS*NBHFT

  62. $ docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE

    nginx latest a785ba7493fd 16 hours ago 132.9 MB $  docker  images 僦⫹墂 8PSLJOHXJUI%PDLFS*NBHFT ⓸ⓛ
  63. $ docker pull mysql:latest latest: Pulling from mysql 7a3e804ed6c0: Pull

    complete …… ff78d9bb5f46: Already exists Status: Downloaded newer image for mysql:latest $  docker  pull  image_name:tag 僦⫹墂 8PSLJOHXJUI%PDLFS*NBHFT 㖪甩
  64. $ docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE

    nginx latest a785ba7493fd 16 hours ago 132.9 MB mysql latest ff78d9bb5f46 3 days ago 282.9 MB $  docker  images 僦⫹墂 8PSLJOHXJUI%PDLFS*NBHFT ⓸ⓛ
  65. $ docker rmi mysql:latest Untagged: mysql:latest Deleted: ff78d9bb5f46b3a619310a8e3f3e62b98ce08b476d0d5d4088f1853d604f8218 …… Deleted:

    7a3e804ed6c015465a6cb957d032276dbff7ba8582153c38b83acd70ffdad0c6 $  docker  rmi  image_name:tag|image_id 僦⫹墂 8PSLJOHXJUI%PDLFS*NBHFT ┋楅
  66. 耢穡㺂㐼 -JOLJOH$POUBJOFST5PHFUIFS

  67. 盘椚㺂㐼⚥涸须俲 .BOBHJOH%BUBJO$POUBJOFST

  68. 荈鎎䖕鏤须俲 "QQMZDVTUPNNFUBEBUB

  69. 㐼 6QQNU %PDLFS湱ꡠ䊨Ⱘ 69

  70. %PDLFS$PNQPTF #  curl  -­‐L  https://github.com/docker/compose/releases/……  >  /usr/local/bin/docker-­‐compose   #  chmod

     +x  /usr/local/bin/docker-­‐compose $  sudo  pip  install  -­‐U  docker-­‐compose *OTUBMMWJB$63- *OTUBMMWJBQJQ
  71. %PDLFS$PNQPTF # docker-compose.yml web: build: . command: python app.py ports:

    - "5000:5000" volumes: - .:/code links: - redis
  72. %PDLFS$PNQPTF # docker-compose.yml service: ... ports: - "3000" - "8000:8000"

    - "49100:22" - "127.0.0.1:8001:8001"
  73. %PDLFS$PNQPTF # docker-compose.yml service: … volumes: - /var/lib/mysql - cache/:/tmp/cache

    - ~/configs:/etc/configs/:ro
  74. %PDLFS$PNQPTF # docker-compose.yml service: ... environment: RACK_ENV: development SESSION_SECRET: environment:

    - RACK_ENV=development - SESSION_SECRET
  75. $ docker-compose up -d Creating swl_service_1… $ docker-compose up -d

    Recreating swl_service_1… $ docker-compose up -d --no-recreate $  docker-­‐compose  up  -­‐d %PDLFS$PNQPTF 61 ㇛䶬猺撮㇛猻珮界▶
  76. $ docker-compose stop Stopping swl_service_1... $  docker-­‐compose  stop %PDLFS$PNQPTF ㇛䶬猺撮㇛猻珮界▶⺚⥉

  77. $ docker-compose start Starting swl_service_1... $  docker-­‐compose  start %PDLFS$PNQPTF 界▶⺚⥉

  78. $ docker-compose restart Restarting swl_service_1... $  docker-­‐compose  restart %PDLFS$PNQPTF 撮界⺚⥉

  79. $ docker-compose rm No stopped containers $ docker-compose stop &&

    docker-compose rm Going to remove swl_service_1 Are you sure? [yN] y Removing swl_service_1... $  docker-­‐compose  rm %PDLFS$PNQPTF ┋楅ビ⌽㹃䥥⺚⥉
  80. ⢿ 5CORNGQT&GOQ 㥶⡦欽%PDLFS 卺饱㻜뀿㹔䌢欽剪⹡ 80

  81. None
  82. ⻋ &QEMGTK\G 荈䊹㻨%PDLFSMF㼓酤♧⦐*NBHF 82

  83. ㉏ 3WGUVKQPCPF#PUYGT Ꟛ佞㉏瘸 83

  84. 闒 6JCPM[QW 䠮闒翕聃 84 CMPHGOUTSUX