當專案漸趕,當遷移也不再那麼難 (Ship Your Projects with Docker EcoSystem)

當專案漸趕,當遷移也不再那麼難 (Ship Your Projects with Docker EcoSystem)

距離去年講述「運用 Docker 部署 PHP 專案」已經過了一年,Docker 生態系逐漸成長,Machine、Compose、Swarm 皆趨於穩定、值得投入。本議程將承襲去年議程的概念,並講述如何透過這些工具利用 Docker 協助我們更方便的開發、測試、部署、遷移我們的 PHP 專案。

Ps. 本議程謹會概略提到 Docker 概念作為複習,不會針對指令一一教學、建議會眾在議程前,能先了解 Docker 基本使用方式。

C6f3d51bca936e1d0ab0ee9e091545f9?s=128

Ruoshi Ling

October 09, 2015
Tweet

Transcript

  1. 䡗⻩㬩䈙恶 䡗捘䳜⅀⃮⑮掄熝槄 嗆夼獒HPVUTNKMG 1 4IJQ:PVS1SPKFDUTXJUI%PDLFS&DP4ZTUFN 1JDUVSF$SFEJUEPDLFSDPN PHPConf Taiwan 2015 WFSTJPO

  2. 晝劥刿倞私ꏗ 2 粚๜ 狕ྋ獉਻ 2015-11-01-a 狕ྋ p45 㻌ਁ梊藮ҁ VALUME ->

    VOLUME҂牧眤蘛 @date
  3. 铪 3 Ŏ ຋䨗翨ҁ舙蒅҂ Ŏ 3+3HU\HDUV Ŏ 3+3&RQI7DLZDQ6SHDNHU Ŏ 'RFNHU7DLSHLPHPEHU

    Ŏ KWWSIQWVUWZ 5RGCMGT
  4. 㣐笧0VUMJOF ˙ 陏%PDLFS涮㾝噲矦➝ ˙ 燊%PDLFS㛇燊錚䙂 ˙ 卺㸞酤%PDLFS ˙ 麌%PDLFS麌⡲倰䒭 ˙

    㐼%PDLFS$PNQPTF➝稲 ˙ ⢿鼄獵鿈縭1)1㼠呪 4
  5. 陏 &QEMGT 矦㋲➝稲%PDLFS鸏䎙䎃涸佖隶 5

  6. 䖰ꟗ彂ⵌꟚ彂 6 1JDUVSF$SFEJUEPU$MPVUDPN EPDLFSDPN 

  7. 刿㢵㥪欽涸䊨Ⱘ姻䒭⸈Ⰵ侮ざ䧴钲欰 7 machine compose 1IPUP$SFEJUIUUQXXXHTI IUUQTHJUIVCDPNCPPUEPDLFS IUUQTHJUIVCDPNEPDLFSDPNQPTF IUUQTHJUIVCDPNEPDLFSNBDIJOF

  8. 刿⸈䧭擿涸鰊⸔䊨Ⱘ莅欰䢀㕖 8 1JDUVSF$SFEJUEPDLFSDPN

  9. 燊 (QWPFCVKQP %PDLFS㛇燊錚䙂 9

  10. 10 鯨⿂搬運的貨櫃 在你還在打包的時候 貨櫃早就部署好了 你有想過這個嗎? 沒有 因為你只會想到你⾃⼰。 1IPUP$SFEJUIUUQT[IZVFXJLJQFEJBPSHXJLJ⽂ꬌꟀ걸뛩

  11. Foundation 11 Host OS Hypervisor Guest OS … 7JSUVBM.BDIJOF 㖈➝稲㺂㐼⛓⵹⯓⢵溏溏贡亼堥㐼

  12. Foundation 12 Linux Kernel Linux Distribution FS … :PVS-JOVY04

  13. Foundation 13 Linux Kernel Linux Distribution FS … Base Image

    FS …. :PVS-JOVY04 %PDLFS*NBHF
  14. Foundation 14 Linux Kernel Your Environments Container 䁆ᤈ絑ह Linux Distribution

    FS Base Image FS ….
  15. Foundation 15 Linux Kernel Linux Distribution FS Your Environments Base

    Image FS Images Container 䁆ᤈ絑ह Process Process
  16. Image 16 Linux Kernel Base Image Image Layer A Image

    Layer B
  17. Image 17 Linux Kernel Base Image Image Layer A Image

    Layer B Image Layer ከ Image Layer C Image Layer Ԛ Image Layer Ӱ
  18. Image 18 Linux Kernel Ubuntu PHP 5.5 Apache PHP 7

    Nginx Apache Nginx
  19. Image 19 Linux Kernel Ubuntu PHP 5.5 Apache PHP 7

    Nginx Apache Nginx CentOS
  20. Container 20 Images Container

  21. Container Container 21 Images Process Executing Running

  22. Container Container 22 Images Exited (Stop)

  23. Container 23 Images Changed FS Process Executing QPSUFYQPTF Host FS

    WPMVNFT Running
  24. Container Container 24 Images Changed FS Host FS WPMVNFT Exited

    (Stop)
  25. Container A Container Links 25 Container B Container C MJOL

    MJOL FYQPTF
  26. App Container Links 26 SQL DB Redis QPSU QPSU QPSU

  27. Links Contributes  腋麨ⵌ0OF$POUBJOFS0OF4FSWJDF涸鏤鎙♶欽䪾 䥰欽玑䒭须俲䏨瘞鿪㝱鹎♧⦐JNBHF⿡駵  䪭♳㔔姽Ⱖ⚥♧갪剪⹡刿倞⛳〫꨾銴剐䳖䱈♧⦐ DPOUBJOFS罜♶欽侮⦐剪⹡鿪ꅾ䒊 27

  28. Container Volumes 3f8566045a0994e..... Host /var/www/html …… …… Container A (3f8566045a....)

    Volume WBSMJCEPDLFSWPMVNFT
  29. Container Volumes Host /var/www/html …… …… Volume /var/www/html …… Container

    B Volume
 From 3f8566045a0994e..... Container A (3f8566045a....) WBSMJCEPDLFSWPMVNFT
  30. Container Volumes Host …… 3f8566045a0994e..... WBSMJCEPDLFSWPMVNFT

  31. Container Volumes Custom Directory Host /var/www/html …… …… Container A

    Volume 蕰僽$POUBJOFS酭꬗䭸㹁涸鑪须俲㣰䊺竤㶸㖈 ⵱剚鄄)PTU涸须俲㣰窍銼覈
  32. Volume Contributes  雊$POUBJOFS酭꬗涸须俲鼇乵䚍莅)PTU㼩鸮  〳⟃雊㢵⦐$POUBJOFSⰟ欽ず⦐须俲 32

  33. 卺 +PUVCNN 㸞酤%PDLFS矦鶤 33

  34. 㖈-JOVY♳莅㖈Ⱖ➮04♳麌⡲涸䊴ⴽ 34 1IPUP$SFEJUIUUQTEPDTEPDLFSDPNJOTUBMMBUJPONBD

  35. %PDLFS5PPMCPY "MMJO0OF 35 4DSFFOTIPUIUUQTXXXEPDLFSDPNUPPMCPY

  36. 8IBUEPFTJUDPOUBJOT 36 ˙ %PDLFS$MJFOU ˙ #PPU%PDLFS ˙ %PDLFS.BDIJOF ˙ %PDLFS$PNQPTF

    ˙ 7JSUVBM#PY ˙ ,JUFNBUJD
  37. ,JUFNBUJD 37

  38. %PDLFS2VJDL4UBSU5FSNJOBM 38

  39. 麌 *QYKVYQTMU %PDLFS湱ꡠ䊨Ⱘ

  40. )PXJUXPSLT 40

  41. %PDLFSMF FROM php:7-apache
 MAINTAINER<Ruoshi Ling <foo@bar.tw>
 
 RUN apt-get update


    
 ADD ./script.php /app/
 
 ENTRYPOINT php
 CMD ["app/script.php"] 41
  42. Skills (1)  ⵄ欽䧴=䪾㢵⦐36/䧴Ⱖ➮䭸⟂鸮㖈♧饱 42 RUN apt-get update \
 &&

    apt-get install -y \
 wget \ 
 curl \
 php5 \
 && echo "Installed basic packages"
  43. Skills (2)  ⵄ欽&/7鏤縨갸鏤涸橇㞯隶侸 43 ENV APP_VERSION=1.2.3 \
 DB_USER="foo" \


    DB_PASSWORD="bar" \
 DB_DATABASE="app" \
 DB_HOST="127.0.0.1" \
 ENV=production
  44. Skills (3)  ⵄ欽36/㛂遤TIFMMTDSJQU⨞橇㞯ⴲ㨥⻋ 44 ENV SETUP_DIR="/var/cache/app"
 
 COPY assets/setup/

    ${SETUP_DIR}/
 
 RUN bash ${SETUP_DIR}/install.sh
  45. Skills (4)  管㻨TIFMMTDSJQU⨞捀鹎Ⰵ럊&OUSZ1PJOU雊 $POUBJOFS㛂遤"QQ㉬⹛⵹갸⯓갭㉬⹛涸玑䎸⛳ 腋隡《橇㞯隶侸⿡⨞湱㼩䥰涸贖椚 45 COPY entrypoint.sh /sbin/entrypoint.sh


    RUN chmod 755 /sbin/entrypoint.sh
 
 ENTRYPOINT ["/sbin/entrypoint.sh"]
 CMD ["app:start"]
  46. Skills (5)  〳䒊用♧⦐㼠Ꟍ佞%BUB涸须俲㣰ⵄ欽MO㼟䏁♴涸 㶩须俲㣰㖈"QQ㶸佞涸须俲涸㖒倰䒊用䰦䖜㥶 姽〫꨾銴7PMVNF鑪%BUB须俲㣰⽰〳 46 #!/bin/bash
 ln -sf

    /data/db /var/lib/mysql
 ln -sf /data/uploads /app/uploads ......
 VOLUME ["/data"]
 ENTRYPOINT ["/sbin/entrypoint.sh"]
  47. Contributes  刿僈焷涸䒊用橇㞯䨾꨾涸갪湡  鼄獵刿⸈鰋䊫〫꨾䌟鸏⟨鏤㹁ⵌ倞堥㐼⿡CVJME⽰ 〳䒊用湱ず橇㞯䧴䥰欽玑䒭  嫲饱EPDLFSDPNNJU刿⸈㥪笞隌JNBHF⛳刿腋雊㢵 ➃⼿⡲佖㊥JNBHF涸䒊縨 47

  48. 㐼 1TEJGUVTCVKQPCPF6QQNU %PDLFS湱ꡠ䊨Ⱘ 48 compose swarm machine

  49. 49 docker machine

  50. $SFBUF-PDBM.BDIJOF 50 $ docker-machine create \ --driver virtualbox default Creating

    VirtualBox VM... Creating SSH key... Starting VirtualBox VM... Starting VM... To see how to connect Docker to this machine, run: docker-machine env default
  51. $IBOHFEPDLFSDMJFOUFOWJSPONFOU 51 $ docker-machine env default
 export DOCKER_TLS_VERIFY="1" export DOCKER_HOST="tcp://192.168.99.100:2376"

    export DOCKER_CERT_PATH="/Users/fntsr/.docker/ machine/machines/default" export DOCKER_MACHINE_NAME="default" # Run this command to configure your shell: # eval "$(docker-machine env default)" $ eval "$(docker-machine env default)"
  52. $IFDLJTTFSWFSDPOOFDUJOH 52 $ docker version Client: Version: 1.8.2 .... Server:

    Version: 1.8.2 API version: 1.20 Go version: go1.4.2 Git commit: 0a8c2e3 Built: Thu Sep 10 19:10:10 UTC 2015 OS/Arch: linux/amd64
  53. %SJWFST 53 $ docker-machine create --driver .... Generic Create machines

    using an existing VM/Host with SSH.
  54. $POUSJCVUFT  㖈劥堥䊺㶸㖈涸堥㐼ぐꨣ畮⣘䥰㉂♳麌遤 %PDLFS刿⸈倰꬗  ꦑ儘ⴗ䳖Ꟛ涮庠鑑姻䒭橇㞯涸%PDLFS堥㐼鹎 遤㼓酤鼄獵鿈縭  䵩ꂂ%PDLFS4XBSN刿䘰鸠䒊用%PDLFS$MVTUFS 

    ♶欽TTI黇畮堥㐼⽰〳㖈劥堥欽EPDLFSDMJFOU乽 ⡲劥㖒㻨艁劥麌遤㖈黇倰 54
  55. 55 docker compose

  56. 56 自從會用 Docker Compose 後,我 就不再下指令了呢。 認同請分享 身體健康、萬事如意、南無阿彌陀佛 有緣認識你真好

  57. 57 # Comment <service_name>: attribute: <value> attribute: - item1 -

    item2 'PSNBU
  58. 58 # docker-compose.yml web: image: php:7-fpm ports: - "5566:80" volumes:

    - .:/app #BTF
  59. $PNQPTF$PNNBOE 59 # create container(s) $ docker-compose up -d #

    start container(s) $ docker-compose start # stop container(s) $ docker-compose stop # remove container(s) $ docker-compose rm
  60. 60 # docker-compose.yml web: build: ./ links: - mysql ports:

    - "9527:80" mysql: image: mysql:5.7 environment: - MYSQL_ROOT_PASSWORD=password - MYSQL_DATABASE=wordpress .VMUJ$POUBJOFST
  61. &YUFOET 61 # development.yml web: extends: file: docker-compose.yml service: web

    environment: - ENV=development - DB=sqlite3
  62. Skills (1)  ⵄ欽FOWJSPONFOU鏤縨橇㞯隶侸《➿%PDLFSMF涸 갸鏤⧩ 62 # docker-compose.yml web: ....

    environment: - DB_USER=elephant
 - DB_PASSWORD=ilovePHPfOEvEr
 - DB_DATABASE=elephant
  63. Skills (2)  〳欽湱㼩騟䖜鏤縨)PTU7PMVNF竁㹁雊鏤㹁墂鼄 獵刿⸈⤑ⵄ 63 # docker-compose.yml web: volumes:

    - ./app:/app db: volumes: - ./db:/var/lib/mysql
  64. Skills (3)  鏤縨♧⦐DPNNPOZNMⱄⵄ欽FYUFOE醢⡲Ꟛ 涮庠鑑姻䒭橇㞯涸鏤㹁墂 64 # docker-compose.yml web: extends:

    file: common.yml service: web environment: - ENV=testing - DB=mysqli db: ......
  65. Skills (4)  ⵄ欽SFTUBSU鼚⯝DPOUBJOFS♴箁㽠皿ꅾ㉬)PTU ⛳剚荈⹛Ꟛ㉬ 65 # docker-compose.yml web: ....

    restart: always
  66. $POUSJCVUFT  ♶欽ⱄ⫹⟃⵹♧垺捀✫Ꟛ㉬㢵⦐DPOUBJOFS♴㥪䎙 妄`docker run`  ⫹EPDLFSMF⛓倴JNBHFTDPNQPTF雊 DPOUBJOFS涸盘椚笞隌鿈縭刿⸈倰⤑⡜䧭劥  㼩倴♧⦐DPOUBJOFS♧⦐TFSWJDF涸垸䒭刿㥪㻜植

    66
  67. ⢿ 5CORNGQT&GOQ 㻜⢿怵箻 67

  68. 68 IUUQTIVCEPDLFSDPN@XPSEQSFTT 卺鏤1)1$.4

  69. 69 web: image: wordpress links: - db environment: - WORDPRESS_DB_PASSWORD=password

    ports: - "9527:80" db: image: mysql:5.7 environment: - MYSQL_ROOT_PASSWORD=password - MYSQL_DATABASE=wordpress
  70. 70 IUUQTIVCEPDLFSDPN@QIQ ⢪欽ぐ1)1晝劥

  71. 71 php5: extends: file: app.yml service: web image: php:5-fpm ports:

    - "11024:80" php7: extends: file: app.yml service: web image: php:7-fpm ports: - "11025:80"
  72. ㉏ 3WGUVKQPCPF#PUYGT Ꟛ佞㉏瘸 72

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