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

Docker3兄弟

C887161d922da14c9cbe957ef4b4a6fe?s=47 Tsuyoshi Torii
August 22, 2015
5.5k

 Docker3兄弟

Docker machine / Docker compose / Docker swarm の話をしました

C887161d922da14c9cbe957ef4b4a6fe?s=128

Tsuyoshi Torii

August 22, 2015
Tweet

Transcript

  1. %PDLFSܑ̏ఋ ʙɹܑͦͯ̐͠ఋ΁ɹʙ :"1$"TJB5PLZP !UPSJUPSJ

  2. 1SPpMF w ௗډɹ߶࢘ʢ!UPSJUPSJʣ w גࣜձࣾ)"30J% w IUUQXXXIBSPJEDPN w αʔόαΠυΤϯδχΞ w

    -VB(PMBOH1ZUIPO/PEFKT1FSM3VCZ w ೋࣇͷ෕
  3. ΞδΣϯμ w %PDLFSܑ̏ఋͱ͸ʁ w %PDLFSܑ̏ఋͷ͸͡Ί͔ͨ w %PDLFSܑ̏ఋͷ͝঺հ w Ϣʔεέʔε w

    %PDLFSܑ̏ఋΛศརʹ࢖͏ w %PDLFSܑ̏ఋͷ͜Ε͔Β w ·ͱΊ w ͓·͚ɿ%PDLFS$PNQPTFΞοϓσʔτ
  4. %PDLFSܑ̏ఋͱ͸

  5. %PDLFSܑఋͱ͸ w %PDLFS.BDIJOFʢ%PDLFSϗετߏஙπʔϧʣ w %PDLFS$PNQPTFʢෳ਺ίϯςφఆٛʗߏஙπʔϧʣ w %PDLFS4XBSNʢ%PDLFSΫϥελϦϯάπʔϧʣ

  6. %PDLFSܑ̏ఋͷ ͸͡Ί͔ͨ

  7. %PDLFS5PPMCPY

  8. %PDLFS5PPMCPY w %PDLFSΫϥΠΞϯτ w %PDLFS.BDIJOF w %PDLFS$PNQPTFʢ.BDͷΈʣ w %PDLFS,JUFNBUJDʢ%PDLFSίϯςφ(6*ʣ w

    7JSUVBM#PY ˞طଘͷCPPUEPDLFSϚΠάϨʔγϣϯ΋ͯ͘͠ΕΔ
  9. ʢ˞LJUFNBUJDಈ࡞σϞʣ

  10. %PDLFSܑ̏ఋͷ঺հ

  11. ˞ͷલʹॾ஫ҙ

  12. ʊਓਓਓਓਓਓਓਓਓਓਓਓਓਓਓʊ ʼɹܑ̏ఋશͯϕʔλόʔδϣϯɹʻ ʉ:?:?:?:?:?:?:?:?:?:?:?:?:?:

  13. %PDLFS.BDIJOF

  14. %PDLFS.BDIJOF w ೚ҙͷϓϩόΠμʢ7JSUVBMCPY"84 %JHJUBM0DFBOFUDʣ্ʹ͋Δ%PDLFSαʔόΛ ૢ࡞͢ΔͨΊͷπʔϧ w 7.ىಈʗఀࢭʗ࠶ىಈʗҰཡදࣔ w 7.্ͷ%PDLFSϗετͱͷ΍ΓͱΓ w

    CPPUEPDLFSͷ୅ସ Ћͱͯ͠࢖͑Δ
  15. 4VQQPSU%SJWFST w "NB[PO8FC4FSWJDFT w .JDSPTPGU"[VSF w %JHJUBM0DFBO w &YPTDBMF w

    (PPHMF$PNQVUF&OHJOF w (FOFSJD w .JDSPTPGU)ZQFS7 IUUQTEPDTEPDLFSDPNNBDIJOFESJWFST w 0QFO4UBDL w 3BDLTQBDF w *#.4PGUMBZFS w 0SBDMF7JSUVBM#PY w 7.XBSFW$MPVE"JS w 7.XBSF'VTJPO w 7.XBSFW4QIFSF
  16. %PDLFS.BDIJOF2VJDL4UBSU # ৽ن࡞੒ docker-machine create … # Ϛγϯ੾Γସ͑ʗදࣔͳͲ docker-machine env

    <name> docker-machine ls # VMૢ࡞ܥ docker-machine start <name> docker-machine stop <name> docker-machine restart <name> # ssh / scp docker-machine ssh <name> docker-machine scp <src> <dest> # VM࡟আ docker-machine rm <name>
  17. MT % docker-machine ls NAME ACTIVE DRIVER STATE URL default

    virtualbox Running tcp://xxx:2376 ec2vm amazonec2 Running tcp://xxx:2376 dovm * digitalocian Running tcp://xxx:2376
  18. %PDLFS$PNQPTF

  19. %PDLFS$PNQPTF w ෳ਺ͷ%PDLFSίϯςφ͔Βߏ੒͞ΕΔ̍ͭͷαʔ ϏεΛ࣮ߦ͍ͨ͠৔߹ɺ:".-ϑΝΠϧͰఆٛ͢ Δ͚ͩͰҰܸͰߏஙʗ؅ཧ͢Δ͜ͱ͕Ͱ͖Δπʔ ϧ w ىಈʗఀࢭʗαʔϏε࠶ߏஙʗεςʔλεҰ ཡදࣔʗϩάදࣔʗ౎౓࣮ߦ w

    ݩʑ͸pHͱ͍͏໊લͰެ։͞Ε͍ͯͨ
  20. %PDLFS$PNQPTF2VJDL4UBSU # ίϯςφىಈ docker-compose up … # ίϯςφεέʔϧ docker-compose scale

    <name>=<num> # େମDockerίϚϯυͱҰॹ docker-compose ps docker-compose build docker-compose pull docker-compose logs docker-compose run docker-compose start docker-compose stop docker-compose restart docker-compose kill docker-compose rm
  21. 4BNQMF:".- webapp: build: . ports: - "5000:5000" volumes: - .:/code

    links: - redis redis: image: redis ΄ͱΜͲͷ%PDLFSΦϓγϣϯʹରԠ͍ͯ͠Δ
  22. :".-ิ଍ webapp: build: ./webapp environment: - DEBUG=false - SEND_EMAILS=false web:

    extends: file: common.yml service: webapp ports: - "8000:8000" links: - db environment: - DEBUG=true db: image: postgres • extends • ֎෦ͷdocker-compose.ymlΛར༻͢Δ͜ͱ͕Ͱ͖Δ DPNNPOZNM EPDLFSDPNQPTFZNM
  23. :".-ิ଍ external_links: - redis_1 - project_db_1:mysql • external_links • docker-compose؅ཧ֎ͷίϯςφͱϦϯΫ͢Δ

  24. %PDLFS4XBSN

  25. %PDLFS4XBSN w %PDLFSϗετͷू߹Λந৅Խ͠ɺҰͭͷ%PDLFS Τϯδϯͱ࣮ͯ͠ߦ͢ΔͨΊͷπʔϧ w εέδϡʔϥʗαʔϏεσΟεΧόϦ w %PDLFS.BDIJOFͱ࿈ܞ

  26. 4XBSN$MVTUFS 4XBSN .BTUFS 4XBSN/PEF $POUBJO FS 4XBSN/PEF %JTDPWFSZ 4FSWJDF $POUBJO

    FS $POUBJO FS DMJFOU
  27. %JTDPWFSZ w ͍͔ͭ͘ͷσΟεΧόϦɾόοΫΤϯυͱ࿈ܞ w )PTUFE%JTDPWFSZXJUI%PDLFS)VC w Ϋϥελ৘ใ͕هड़͞Εͨ੩తϑΝΠϧ w FUDE w

    DPOTVM w [PPLFFQFS w *1ΞυϨεϦετࢦఆ w *1ΞυϨεൣғࢦఆ IUUQTEPDTEPDLFSDPNTXBSNEJTDPWFSZ
  28. 'JMUFST w εέδϡʔϦϯάͷͨΊͷ͍͔ͭ͘ͷϑΟϧλΛఏڙ͍ͯ͠Δ w $POTUSBJOUϑΟϧλ w "⒏OJUZϑΟϧλ w ஥ྑ͠άϧʔϓʹΑΔϑΟϧλʢίϯςφʗΠϝʔδʗϥ ϕϧʣ

    w 1PSUϑΟϧλ w %FQFOEFODZϑΟϧλ w ಉҰϊʔυ্ͰͷґଘΛεέδϡʔϦϯάʢWPMVNFTGSPN ʗMJOLʗOFUʣ w )FBMUIϑΟϧλ IUUQTEPDTEPDLFSDPNTXBSNTDIFEVMFSpMUFS
  29. 4USBUFHJFT w 4XBSN͸͍͔ͭ͘ͷεέδϡʔϦϯάํ਑Λ͍࣋ͬͯΔ w TQSFBE EFGBVMU  w Ͱ͖Δ͚ͩ෼ࢄ͢Δ w

    CJOQBDL w Ͱ͖Δ͚ͩू໿͢Δ w SBNEPN w ϥϯμϜ IUUQTEPDTEPDLFSDPNTXBSNTDIFEVMFSTUSBUFHZ
  30. ܑ̏ఋͷྗΛ ू݁ͯ͠ΈΑ͏

  31. ܑ̏ఋͷྗΛू݁ͯ͠ΈΑ͏ w ʢࣄલ४උʣ w TXBSNUPLFOΛੜ੒͓ͯ͘͠ w EPDLFSNBDIJOFͰTXBSNϚελʔ༻ʗ TXBSNϊʔυ༻Y̎ͷ7.Λىಈ w EPDLFSDPNQPTFͰVQ΍TDBMFΛ࣮ߦͯ͠ΈΔ

  32. τʔΫϯੜ੒ʗ7.ىಈ # create token docker run --rm swarm create #

    create vm # master docker-machine create -d … \ --swarm --swarm-discovery token://$SWARM_TOKEN \ --swarm-master \ vm-swarm-master # node docker-machine create -d … \ --swarm --swarm-discovery token://$SWARM_TOKEN \ vm-swarm-nodex # env ઃఆ eval "$(docker-machine env --swarm vm-swarm-master)"
  33. EPDLFSDPNQPTFZNM web: image: toritori0318/ubuntu-perl-dancer-demoapp ports: - "5000" environment: - REDIS_HOST

    redis: image: redis ports: - "6379:6379" environment: - constraint:node==vm-swarm-master ˞ݱ࣌఺͸MJOL͕ͦͷ··࢖͑ͳ͍ʜ
  34. %BODFS"QQ package MyWeb::App; use Dancer2; use Redis; our $VERSION =

    '0.1'; my $redis_host = $ENV{REDIS_HOST} || 'localhost'; my $redis_port = $ENV{REDIS_PORT} || '6379'; my $redis = Redis->new(server => "${redis_host}:${redis_port}"); get '/' => sub { $redis->incr('hits'); return sprintf("Hello Dancer! I have been seen %s times.", $redis->get('hits')); }; true;
  35. ˞EPDLFSNBDIJOF EPDLFSDPNQPTFEPDLFS TXBSNಈ࡞σϞ

  36. Ϣʔεέʔε

  37. ̍ɽ։ൃ؀ڥ഑෍ w ։ൃऀ͸ɺϦϙδτϦΛ$MPOF͢Δ͚ͩͰΞϓϦέʔγϣ ϯΛ։ൃ͢Δ؀ڥΛߏஙͰ͖Δ w EPDLFSNBDIJOF EPDLFSDPNQPTF w खॱ w

    ʢલఏʣΞϓϦέʔγϣϯϦϙδτϦʹEPDLFS DPNQPTFZNM ЋΛؚΊ͓ͯ͘ w ར༻ऀ͸ϦϙδτϦΛDMPOFͯ͠EPDLFSDPNQPTFVQ w αʔόىಈ΍Ϣχοτςετ͕ͦͷ··ಈ͘
  38. ̎ɽ$*؀ڥ w EPDLFSDPNQPTF؀ڥΛͦͷ··$*Ͱಈ࡞ʗςετΛߦ͏ w EPDLFSDPNQPTF w खॱ w ʢલఏ৚݅ʣέʔε̍ͷ؀ڥΛ༻ҙ͓ͯ͘͠ w

    $*ͷதͰҎԼͷ༷ͳίϚϯυΛ࣮ߦ͢Δ w EPDLFSDPNQPTFCVJMEʜVQʜSVONBLFUFTUʜ w 3FG%SPOF pHͰ$*ςετͯ͠ΈΔ w IUUQRJJUBDPNUPSJUPSJJUFNT GDEBEE
  39. ̏ɽϚϧνΫϥ΢υΫϥελ w %PDLFS4XBSNͰ؆୯ϚϧνΫϥ΢υΫϥελʂʁʢະདྷͷ࿩ ʴໝ૝ؚΉʣ w %PDLFS.BDIJOF%PDLFS$PNQPTF%PDLFS4XBSN w खॱ w %PDLFS.BDIJOFͰ֤Ϋϥ΢υʹ7.ཱͯΔ

    w %PDLFS4XBSNͰΫϥελϦϯά w %PDLFS$PNQPTFͰίϯςφ഑ஔ w %/4Ͱ֤Ϋϥ΢υ؀ڥʹϧʔςΟϯά w σʔλετΞ͸3JBL΍$BTTBOESB
  40. 4XBSN$MVTUFS "[VSF ($& "84 4XBSN .BTUFS 4XBSN/PEF $POUBJOFS 4XBSN/PEF $POUBJOFS

    4XBSN/PEF $POUBJOFS DMJFOU $POUBJOFS $POUBJOFS $POUBJOFS 4XBSN .BTUFS 4XBSN .BTUFS
  41. %PDLFSܑ̏ఋΛศརʹ࢖͏ ͨΊͷ͍Ζ͍Ζ

  42. "MJBT # docker-machine cli alias dm="docker-machine" dme() { command docker-machine

    $1 ${DOCKER_MACHINE_NAME};} # RPrompt get_docker_machine_name() { if [ "$DOCKER_MACHINE_NAME" != "" ]; then echo "(dm=$DOCKER_MACHINE_NAME)" else echo "" fi } RPROMPT=‘%{$fg[green]%}$(get_docker_machine_name)$ \ (git_prompt_info)%{$reset_color%}% '
  43. "MJBT # fig sudo ln -s /usr/loca/bin/docker-compose \ /usr/local/bin/fig #

    fig recreate fig-recreate() { command \ fig stop -t 1 && fig rm —force \ && fig build && fig up -d; }
  44. QFDP # docker-machine env function peco-docker-machine-change() { # docker-machine ls

    + eval local dm_name=$(docker-machine ls | awk '{print $1}' | grep -vw 'NAME' | peco --query "$LBUFFER") if [ -n "$dm_name" ]; then eval "$(docker-machine env ${dm_name})" fi zle reset-prompt } zle -N peco-docker-machine-change bindkey '^x^m' peco-docker-machine-change # docker-machine ssh function peco-docker-machine-ssh() { BUFFER="docker-machine ls | awk '{print \$1}' | grep -vw 'NAME' | peco | awk '{print \$1}' | xargs -o -n 1 docker-mac hine ssh" zle accept-line } zle -N peco-docker-machine-ssh bindkey '^x^s' peco-docker-machine-ssh
  45. [FNCVUTV͞Μ w %PDLFSʗ)BTIJDPSQͳͲͷ৘ใ΍຋༁Λެ։͞Ε͍ͯΔ w %PDLFS.BDIJOFυΩϡϝϯτࢀߟ೔ຊޠ༁ w IUUQRJJUBDPN[FNCVUTVJUFNTEEBEDB w %PDLFS$PNQPTFυΩϡϝϯτࢀߟ೔ຊޠ༁ɾ໨࣍ w

    IUUQRJJUBDPN[FNCVUTVJUFNTDDEBCB w %PDLFS4XBSNυΩϡϝϯτࢀߟ೔ຊޠ༁ɾ໨࣍ w IUUQRJJUBDPN[FNCVUTVJUFNTBGBEEFF
  46. %PDLFSܑ̏ఋͷ͜Ε͔Β

  47. %PDLFS.BDIJOF3PBENBQ w ΰʔϧɿຊ൪؀ڥͰͷ؅ཧʗϓϩϏδϣχϯά Λ໰୊ͳ͘ಈ࡞ͤ͞Δ͜ͱ w $4ΞʔΩςΫνϟ w TXBSN౷߹ w ॊೈͳϓϩϏδϣχϯά

  48. %PDLFS$PNQPTF3PBENBQ w ຊ൪؀ڥʹ޲͚ͯ w VQ࣌ͷվળ w θϩμ΢ϯλΠϜʗࣦഊ࣌ͷϩʔϧόοΫ w ։ൃ؀ڥͱͯ͠ΑΓΑ͍ػೳ w

    XBUDIػೳ w TXBSNͱͷ౷߹
  49. %PDLFS$PNQPTF3PBENBQ include: # link using http servicea: http: http://localhost:8080/projects/servicea/fig.yml #

    local paths serviceb: path: ../projects/serviceb/fig.yml webapp: #... links: - servicea_webapp - serviceb_webapp w ෳ਺νʔϜ࿈ܞΛߟྀ
  50. %PDLFS4XBSN3PBENBQ w ૊ΈࠐΈεέδϡʔϥͷվળ w Ծ૝ίϯςφ*%ʗϗετো֐࣌Ϧόϥϯεʗάϩʔόϧεέδϡʔϥɹɹɹ w 4XBSNϚελʔϦʔμʔબग़ w "1*Ϛονϯά w

    ϚϧνϗετωοτϫʔΫ w ֦ுੑͷߟྀ w εέδϡʔϥͷϓϥάΠϯԽ w NFTPTLVCFSOFUFT
  51. ϩʔυϚοϓ·ͱΊ w શମతʹ͸ຊ൪Ͱ໰୊ͳ͘ಈ࡞͢Δ͜ͱΛ໨ࢦ͍ͯ͠Δ w ࣌ظ͸ʜʁ w NBDIJOFDPNQPTF͸TXBSN౷߹ͷॆ࣮ w ಛʹDPNQPTFपΓͱͷ࿈ܞ͕ڧԽ͞ΕΔͱָͦ͠͏ w

    NBDIJOFTXBSN͸ΑΓϓϥΨϒϧʹ
  52. ·ͱΊ

  53. ·ͱΊ w %PDLFSܑ̏ఋΛ࢖͏ͱ%PDLFSΛΏΔ;Θʹར༻͢Δ͜ͱ͕Ͱ͖Δ w ౷Ұ͞ΕͨΠϯλϑΣʔεʗϙʔλϒϧʗॊೈ w ։ൃ؀ڥͱͯ͠ͷར༻ w े෼ར༻Մೳ w

    ຊ൪؀ڥͱͯ͠ͷར༻ w ·ͩ·ͩ՝୊͸ଟ͍͕ɺ͖ͪΜͱϩʔυϚοϓ͕ఏࣔ͞Ε͍ͯΔ w %PDLFSܑ̏ఋʹ͸ɺເ͕͋Δοʂʂʂ
  54. ·ͱΊ w %PDLFSܑ̏ఋΛ࢖͏ͱ%PDLFSΛΏΔ;Θʹར༻͢Δ͜ͱ͕Ͱ͖Δ w ౷Ұ͞ΕͨΠϯλϑΣʔεʗϙʔλϒϧʗॊೈ w ։ൃ؀ڥͱͯ͠ͷར༻ w े෼ར༻Մೳ w

    ຊ൪؀ڥͱͯ͠ͷར༻ w ·ͩ·ͩ՝୊͸ଟ͍͕ɺ͖ͪΜͱϩʔυϚοϓ͕ఏࣔ͞Ε͍ͯΔ w %PDLFSܑ̏ఋʹ͸ɺເ͕͋Δοʂʂʂ ܑ̐ఋ
  55. ͓·͚ɿ%PDLFS$PNQPTFิ଍ w Ͱ࣮૷͞ΕͨػೳʢҰ෦ʣ w EPDLFSDPNQPTFVQͷվળ w EPDLFSDPNQPTFTUPQLJMMSNTDBMF͕ฒྻ࣮ߦ͢ΔΑ͏ʹͳͬͨ w DPOUBJOFS@OBNFͰίϯςφ໊ΛΧελϚΠζͰ͖ΔΑ͏ʹͳͬͨ w

    αʔϏε໊ʹʮʯʮʯʮ@ʯΛؚΊΕΒΕΔΑ͏ʹͳͬͨ w MPH@ESJWFSͰ೚ҙͷϩάυϥΠόΛར༻Ͱ͖ΔΑ͏ʹͳͬͨ w ඪ४ೖྗ͔Βߏ੒ΛಈతߏஙͰ͖ΔΑ͏ʹͳͬͨ w FDIPSFEJT\JNBHFSFEJT^cEPDLFSDPNQPTFpMFVQ
  56. ͓·͚ɿ%PDLFS$PNQPTFิ଍ w ʢ͓ͦΒ͘ʣͰ؀ڥม਺पΓͷվળ͕ߦΘΕΔʂ w IUUQTHJUIVCDPNEPDLFSDPNQPTFQVMM w γΣϧ؀ڥม਺Λͦͷ··:".-ϑΝΠϧ಺Ͱهड़͢Δ͜ͱ ͕Ͱ͖ΔΑ͏ʹͳΔ $ export

    POSTGRES_VERSION=9.3 db: image: "postgres:${POSTGRES_VERSION}"
  57. એ఻

  58. એ఻ גࣜձࣾ)"30J%Ͱ͸ ΤϯδχΞΛืू͍ͯ͠·͢ʂ

  59. ͝ਗ਼ௌ͋Γ͕ͱ͏͟͝ ͍·ͨ͠