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

Docker ハンズオン 基礎編2 / Docker Handson Basic 2

takasp
August 24, 2018
88

Docker ハンズオン 基礎編2 / Docker Handson Basic 2

takasp

August 24, 2018
Tweet

Transcript

  1. Dockerfile ͷ࡞੒ • FROM ໋ྩ • λά໊Λলུ͢Δͱ latest ͕࢖༻͞ΕΔɻ •

    ࣮ࡍʹར༻͢Δ࣌͸ɺͲͷΑ͏ͳόʔδϣϯ͕
 ಈ࡞͍ͯ͠Δ͔൑அͰ͖ͳ͍ͷͰɺlatest ͸࢖༻͠ͳ͍ɻ '30.Πϝʔδ໊<λά໊> ϕʔεΠϝʔδྫ '30.VCVOUV
  2. Dockerfile ΛϏϧυͯ͠ΈΑ͏ • sample:1.0 ͱ͍͏໊લͰࠓͷσΟϨΫτϦʹ
 ͋Δ Dockerfile ΛϏϧυ͠·͢ɻ • docker

    image ls ίϚϯυͰ֬ೝ͢Δͱ
 ϕʔεΠϝʔδͷ ubuntu:18.04 ͱ sample:1.0 ͷ Πϝʔδ͕Ͱ͖͍ͯΔ͜ͱ͕෼͔Δɻ EPDLFSJNBHFCVJMEUTBNQMF
  3. Dockerfile ͔ΒΠϝʔδ࡞੒ • docker image build ίϚϯυ • ҎԼͷΑ͏ͳ DockerfileΛ࡞੒ͯ͠Ϗϧυ͢Δɻ

    EPDLFSJNBHFCVJMEUΠϝʔδ໊<λά໊><%PDLFSpMFͷ৔ॴ> ϕʔεΠϝʔδͷઃఆ '30.VCVOUV
  4. • ໋ྩ͝ͱʹΠϝʔδ͕ੵΈॏͳΔɻ 4UFQ
 '30.໋ྩ 4UFQ
 '30.໋ྩ 4UFQ
 36/໋ྩ 4UFQ
 '30.໋ྩ

    4UFQ
 36/໋ྩ 4UFQ
 $01:໋ྩ 4UFQ
 $.%໋ྩ 4UFQ
 '30.໋ྩ 4UFQ
 36/໋ྩ 4UFQ
 $01:໋ྩ
  5. CMD ໋ྩͷ Exec ܗࣜ • γΣϧΛհͣ͞௚઀࣮ߦ͢Δɻ
 ͕ͨͬͯ͠ $HOME ͷΑ͏ͳ؀ڥม਺ΛࢦఆͰ͖ͳ͍ɻ •

    JSON ഑ྻͰࢦఆ͢Δɻ • ผͷγΣϧΛ࢖͍͍ͨͱ͖͸ɺγΣϧͷύεΛࢦఆͯ͠ɺ ࣮ߦ͍ͨ͠ίϚϯυΛࢦఆ͢Δɻ • ެࣜͰ͸ CMD ໋ྩ͸ Exec ܗ͕ࣜ๬·͍͠ͱ͞Ε͍ͯΔɻ
  6. CMD ໋ྩͷ Shell ܗࣜ • /bin/sh -c Λ࢖ͬͯίϚϯυΛ࣮ߦͨ͠ͱ͖ͱ ಉ͡ڍಈʹͳΔɻ •

    σϑΥϧτͷγΣϧΛมߋ͍ͨ͠ͱ͖͸ɺ SHELL ໋ྩΛ࢖͏ɻ OHJOYͷ$.%໋ྩͷ4IFMMܗࣜͷྫ $.%OHJOYH`EBFNPOP⒎`
  7. ENTRYPOINT ໋ྩ • ENTRYPOINT ໋ྩ͸ඞ࣮ͣߦ͞ΕΔ͕ɺ
 ࣮ߦ࣌ʹίϚϯυҾ਺Λࢦఆ͍ͨ͠ͱ͖ʹ
 CMD ໋ྩͱ૊Έ߹Θͤͯ࢖༻͢Δɻ • ENTRYPOINT

    ໋ྩͰ࣮ߦ͍ͨ͠ίϚϯυɺ
 CMD ໋ྩͰσϑΥϧτͷҾ਺Λఆ͓͖ٛͯ͠ɺ
 docker container run ίϚϯυͰڍಈΛม͍͑ͨ৔߹ʹར༻͢Δɻ • Ұ͚ͭͩఆٛͰ͖Δɻ
 ෳ਺ࢦఆͨ͠৔߹͸ɺ࠷ޙͷ ENTRYPOINT ͕༗ޮʹͳΔɻ
  8. ENV ໋ྩ • ؀ڥม਺Λઃఆ͍ͨ͠ͱ͖ʹ ENV ໋ྩΛ
 ࢖༻͢Δɻ &/7໋ྩͷྫ &/7)045IPTU &/7/".&EPDLFSNPCZ

    &/7500-,VCFSOFUFT"QBDIF.FTPT%PDLFS4XBSN &/71035QPSU &/7<LFZ><WBMVF> PS &/7<LFZ><WBMVF>
  9. USER ໋ྩ • RUN ໋ྩ΍ CMD ໋ྩΛ࣮ߦ͢ΔϢʔβʔΛࢦఆ͢Δɻ • USER ໋ྩͰࢦఆ͢ΔϢʔβʔ͸͋Β͔͡Ί

    RUN ໋ྩͰ
 ࡞੒͞Ε͍ͯΔඞཁ͕͋Δɻ • ϕʔεΠϝʔδͷσϑΥϧτ͸ root ϢʔβʔͰ͋Δ͜ͱ͕ଟ͘ɺ
 ϓϩηεͷ࣮ߦʹ root ݖݶ͕ඞཁͰͳ͍৔߹͸ɺ
 USER Λ੾Γସ͑ͨํ͕ྑ͍ɻ 64&3<Ϣʔβʔ໊6*%>
  10. RUN ໋ྩ͸·ͱΊΔ • ϨΠϠʔΛ૿΍͞ͳ͍ͨΊʹ RUN ໋ྩΛ·ͱΊΔɻ • ෳ਺ͷҾ਺Λӳ਺ࣈͰฒ΂ସ͑Δͱྑ͍ɻ • վߦ

    \ ΛೖΕΔͱՄಡੑ͕ߴ͘ɺอक͠΍͍͢ɻ
 
 
 
 36/໋ྩΛ·ͱΊͨྫ
 36/BQUHFUVQEBUFBQUHFUJOTUBMMZa C[Sa DWTa HJUa NFSDVSJBMa TVCWFSTJPO
  11. ϏϧυΩϟογϡΛ༗ޮ׆༻͢Δ • ΠϝʔδΛϏϧυ͢Δࡍʹ࠶ར༻Ͱ͖ΔΠϝʔδͷΩϟογϡ͕ଘࡏ͢Δ͔
 ֬ೝ͢Δɻ • جຊϧʔϧ • ਌Πϝʔδ͔Β೿ੜͨ͠ࢠΠϝʔδͱൺֱ͠ɺಉ໋͡ྩɺίϚϯυ͕
 ࢖༻͞Ε͍ͯΔ͔ൺֱ͢Δɻ •

    ADD ͱ COPY ໋ྩ͸ɺϑΝΠϧ͝ͱʹνΣοΫαϜ͕ܭࢉ͞Εɺ
 ΩϟογϡͷνΣοΫαϜΛൺֱ͢Δɻ • ίϯςφ಺ͷϑΝΠϧΛνΣοΫ͠ͳ͍ͷͰɺ
 ྫ͑͹ɺRUN apt-get -y update ίϚϯυͷΈ͕࣮ߦ͞Εͨ৔߹͸ɺ
 ίϚϯυจࣈྻࣗମΛൺֱ͢ΔͷͰɺΩϟογϡΛར༻ͯ͠͠·͏ɻ
  12. ϏϧυΩϟογϡͷ༗ޮ׆༻ྫ • ѱ͍ྫ͸ɺҰͭͰ΋ϩʔΧϧͷϑΝΠϧʹมߋ͕͋Δ࣌఺Ͱɺ
 ιʔεͷίϐʔʢCOPY ໋ྩʣͱ npm installʢRUN ໋ྩʣ͕࣮ߦ͞ΕΔɻ • ྑ͍ྫ͸ɺpackage.json

    ΛઌʹίϐʔʢCOPY ໋ྩʣͯ͠
 npm install ʢRUN ໋ྩʣΛ͢Δ͜ͱͰɺpackage.json ʹมߋ͕ͳ͍ݶΓɺ
 npm installʢRUN ໋ྩʣͷ࠶࣮ߦ͸ͤͣɺιʔεͷίϐʔʢCOPY ໋ྩʣͷΈ
 ࣮ߦ͢Δɻ
 
 
 
 
 ྑ͍%PDLFSpMFͷྫ
 '30.OPEF 803,%*3TSD $01:QBDLBHFKTPOTSDQBDLBHFKTPO
 36/OQNJOTUBMM $01:TSD ѱ͍%PDLFSpMFͷྫ
 '30.OPEF 803,%*3TSD $01:TSD
 36/OQNJOTUBMM
  13. ͦͷଞ • RUN ໋ྩ࣮ߦ࣌ͷ࠷ॳʹ set -eux Λ෇༩͢Δɻ • -e ͸Τϥʔ࣌ʹଈ࠲ʹऴྃ͠ɺ࣮ߦ಺༰͕දࣔ͞ΕΔɻ

    • -x ͸࣮ߦͨ͠ίϚϯυΛඪ४ग़ྗʹදࣔ͢Δɻ • -u ͸ະఆٛͷม਺͕ࢀর͞Εͨͱ͖ʹऴྃ͢Δɻ • ෆඞཁͳύοέʔδ͸Πϯετʔϧ͠ͳ͍ɻ • ෳࡶ͞ɺґଘؔ܎ɺϑΝΠϧαΠζɺϏϧυ࣌ؒΛݮΒͨ͢Ίɻ • .dockerignore ϑΝΠϧΛར༻͢Δɻ • Ϗϧυ࣌ʹෆඞཁͳϑΝΠϧΛআ֎͢Δɻ
  14. package.json Λ࡞੒͢Δ • ೚ҙͷσΟϨΫτϦΛ࡞੒͠ɺσΟϨΫτϦʹ
 ೖΓ·͢ɻ • σΟϨΫτϦ಺Ͱӈهͷ
 package.jsonΛ࡞੒͠·͢ɻ
 
 NLEJSOPEFKTEPDLFS


    DEOPEFKTEPDLFS \ OBNFEPDLFSOPEFKTIFMMP  WFSTJPO  EFTDSJQUJPO  BVUIPS  MJDFOTF*4$  EFQFOEFODJFT\ FYQSFTT ^ ^
  15. index.js Λ࡞੒͢Δ • ӈهͷ
 index.js Λ
 ࡞੒͠·͢ɻ VTFTUSJDU DPOTUFYQSFTTSFRVJSF FYQSFTT

     $POTUBOUT DPOTU1035 DPOTU)045 "QQ DPOTUBQQFYQSFTT  BQQHFU   SFR SFT \ SFTTFOE )FMMPXPSMEaO  ^  BQQMJTUFO 1035 )045  DPOTPMFMPH A3VOOJOHPOIUUQ\)045^\1035^A 
  16. Dockerfile Λ࡞੒͢Δ • ӈهͷ
 Dockerfile Λ
 ࡞੒͠·͢ɻ '30.VCVOUV 803,%*3TSD /PEFKTͱOQNΛΠϯετʔϧ

    36/BQUHFUVQEBUFBQUHFUJOTUBMMZa OPEFKTa OQN OQNJOTUBMM $01:QBDLBHFKTPOTSDQBDLBHFKTPO 36/OQNJOTUBMMQSPEVDUJPO ιʔεΛίϐʔ $01:TSD &9104& $.%<OPEF TSDJOEFYKT>
  17. Ϗϧυ͢Δ • docker image build ίϚϯυͰ
 Ϗϧυ͠·͢ɻ • docker container

    run ίϚϯυͰىಈ͠·͢ɻ EPDLFSJNBHFCVJMEUVCVOUVOPEFIFMMP EPDLFSDPOUBJOFSSVOOBNFOPEFIFMMPa
 QEVCVOUVOPEFIFMMP
  18. index.js Λमਖ਼͢Δ • index.js ͷ Hello world Λ Hello docker

    ʹ
 ॻ͖׵͑·͢ɻ "QQ DPOTUBQQFYQSFTT  BQQHFU   SFR SFT \ SFTTFOE )FMMPEPDLFSaO XPSMEΛEPDLFSʹॻ͖׵͑Δɻ ^ 
  19. docker-compose ϑΝΠϧͷ֓ཁ • YAML ܗࣜͰ؅ཧ͢Δɻ • ҎԼΛఆٛ͢Δɻ • αʔϏεʢservicesʣ •

    ωοτϫʔΫʢnetworksʣ • ϘϦϡʔϜʢvolumesʣ • ϑΝΠϧͷઌ಄ʹ version Λࢦఆ͢Δɻ
  20. docker-compose ϑΝΠϧͷ֓ཁ όʔδϣϯΛࢦఆ WFSTJPO αʔϏεΛఆٛ TFSWJDFT XFC CVJME QPSUT 

    OFUXPSLT XFC dӈʹଓ͘d dࠨͷଓ͖d EC JNBHFQPTUHSFT WPMVNFT ECEBUBWBSMJCQPTUHSFTRMEBUB OFUXPSLT XFC ωοτϫʔΫΛఆٛ OFUXPSLT XFC ϘϦϡʔϜΛఆٛ WPMVNFT ECEBUB
  21. Πϝʔδͷࢦఆʢimageʣ • ϕʔεΠϝʔδΛࢦఆ͢Δͱ͖͸ image Λ
 ࢖༻͢Δɻ • λά໊Λলུ͢Δͱ latest ͕࢖༻͞ΕΔ

    • ࣮ࡍʹར༻͢Δ࣌͸ɺͲͷΑ͏ͳόʔδϣϯ͕
 ಈ࡞͍ͯ͠Δ͔൑அͰ͖ͳ͍ͷͰɺ
 latest ͸࢖༻͠ͳ͍ɻ
  22. ίϯςφؒ௨৴ʢportsɾexposeʣ • ίϯςφ͕ϗετ OS ʹϙʔτΛެ։͢Δ৔߹͸ portsɺ ࿈ܞ͢Δίϯςφʹ͚ͩϙʔτΛެ։͢Δ৔߹͸
 expose Ͱࢦఆ͢Δɻ 


    
 QPSUT <ϗετ04ͷϙʔτ൪߸ίϯςφͷϙʔτ൪߸> ࿈ܞ͢ΔίϯςφʹͷΈϙʔτΛެ։͢Δ৔߹͸ɺFYQPTFΛࢦఆ͢Δ FYQPTF <ίϯςφͷϙʔτ൪߸> QPSUTɾFYQPTFΛࢦఆ͢Δྫ QPSUT  FYQPTF 
  23. ґଘؔ܎ͷఆٛʢdepends_onʣ • ෳ਺ͷίϯςφͷґଘؔ܎Λఆٛ͢Δͱ͖͸ɺ
 depends_on Λࢦఆ͢Δɻ • depends_on ͸ίϯςφͷ
 ։࢝ॱংΛఆٛ͢Δ͚ͩͰɺ
 ίϯςφ಺ͷΞϓϦ͕ىಈ͢Δ·Ͱ


    ଴੍ͭޚΛ͠ͳ͍ͷͰɺ
 ΞϓϦଆͰରԠ͢Δඞཁ͕͋Δɻ 
 ECʹґଘͨ͠XFCίϯςφΛ
 ఆٛ͢Δྫ TFSWJDFT XFC CVJME EFQFOET@PO EC EC JNBHFQPTUHSFT
  24. ίϯςφͷ֬ೝʢpsʣ • ෳ਺ͷίϯςφͷҰཡΛදࣔ͢Δʹ͸
 docker-compose ps ίϚϯυΛ࢖༻͢Δɻ • docker container ls

    Ͱ΋දࣔͰ͖Δɻ 
 
 
 
 ෳ਺ͷίϯςφΛදࣔ͢Δྫ EPDLFSDPNQPTFQT EPDLFSDPNQPTFQT<Φϓγϣϯ><αʔϏε໊>
  25. ίϯςφͷ֬ೝʢlogsʣ • docker-compose Ͱ؅ཧ͢ΔίϯςφͷϩάΛ
 ֬ೝ͢Δʹ͸ɺdocker-compose logs ίϚϯυΛ
 ࢖༻͢Δɻ • αʔϏε໊Λࢦఆ͢Ε͹αʔϏε໊ͷίϯςφ͚ͩදࣔͰ͖Δɻ

    
 GGPMMPXϩάग़ྗΛଓ͚Δɻ UBJMϩάͷ຤ඌΛදࣔ͢Δɻ EPDLFSDPNQPTFMPHT<Φϓγϣϯ><αʔϏε໊> XFCαʔϏεͷίϯςφͷϩάΛදࣔ͢Δྫ EPDLFSDPNQPTFMPHTXFC
  26. ෳ਺ίϯςφͷ࡟আʢrmʣ • ෳ਺ͷίϯςφΛ࡟আ͢Δͱ͖͸ɺ
 docker-compose rm ίϚϯυΛ࢖༻͢Δɻ • ಛఆͷαʔϏε໊ΛҾ਺ʹ౉͢ͱͦͷίϯςφͷΈ
 ࡟আͰ͖Δɻ 


    EPDLFSDPNQPTFSN<αʔϏε໊> GGPSDF֬ೝͤͣ࡟আ͢Δɻ XFCαʔϏεͷίϯςφΛ࡟আ͢Δྫ EPDLFSDPNQPTFSNXFC
  27. ෳ਺ϦιʔεͷҰׅ࡟আʢdownʣ • docker-compose up Ͱੜ੒ͨ͠ίϯςφ΍ΠϝʔδͳͲͷ
 ϦιʔεΛ·ͱΊͯ࡟আ͢Δͱ͖ʹ docker-comopse down
 ίϚϯυΛ࢖༻͢Δɻ •

    ࣮ߦதͷίϯςφΛఀࢭ͠ɺΠϝʔδɺωοτϫʔΫɺ
 ϘϦϡʔϜΛ࡟আ͢Δɻ 
 EPDLFSDPNQPTFEPXO<Φϓγϣϯ> SNJBMMશͯͷΠϝʔδΛ࡟আ ΠϝʔδؚΊશͯΛ࡟আ͢Δྫ EPDLFSDPNQPTFEPXOSNJBMM
  28. Dockerfile Λ࡞੒͢Δ • ೚ҙͷσΟϨΫτϦΛ࡞੒͠ɺσΟϨΫτϦʹ
 ೖΓ·͢ɻ • σΟϨΫτϦ಺Ͱ
 ӈهͷ Dockerfile Λ


    ࡞੒͠·͢ɻ
 NLEJSEKBOHPEPDLFSDPNQPTF DEEKBOHPEPDLFSDPNQPTF '30.QZUIPO &/71:5)0/6/#6''&3&% 803,%*3DPEF $01:SFRVJSFNFOUTUYUDPEF 36/QJQJOTUBMMSSFRVJSFNFOUTUYU $01:DPEF
  29. docker-compose.yml • Լهͷ docker-compose.yml Λ࡞੒͠·͢ɻ WFSTJPO TFSWJDFT EC JNBHFQPTUHSFT XFC

    CVJME DPNNBOEQZUIPONBOBHFQZSVOTFSWFS WPMVNFT DPEF QPSUT  EFQFOET@PO EC
  30. DjangoϓϩδΣΫτΛ࡞੒͢Δ • ҎԼͷ docker-compose run ίϚϯυͰ
 Django ϓϩδΣΫτΛ࡞੒͠·͢ɻ • ϑΝΠϧҰཡΛදࣔ͢Δͱ

    Django ϓϩδΣΫτ͕
 ੜ੒͞Ε͍ͯΔ͜ͱ͕෼͔Δɻ
 
 
 
 EPDLFSDPNQPTFSVOXFCa EKBOHPBENJOQZTUBSUQSPKFDUDPNQPTFFYBNQMF MTM
  31. docker-compose Λىಈͯ͠
 ϒϥ΢βͰ֬ೝ͢Δ • docker-compose up ίϚϯυͰىಈ͠·͢ɻ • ϒϥ΢β͔Β http://localhost:8000/

    ʹΞΫηεͯ͠ τοϓը໘͕දࣔ͞ΕͨΒಈ࡞֬ೝ׬ྃͰ͢ɻ
 
 EPDLFSDPNQPTFVQE
  32. ·ͱΊ • Dockerfile ͰඞཁͳϦιʔεΛఆٛͰ͖Δɻ • docker-compose Ͱෳ਺ͷίϯςφΛ
 γϯϓϧͳίϚϯυͰѻ͑Δɻ • Dockerfile

    ͷ࡞੒ํ๏Λֶͼɺ
 ࡞੒ɺϏϧυͯ͠ಈ͔ͯ͠Έͨɻ • docker-compose ͷ࡞੒ํ๏Λֶͼɺ
 ࡞੒ɺϏϧυͯ͠ಈ͔ͯ͠Έͨɻ
  33. ࢀߟจݙ • ϓϩάϥϚͷͨΊͷDockerڭՊॻ ୈ2൛ Πϯϑϥͷجૅ஌ࣝ&ίʔυʹΑΔ؀ڥߏஙͷࣗಈԽ • ίϯςφɾϕʔεɾΦʔέετϨʔγϣϯ Docker/KubernetesͰ࡞ΔΫϥ΢υ࣌୅ͷγεςϜج ൫ •

    https://docs.docker.com/ • https://qiita.com/pottava/items/452bf80e334bc1fee69a • https://qiita.com/dskshmz/items/c01f29f8398cc7f5c396 • https://qiita.com/yugui/items/517480f4383b893e092e • https://qiita.com/yugui/items/517480f4383b893e092e • https://nodejs.org/en/docs/guides/nodejs-docker-webapp/ • http://docs.docker.jp/engine/examples/nodejs_web_app.html