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

Docker を使ったサービス構築速習会

Docker を使ったサービス構築速習会

Docker を使ったサービス構築速習会 @ Wantedly #docker_wantedly の発表資料です
http://wantedly.connpass.com/event/27061/

Daisuke Fujita

April 12, 2016
Tweet

More Decks by Daisuke Fujita

Other Decks in Technology

Transcript

  1. %PDLFSΛ࢖ͬͨ
    αʔϏεߏங
    ଎शձ!8"/5&%-:

    EPDLFS@XBOUFEMZ
    %BJTVLF'VKJUB!EUBO
    ʢެ։൛ʣ

    View full-size slide

  2. Daisuke Fujita
    @dtan4
    Πϯλʔϯ

    @ΠϯϑϥνʔϜ

    View full-size slide

  3. ࣭໰౳͸4ZODͷ෦԰ʹͯड͚෇͚·͢
    ʢຊ೔ΑΓʣ.BD"QQ4UPSF͔Βμ΢ϯϩʔυ΋Ͱ͖·͢ʂ
    CHECK!
    http://s.wantedly.com/1SL5Um0

    View full-size slide

  4. %PDLFSೖͬͯ·͔͢ʁಈ͖·͔͢ʁ
    CHECK!
    $ docker -v
    Docker version 1.10.0, build 590d510
    $ docker ps
    CONTAINER ID …
    $ docker-compose -v
    docker-compose version 1.6.0, build unknown
    ʢόʔδϣϯ͸ಛʹؾʹ͠ͳ͍Ͱ͢ɻ΋ͪΖΜ࠷৽Ͱ͋Δ΄ͲΑ͍ʣ

    View full-size slide

  5. ຊ೔ͷ಺༰
    w 8BOUFEMZͰͷ։ൃʹ͓͚Δॾ໰୊
    w %PDLFS$PNQPTFͰαʔϏεΛಈ͔ͦ͏
    w %PDLFS$PNQPTFͰߏஙͨ͠ΞϓϦΛ

    σϓϩΠ͠Α͏
    w %PDLFS1BB4*BB4

    View full-size slide

  6. Wantedly Ͱͷ։ൃʹ

    ͓͚Δॾ໰୊

    View full-size slide

  7. www.wantedly.com Λ

    ࢧ͑Δٕज़
    w 3BJMTͷ΄͔ʹʜ
    w 1PTUHSF42-
    w &MBTUJDTFBSDI
    w .FNDBDIFE
    w 3FEJT
    w 1IBOUPN+4

    w ։ൃɾςετʹ͸͜ΕΒͷαʔϏε͕ඞཁ

    View full-size slide

  8. www.wantedly.com Λ

    ࢧ͑Δٕज़
    w )PNFCSFXʹΑΔ։ൃ؀ڥߏங
    w brew updateͰ։ൃ؀ڥ͕յΕΔ
    w &MBTUJDTFBSDI͸ຊମͱϓϥάΠϯͷ

    όʔδϣϯΛγϏΞʹ߹Θͤͳ͍ͱμϝ
    w ຊ൪ͷ&MBTUJDBDIF͸3FEJTYͷΈ
    w ΠϯετʔϧεΫϦϓτͰແཧ΍Γ

    όʔδϣϯΛݻఆʜ
    w bundle install͕ࣦഊʢಛʹ$֦ுHFNʣ

    View full-size slide

  9. Elasticsearch on Docker

    View full-size slide

  10. Elasticsearch on Docker
    w ຊ൪ͷ&MBTUJDTFBSDI͸%PDLFSίϯςφͱ
    ͯ͠Քಇ͍ͯ͠Δ
    w RVBZJP্ͰΠϝʔδ؅ཧ
    w αʔϏεʹ߹Θͤͨνϡʔχϯά
    w ಠࣗࣙॻΛಉࠝ
    w ͜ΕΛ։ൃ΍ςετͰ࢖͍͍ͨʜ

    View full-size slide

  11. ϑΟʔνϟϒϥϯνͷ

    σϓϩΠ
    w ຊ൪؀ڥ 1SPEVDUJPO
    2"؀ڥ 2"

    w 2"ॱ൪଴ͪ໰୊
    w 2"ͭͩͱ଍Γͳ͘ͳ͖ͬͯͨ
    w ࣗ෼ͷϒϥϯνΛؾܰʹσϓϩΠͰ͖Δ

    ؀ڥ͕΄͍͠

    View full-size slide

  12. ϚΠΫϩαʔϏεج൫
    w ੲ͔Β3BJMTͰ࣮૷͞Ε͍ͯͨػೳʹ͍ͭͯɺ
    ύϑΥʔϚϯε޲্ͷͨΊʹ(PͰ࠶࣮૷

    ͢Δྫ͕͋Δ
    w 4ZOD΍8BOUFEMZ5PPMTͷΑ͏ʹɺຊମͱ
    ͸ҟͳΔαʔϏεΛϦϦʔε͢Δػձ͕

    ૿͑ͨ
    w ͜Μͳͱ͖ɺ޻਺Λ͔͚ͯΠϯϑϥΛ༻ҙ͢
    ΔͷͰ͸ͳ͘DockerfileΛॻ͚͹σϓϩΠͰ
    ͖ΔΑ͏ͳ؀ڥ͕΄͍͠

    View full-size slide

  13. Docker Ͱ؀ڥߏங
    w ΞϓϦ୯ҐͰ؀ڥΛಠཱͤ͞ΒΕΔ
    w ͜ͷαʔϏεͰ͸&Tɺ͋ͷαʔϏεͰ
    ͸&TʜͰ΋ॊೈʹରԠՄೳ
    w ຊ൪؀ڥΛखݩͰ༰қʹ࠶ݱՄೳ
    w ΞʔΩςΫνϟґଘ͔Β୤٫Ͱ͖Δ
    w ඇΠϯϑϥͰ΋%PDLFSpMFΛॻ͖΍͍͢
    w ͳͲͳͲ

    View full-size slide

  14. Docker Ͱ؀ڥߏங
    w ෳ਺ίϯςφͷ؅ཧ͕໘౗
    w 3BJMT͸൪ϙʔτͰཱͨͤͯɺ
    1PTUHSF42-ͱMJOLͰͭͳ͛ͯʜ
    w ্ཱͪ͛ॱং΋ߟྀ͠ͳ͍ͱ͍͚ͳ͍
    1PTUHSF42-
    3FEJT
    3BJMT
    3000/tcp
    link db:postgres
    link redis:redis

    View full-size slide

  15. Docker
    Compose

    View full-size slide

  16. EPDLFSDPNQPTF
    Docker Compose
    w ෳ਺ͷ%PDLFSίϯςφͰߏ੒͞ΕΔΞϓϦ
    Λ؆୯ʹ؅ཧ͢ΔͨΊͷπʔϧ
    w :".-Ͱ֤ίϯςφͷىಈߏ੒ͱ࿈ܞΛهड़
    w docker runͷΦϓγϣϯΛߏ଄తʹ

    ॻ͚ΔͨΊɺ୯ҰίϯςφͰ΋ศར
    w ίϚϯυҰൃͰΞϓϦͷVQEPXO

    TDBMJOH͕Մೳ

    View full-size slide

  17. docker-compose.yml
    ຊൃදͰ͸$PNQPTFpMFGPSNBUWʹ

    ४ڌͯ͠આ໌͍͖ͯ͠·͢
    db:
    image: postgres
    web:
    build: .
    command: bundle exec rails s -p 3000 -b '0.0.0.0'
    volumes:
    - .:/myapp
    ports:
    - "3000:3000"
    links:
    - db

    View full-size slide

  18. docker-compose.yml
    ຊൃදͰ͸$PNQPTFpMFGPSNBUWʹ

    ४ڌͯ͠આ໌͍͖ͯ͠·͢
    db:
    image: postgres
    web:
    build: .
    command: bundle exec rails s -p 3000 -b '0.0.0.0'
    volumes:
    - .:/myapp
    ports:
    - "3000:3000"
    links:
    - db
    $ docker build . web
    $ docker run -p 3000:3000 -v .:/myapp --links db web

    View full-size slide

  19. $ git clone \
    https://github.com/dtan4/docker-service-rails.git
    $ git checkout step0
    Docker Compose Ͱ

    Rails ΛݐͯͯΈΑ͏

    View full-size slide

  20. w ·ͣ1PTUHSF42-ΛཱͯΔهड़
    Docker Compose Ͱ

    Rails ΛݐͯͯΈΑ͏
    db:
    image: postgres:9.4

    View full-size slide

  21. w ͍ͭͮͯ3BJMTΛཱͯΔهड़
    Docker Compose Ͱ

    Rails ΛݐͯͯΈΑ͏
    web:
    build: .
    command: bin/rails s -p 3000 -b '0.0.0.0'
    environment:
    - DATABASE_HOST=db
    - DATABASE_PORT=5432
    - DATABASE_USER=postgres
    ports:
    - "3000:3000"
    links:
    - db

    View full-size slide

  22. w ىಈʂ
    w ৽͍͠λʔϛφϧ΢Οϯυ΢Λ։͍ͯʜ
    w σʔλϕʔεͷηοτΞοϓ
    Docker Compose Ͱ

    Rails ΛݐͯͯΈΑ͏
    $ docker-compose build
    $ docker-compose up
    $ docker-compose run web bin/rake db:setup
    %PDLFS.BDIJOF্ͰCVJME͕ࣦഊ͢Δ৔߹͸ɺ
    $ docker-machine stop default
    $ docker-machine up default
    Ͱ7.Λ࠶ىಈ͍ͯͩ͘͠͞
    VQ͕ࣦഊ͢Δ৔߹͸ɺ
    $ docker-compose stop
    $ docker-compose rm -f
    ͰίϯςφΛফͯ͠΍Γ௚͢

    View full-size slide

  23. w ΞΫηεͯ͠Έ·͠ΐ͏ʂ
    Docker Compose Ͱ

    Rails ΛݐͯͯΈΑ͏
    $ open http://$(docker-machine ip dev):3000
    # Linux など直接 Docker が稼働しているなら
    $ open http://localhost:3000
    EFGBVMU EPDLFSWNͳͲ
    खݩͷ؀ڥʹ߹Θ͍ͤͯͩ͘͞

    View full-size slide

  24. αʔόىಈޙͷॳظԽॲཧ͸ɺFOUSZQPJOU

    εΫϦϓτΛ͔·ͤΔํ๏͕͋Δ
    ͋ΘͤͯΑΈ͍ͨ

    IUUQTNFEJVNDPN!LFMTFZIJHIUPXFSGSBDUVSFEBQQTDED
    Tips
    #!/bin/bash
    bin/rake db:create
    bin/rake db:migrate
    bin/rake db:seed
    exec $@
    FROM ruby:2.3.0-slim
    MAINTAINER dtan4
    # …
    ENTRYPOINT ["/app/entrypoint.sh"]
    CMD ["bin/rails", "s]

    View full-size slide

  25. w KXJMEFSOHJOYQSPYZ
    w VIRTUAL_HOST؀ڥม਺ͷத਎Λ༻͍ͯ

    ϧʔςΟϯάΛ࡞੒
    w ઌ΄Ͳͷdocker-compose.ymlʹҎԼΛ௥Ճ
    nginx ΋૊Έ߹ΘͤΑ͏
    nginx:
    image: jwilder/nginx-proxy
    ports:
    - 80:80
    volumes:
    - /var/run/docker.sock:/tmp/docker.sock:ro

    View full-size slide

  26. w 3BJMTͷϙʔτׂ౰Λͳ͘͢
    w VIRTUAL_HOST؀ڥม਺Λ௥Ճ
    nginx ΋૊Έ߹ΘͤΑ͏
    web:
    build: .
    command: bin/rails s -p 3000 -b '0.0.0.0'
    environment:
    - DATABASE_HOST=db
    - DATABASE_PORT=5432
    - DATABASE_USER=postgres
    - VIRTUAL_HOST=docker.sokushu.dev
    links:
    - db

    View full-size slide

  27. w ىಈʂ
    w ઃఆͨ͠ϗετ໊ͰΞΫηε
    w /etc/hostsΛॻ͖׵͑ͯϒϥ΢βͰ΋
    nginx ΋૊Έ߹ΘͤΑ͏
    $ docker-compose up
    $ curl -H "Host: docker.sokushu.dev" \
    http://$(docker-machine ip docker-vm)

    View full-size slide

  28. Heroku
    w git push͢Δ͚ͩͰΞϓϦ͕σϓϩΠ͞Εɺ

    ࣗಈͰ63-΋ׂΓৼΒΕΔ
    w 1PTUHSF42- 3FEJT .FNDBDIFE͸ඪ४Ͱϓ
    ϥάΠϯ͕༻ҙ͞Ε͍ͯΔ
    w ͠͹Β͘લʹ%PDLFSରԠ΋ͨ͠
    w खݩͰಉ͡Α͏ͳߏ੒Λ࠶ݱͰ͖Δ
    w ࣗલOHJOY &MBTUJDTFBSDIͳͲ͸ݐͯΒΕͳ͍

    View full-size slide

  29. Paus
    w ΠϯυωγΞޠͰʮ͘͡Βʯ
    w docker-compose.ymlॻ͍ͯgit push͢Δ

    ͚ͩͰɺΫϥ΢υ্ʹΞϓϦέʔγϣϯ͕

    ىಈ͢Δ؆қ1BB4
    w $PSF04%PDLFS
    EUBOQBVT

    View full-size slide

  30. Paus ʹσϓϩΠ͠Α͏
    • git checkout master
    w docker-compose.ymlมߋ
    web:
    image: quay.io/dtan4/docker-service-rails:latest
    command: bin/rails s -p 8080 -b '0.0.0.0'
    environment:
    - DATABASE_HOST=db
    - DATABASE_PORT=5432
    - DATABASE_USER=postgres
    ports:
    - 8080
    links:
    - db

    View full-size slide

  31. Paus ʹσϓϩΠ͠Α͏
    w IUUQQBVTXBOUFEMZDPNͰެ։伴ొ࿥
    ࣗ෼ͷϢʔβ໊
    44)ެ։伴

    View full-size slide

  32. Paus ʹσϓϩΠ͠Α͏
    • ~/.ssh/configʹҎԼΛ௥Ճ
    Host paus
    HostName paus.wantedly.com
    User git
    Port 2222
    IdentityFile ~/.ssh/id_rsa
    ઌ΄Ͳొ࿥ͨ͠ެ։伴ͷ
    ϖΞͱͳΔൿີ伴

    View full-size slide

  33. Paus ʹσϓϩΠ͠Α͏
    w ϦϞʔτϦϙδτϦpausΛ௥Ճ
    $ git remote add paus \
    paus:/

    View full-size slide

  34. Paus ʹσϓϩΠ͠Α͏
    w git pushͯ͠σϓϩΠʂ
    $ git push paus master

    View full-size slide

  35. Paus ʹσϓϩΠ͠Α͏
    σϓϩΠ͞Εͨ
    ΞϓϦͷ63-

    View full-size slide

  36. Paus ΞʔΩςΫνϟ
    %PDLFS

    $PNQPTF
    FUDE
    HJUSFDFJWFE
    git push
    docker-compose

    up
    etcdctl set
    7VMDBOE
    http://dtan4-rails-ffe7rf8.example.com
    XFC
    EC
    XFC
    EC

    View full-size slide

  37. Docker
    PaaS / IaaS

    View full-size slide

  38. Docker PaaS / IaaS
    w )FSPLVΫϩʔϯ
    w EPLLV BMU
    'MZOO %FJT &NQJSF
    w ಠࣗܗࣜ
    w ,VCFSOFUFT "NB[PO&$4
    w Πϯϑϥ͝ͱ؅ཧ
    w 5VUVN %$)2JP

    View full-size slide

  39. ·ͱΊ
    w 8BOUFEMZͷ։ൃݱ৔Ͱͷॾ໰୊ʹର͢Δ

    ղܾࡦͱͯ͠ɺ%PDLFSΛ༻͍ͨ؀ڥߏஙΛ
    ঺հ͠·ͨ͠
    w %PDLFS$PNQPTFΛ༻͍ͯ3BJMTͱෳ਺ͷ
    αʔϏεΛ૊Έ߹ΘͤͨΞϓϦ͕ߏஙͰ͖Δ
    Α͏ʹͳΓ·ͨ͠
    w 1BB4*BB4ͷ܈༤ׂڌ࣌୅

    View full-size slide