Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Dockerで作るPHP開発環境(入門編)

B63ed943582d5b36a78f3be3c4d659e2?s=47 Takashi Nojima
September 12, 2018

 Dockerで作るPHP開発環境(入門編)

2018.09.12 博多TECH塾

デモ・サンプルコード
https://github.com/nojimage/hakata-tech-php-docker/tree/master/vol0

B63ed943582d5b36a78f3be3c4d659e2?s=128

Takashi Nojima

September 12, 2018
Tweet

Transcript

  1. DockerͰ࡞ΔPHP։ൃ؀ڥ
 ʢೖ໳ฤʣ 
 തଟTECHक़ Vol.55 2018.09

  2. About Me ໊લɿ໺ౡོʢͷ͡·͔ͨ͠ʣ ॴଐɿΠϥεςΟοΫίϯαϧλϯπגࣜձࣾ
 IUUQTFMBTUJDDPOTVMUBOUTDPNʲٻਓதʂʳ
 ‣ CMPHIUUQTQIQUJQTDPN ‣ (JUIVC5XJUUFSOPKJNBHF

  3. ࿩͢͜ͱ ‣ 1)1ΞϓϦέʔγϣϯͷ։ൃ؀ڥ͋Ε͜Ε ‣ %PDLFSͱ͸ ‣ %PDLFS؀ڥͷߏஙํ๏ ‣ )FMMP1)1PO%PDLFS ‣

    %PDLFS$PNQPTFͰ։ൃ؀ڥΛͭ͘Δ
  4. PHP։ൃ؀ڥ͋Ε͜Ε

  5. डୗ։ൃͰͷPHP։ൃ؀ڥ ‣ डୗ։ൃʹ͓͍ͯ͸Ҋ݅͝ͱʹ࢖༻Ͱ͖Δ1)1όʔδϣ ϯ͕ҟͳΔ ‣ ։ൃϚγϯʹ͸Ҋ݅͝ͱʹຊ൪؀ڥʹԠͨ͡όʔδϣ ϯͷ։ൃ؀ڥΛ༻ҙ͢Δ

  6. डୗ։ൃͰͷPHP։ൃ؀ڥ ‣ ։ൃ؀ڥͷόʔδϣϯ͕ຊ൪؀ڥͷόʔδϣϯ͕ҟͳ Δͱ ݹ͍όʔδϣϯͰར༻Ͱ͖ͳ͍จ๏ؔ਺Λ࢖༻ͯ͠͠·͏ όʔδϣϯʹΑͬͯڍಈͷมΘͬͨؔ਺Λ࢖༻ͯ͠͠·͏ ʮ։ൃ؀ڥͰ͸ಈ͍͍ͯͨͷʹຊ൪؀ڥͰ͸ಈ͔ͳ͍ʯ

  7. डୗ։ൃͰͷPHP։ൃ؀ڥ ʮ։ൃ؀ڥͰ͸ಈ͍͍ͯͨͷʹຊ൪؀ڥͰ͸ಈ͔ͳ͍ʯ Λආ͚ΔͨΊʹ ຊ൪؀ڥʹ͍ۙ؀ڥΛ։ൃ؀ڥͱͯ͠ར༻͢Δ Ҋ݅͝ͱʹ։ൃ؀ڥΛ੾Γ෼͚ͯ࡞੒͢Δ ෳ਺ਓͰ։ൃ͢Δ৔߹͸ಉ͡։ൃ؀ڥΛఏڙ͢Δ

  8. ෳ਺όʔδϣϯͷ؀ڥΛ
 ߏங͢ΔͨΊͷπʔϧ

  9. phpenv + php-build ‣ ϩʔΧϧϚγϯ্ͷQIQόʔδϣϯͷ੾ସ͑ ‣ ϩʔΧϧͰDPNQPTFS΍QIQVOJUΛ࣮ߦ͢Δ࣌ʹ࢖༻ σΟϨΫτϦ͝ͱʹόʔδϣϯΛઃఆɾ੾ସ͑Ͱ͖Δ ‣ QIQCVJMEͰ೚ҙͷόʔδϣϯͷ1)1ΛΠϯετʔϧͰ͖Δ

    QIQCVJME͸ιʔείʔυ͔ΒϏϧυ͢ΔͷͰ͕͔͔࣌ؒΔ ґଘϥΠϒϥϦͷΞοϓσʔτͰϦϯΫ͕੾Εͨ৔߹ͳͲ͸ ࠶Ϗϧυʜ
  10. Vagrant / VirtualBox ‣ Ҋ݅ຖʹԾ૝ϚγϯΛߏஙͯ͠։ൃ؀ڥͱ͢Δ ຊ൪؀ڥͱ΄΅ಉҰͷ؀ڥΛߏஙՄೳ ‣ σΟϨΫτϦߏ੒ɺϛυϧ΢ΣΞΛؚΊͯಉҰԽͰ͖Δ Ծ૝ϚγϯͳͷͰىಈʹগʑ͕͔͔࣌ؒΔ Ծ૝Ϛγϯ͸04Λؙ͝ͱؚΉͷͰσΟεΫ༰ྔΛଟ͘ফඅ

    σΟεΫΠϝʔδ͕େ͖͍ͷͰ։ൃ؀ڥͷڞ༗͕͍ͨ΁Μ
  11. Docker ‣ Ҋ݅ຖʹEPDLFSNBDIJOFͰԾ૝ϚγϯΛߏங ‣ Ծ૝ϚγϯͷதͰ%PDLFSίϯςφΛىಈͯ͠։ൃ؀ڥΛߏங 7BHSBOUʹൺ΂04Λؚ·ͳ͍෼σΟεΫ༰ྔΛফඅ͠ͳ͍ ։ൃ؀ڥͷڞ༗͸%PDLFSΠϝʔδΛ഑෍͢Ε͹͍͍ ։ൃʹ࢖༻ͨ͠ίϯςφΛຊ൪؀ڥͰ࢖༻͢Δ͜ͱ΋Մೳ

  12. ͱ͸

  13. ‣ ίϯςφܕͷԾ૝؀ڥΛ࡞੒ɺ഑෍ɺ࣮ߦ͢ΔͨΊͷ ϓϥοτϑΥʔϜ ‣ -JOVYΧʔωϧ͕ݩʑ͍࣋ͬͯΔػೳΛ࢖ͬͯɺ࣮ߦ؀ ڥΛଞͷϓϩηε͔Βִ཭͠ɺͦͷதͰΞϓϦέʔγϣ ϯΛಈ࡞ͤ͞Δ ‣ ैདྷͷԾ૝Խٕज़ʢ)ZQFS7 7JSUVBM#PY౳ʣͱൺ΂Ϧ

    ιʔεͷফඅ΍Φʔόʔϔου͕গͳ͍
  14. ϋʔυ΢ΣΞ ϗετ04 Ծ૝Խιϑτ ήετ04 ϛυϧ΢ΣΞ ΞϓϦ ήετ04 ϛυϧ΢ΣΞ ΞϓϦ ϋʔυ΢ΣΞ

    ϗετ04 %PDLFS&OHJOF Ծ૝Ϛγϯ ίϯςφ ϛυϧ΢ΣΞ ΞϓϦ ϛυϧ΢ΣΞ ΞϓϦ Ծ૝Ϛγϯ ίϯςφ Ծ૝ϚγϯͱDockerͷҧ͍
  15. ༻ޠ ‣ %PDLFSίϯςφ
 %PDLFS&OHJOF্Ͱىಈ͞ΕΔ࣮ߦ؀ڥ
 %PDLFSΠϝʔδ͔Βίϐʔͯ͠ੜ੒͢Δ ‣ %PDLFSΠϝʔδ
 ίϯςφͷݩͱͳΔϑΝΠϧγεςϜɻಡΈࠐΈઐ༻ ‣ %PDLFSpMF


    ΠϝʔδΛ࡞੒͢ΔͨΊͷखॱΛॻ͍ͨϑΝΠϧ
  16. Dockerfile→Πϝʔδ→ίϯςφ %PDLFSpMF %PDLFS
 Πϝʔδ %PDLFS
 ίϯςφ EPDLFSCVJME EPDLFSSVO

  17. DockerΠϝʔδΛऔಘ͢Δ %PDLFS
 Πϝʔδ %PDLFS
 ίϯςφ EPDLFSQVMM EPDLFSSVO ΠϝʔδϨδετϦ

  18. Docker؀ڥͷߏங

  19. Windows/MacͰͷDocker؀ڥ ‣ %PDLFS%FTLUPQ %PDLFSGPS8JOEPXT.BD 
 IUUQTXXXEPDLFSDPNQSPEVDUTEPDLFSEFTLUPQ ‣%PDLFS.BDIJOF
 IUUQTEPDTEPDLFSDPNNBDIJOF

  20. Docker for Windows/Mac ͱ Docker Machine ͷҧ͍ ϋʔυ΢ΣΞ 8JOEPXTNBD04 )ZQFS7YIZWF

    ϋʔυ΢ΣΞ ϗετ04 %PDLFS&OHJOF %PDLFSGPS8JOEPXT.BD %PDLFS.BDIJOF ϛυϧ΢ΣΞ ΞϓϦ ϛυϧ΢ΣΞ ΞϓϦ Ծ૝Խιϑτ ήετ04 %PDLFS&OHJOF ϛυϧ΢ΣΞ ΞϓϦ ϛυϧ΢ΣΞ ΞϓϦ
  21. Docker Machineͷεεϝ Ҋ݅͝ͱʹԾ૝ϚγϯΛ࡞੒͢Ε͹ɺෳ਺Ҋ݅ͷίϯςφΛಉ࣌ʹ
 ্ཱͪ͛ͯ΋ϙʔτͷ͔ͿΓ͕ͳ͍ Ծ૝Ϛγϯຖʹ*1͕ৼΒΕΔͷͰIPTUT౳Ͱ໊લղܾ͕Մೳ Ծ૝Ϛγϯ෼ͷσΟεΫ༰ྔΛফඅͯ͠͠·͏ ϗετϚγϯʹϙʔτϚοϐϯά͢ΔΘ͚Ͱ͸ͳ͍ͷͰɺ
 ଞͷϚγϯ͔Β͸ΞΫηεͰ͖ͳ͍
 ˠϗετϚγϯʹ8FCαʔόΛཱ͔ͯͯͦ͜ΒϓϩΩγͰԾ૝Ϛγϯʹඈ͹͢ํ๏ͰΞΫηεՄೳ 

  22. DockerͷΠϯετʔϧ

  23. Docker DesktopΛΠϯετʔϧ ‣ IUUQTXXXEPDLFSDPNQSPEVDUTEPDLFSEFTLUPQ

  24. Docker DesktopΛΠϯετʔϧ

  25. Docker DesktopΛΠϯετʔϧ

  26. Docker DesktopΛΠϯετʔϧ ্2ͭͷνΣοΫϘοΫε͸ඞਢ ར༻ن໿ͱϓϥΠόγʔϙϦγʔ΁ͷಉҙ ొ࿥֬ೝͷϝʔϧ͕ඈΜͰ͘ΔͷͰ ϝʔϧ಺ͷϦϯΫΛΫϦοΫͯ͠ొ࿥׬ྃ

  27. Docker DesktopΛΠϯετʔϧ

  28. Docker DesktopΛΠϯετʔϧ

  29. Docker DesktopΛΠϯετʔϧ ‣ Πϯετʔϧޙɺίϯιʔϧ͔ΒEPDLFSWFSTJPO ͱଧͬͯىಈΛ֬ೝ $ docker version Client: Version:

    18.06.1-ce API version: 1.38 Go version: go1.10.3 Git commit: e68fc7a Built: Tue Aug 21 17:21:31 2018 OS/Arch: darwin/amd64 Experimental: false Server: Engine: Version: 18.06.1-ce API version: 1.38 (minimum version 1.12) Go version: go1.10.3 Git commit: e68fc7a Built: Tue Aug 21 17:29:02 2018 OS/Arch: linux/amd64 Experimental: true ClientͱServer྆ํͷόʔδϣϯ͕ දࣔ͞Ε͍ͯΕ͹OK
  30. Hello PHP on Docker

  31. PHPެࣜΠϝʔδ ‣ %PDLFS)VCIUUQTIVCEPDLFSDPN ‣ %PDLFSΠϝʔδͷϨϙδτϦ
 ‣ 1)1ެࣜͷΠϝʔδ
 IUUQTIVCEPDLFSDPN@QIQ ‣ d

    CFUB ·Ͱ͋Δ
  32. None
  33. PHPެࣜΠϝʔδ ‣ 1)1ͷΠϝʔδʹ͸৭ʑͳछྨ͕͋Δ ‣ DMJ ‣ GQN ‣ BQBDIF ‣

    DMJBMQJOF ‣ GQNBMQJOF ‣ DMJBMQJOF ࠷ॳͷ਺ࣈ͸1)1όʔδϣϯ DMJ$-* ίϚϯυϥΠϯ ൛ GQN1)1'1.൛ BQBDIF"QBDIFIUUQEಉࠝ൛ [UT;54ʢ;FOE5ISFBE4BGFUZʣରԠ൛ ௨ৗ͸%FCJBOϕʔε BMQJOF͕ͭ͘ͱΑΓܰྔͳ"MQJOF-JOVYϕʔε
  34. σϞ

  35. DockerΠϝʔδΛpull͢Δ ‣ ࠓճ͸BQBDIFΛ࢖༻ͯ͠ΈΔ ‣docker pull php:7.2-apache ‣ docker imagesͰखݩͷΠϝʔδͷϦετΛ֬ೝ

  36. hello.phpΛ༻ҙ͢Δ <?php $version = phpversion(); $sapi = PHP_SAPI; ?> <h1>Hello

    PHP <?= $version ?> on <?= $sapi ?></h1>
  37. PHPίϯςφΛىಈ͢Δ docker run \ --rm \ -p 8000:80 \ -v

    "$PWD":/var/www/html \ php:7.2-apache ˡίϯςφΛىಈ ˡίϯςφΛऴྃͨ͠Β࡟আ͢Δ ˡίϯςφͷϙʔτΛϗετͷʹׂΓ౰ͯ ˡݱࡏͷσΟϨΫτϦΛWBSXXXIUNMʹϚ΢ϯτ ˡQIQBQBDIFͷΠϝʔδ͔Β࣮ߦ IUUQMPDBMIPTUͰΞΫηεͰ͖Δ
  38. CLIͰPHPΛىಈ͢Δ docker run \ --rm \ -v "$PWD":/var/www/html \ php:7.2-apache

    \ php hello.php ˡίϯςφΛىಈ ˡίϯςφΛऴྃͨ͠Β࡟আ͢Δ ˡݱࡏͷσΟϨΫτϦΛWBSXXXIUNMʹϚ΢ϯτ ˡQIQBQBDIFͷΠϝʔδ͔Β࣮ߦ ˡQIQίϚϯυͰIFMMPQIQΛ࣮ߦ
  39. DockerͰ͓͖͍֮͑ͯͨίϚϯυ ‣ EPDLFSQVMM ‣ EPDLFSSVO ‣ EPDLFSQTEPDLFSQTB ‣ EPDLFSMPHT ‣

    EPDLFSJNBHFT
  40. ΧελϜPHPΠϝʔδΛ࡞੒

  41. ެࣜΠϝʔδͰ͸଍Γͳ͍ ‣ ެࣜΠϝʔδʹσϑΥϧτͰೖ͍ͬͯΔ1)1֦ுͰ͸ ࣮ࡍͷ։ൃ͸Ͱ͖ͳ͍ ‣ QEP@NZTRM΍(% 9EFCVHͳͲΑ͘࢖͏֦ு͸ೖͬ ͍ͯͳ͍ ඞཁͳ֦ுΛΠϯετʔϧͨ͠
 %PDLFSΠϝʔδΛ࡞Δ

  42. DockerfileͰ࢖͑Δαϙʔτπʔϧ ‣ 1)1ίΞʹؚ·ΕΔ֦ுΛΠϯετʔϧ͢Δ ‣ EPDLFSQIQFYUJOTUBMM ‣ EPDLFSQIQFYUDPOpHVSF
 ‣ QFDM֦ுΛΠϯετʔϧ͢Δ ‣

    EPDLFSQIQFYUFOBCMF
  43. GD֦ுΛΠϯετʔϧ FROM php:7.2-apache RUN apt-get update \ && apt-get install

    -y \ libfreetype6-dev \ libjpeg62-turbo-dev \ libpng-dev \ && docker-php-ext-configure gd \ --with-freetype-dir=/usr/include/ \ --with-jpeg-dir=/usr/include/ \ && docker-php-ext-install gd ˡQIQBQBDIFΛϕʔεʹ͢Δ ˡBQUύοέʔδΞοϓσʔτ ˡ(%ʹඞཁͳύοέʔδΛΠϯετʔϧ ˡ(%֦ுͷϏϧυΦϓγϣϯΛࢦఆ ˡ(%֦ுΛΠϯετʔϧ
  44. Docker ComposeͰ
 ։ൃ؀ڥΛߏங͢Δ

  45. Docker Composeͱ͸ ‣ ෳ਺ͷίϯςφ͔Β੒ΔαʔϏεͷߏ੒ɾؔ࿈Λ
 ϑΝΠϧͰఆٛ͠؅ཧΛ༰қʹ͢Δπʔϧ ‣ ֤αʔϏείϯςφͷఆٛͳͲهड़ͨ͠
 EPDLFSDPNQPTFZNMϑΝΠϧΛ࡞੒͢Δ ‣ EPDLFSDPNQPTFίϚϯυΛ࢖ͬͯαʔϏε؅ཧ

    EPDLFSίϚϯυͷΈͰෳ਺ίϯςφΛ ಈ͔͢ͷ͸ਏ͍ͷͰඞਢʂ
  46. WordPressΛDockerͰಈ͔͢ ‣ 1)1"QBDIFͱ.Z42-ͷ૊Έ߹Θͤ ‣ 1)1͸ΧελϜΠϝʔδɺ.Z42-͸ެࣜΠϝʔδΛ ࢖༻͢Δ ‣ ࠓճ͸%PDLFSGPS.BD্Ͱಈ͔͢ "QBDIF 1)1

    .Z42- 8PSE1SFTT 8000:80 13306:3306
  47. WordPressΛDockerͰಈ͔͢ ‣ 1)1ΧελϜΠϝʔδͷϙΠϯτ ‣ 8PSE1SFTT͸.Z42-֦ுPS.Z42-J֦ுΛ࢖ͬͯ .Z42-ͱ઀ଓ͢Δ ‣ ը૾αϜωΠϧͷ࡞੒ʹ(%·ͨ͸JNBHJDL͕ඞཁ ͜ΕΒͷ֦ு͸1)1ެࣜΠϝʔδʹ͸ ؚ·Εͳ͍ͷͰཁΠϯετʔϧ

  48. σϞ

  49. ·ͱΊ ։ൃ؀ڥ͸ͳΔ΂͘ຊ൪؀ڥͱಉ͡ʹ͠Α͏ ‣ νʔϜͰ։ൃ͢ΔͳΒ։ൃ؀ڥΛڞ༗Ͱ͖ΔΑ͏ʹ %PDLFS͸։ൃ؀ڥΛ࡞Δɾڞ༗͢Δͷʹศར ‣ͦͷ··ຊ൪؀ڥ΁Ͱར༻͢Δ͜ͱ΋Մೳ %PDLFS$PNQPTFΛ࢖ͬͯߏ੒Λఆٛ͠Α͏

  50. DockerͷυΩϡϝϯτ ‣%PDLFSެࣜ
 IUUQTEPDTEPDLFSDPN ‣༗ࢤͷํʹΑΔ೔ຊޠ༁
 IUUQEPDTEPDLFSKQ
 ˞Wϕʔεʢ೥લʣͷ΋ͷͳͷͰ஫ҙ

  51. ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠ Ξϯέʔτͷ͝ճ౴ΑΖ͓͘͠ئ͍͠·͢