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

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

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

92ce4587cc8465736433e698b1e50aaa?s=128

Daisuke Fujita

April 12, 2016
Tweet

Transcript

  1. %PDLFSΛ࢖ͬͨ αʔϏεߏங ଎शձ!8"/5&%-:  EPDLFS@XBOUFEMZ %BJTVLF'VKJUB!EUBO ʢެ։൛ʣ

  2. Daisuke Fujita @dtan4 Πϯλʔϯ
 @ΠϯϑϥνʔϜ

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

  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 ʢόʔδϣϯ͸ಛʹؾʹ͠ͳ͍Ͱ͢ɻ΋ͪΖΜ࠷৽Ͱ͋Δ΄ͲΑ͍ʣ
  5. ຊ೔ͷ಺༰ w 8BOUFEMZͰͷ։ൃʹ͓͚Δॾ໰୊ w %PDLFS$PNQPTFͰαʔϏεΛಈ͔ͦ͏ w %PDLFS$PNQPTFͰߏஙͨ͠ΞϓϦΛ
 σϓϩΠ͠Α͏ w %PDLFS1BB4*BB4

  6. Wantedly Ͱͷ։ൃʹ
 ͓͚Δॾ໰୊

  7. www.wantedly.com Λ
 ࢧ͑Δٕज़ w 3BJMTͷ΄͔ʹʜ w 1PTUHSF42- w &MBTUJDTFBSDI w

    .FNDBDIFE w 3FEJT w 1IBOUPN+4  w ։ൃɾςετʹ͸͜ΕΒͷαʔϏε͕ඞཁ
  8. www.wantedly.com Λ
 ࢧ͑Δٕज़ w )PNFCSFXʹΑΔ։ൃ؀ڥߏங w brew updateͰ։ൃ؀ڥ͕յΕΔ w &MBTUJDTFBSDI͸ຊମͱϓϥάΠϯͷ


    όʔδϣϯΛγϏΞʹ߹Θͤͳ͍ͱμϝ w ຊ൪ͷ&MBTUJDBDIF͸3FEJTYͷΈ w ΠϯετʔϧεΫϦϓτͰແཧ΍Γ
 όʔδϣϯΛݻఆʜ w bundle install͕ࣦഊʢಛʹ$֦ுHFNʣ
  9. Elasticsearch on Docker

  10. Elasticsearch on Docker w ຊ൪ͷ&MBTUJDTFBSDI͸%PDLFSίϯςφͱ ͯ͠Քಇ͍ͯ͠Δ w RVBZJP্ͰΠϝʔδ؅ཧ w αʔϏεʹ߹Θͤͨνϡʔχϯά

    w ಠࣗࣙॻΛಉࠝ w ͜ΕΛ։ൃ΍ςετͰ࢖͍͍ͨʜ
  11. ϑΟʔνϟϒϥϯνͷ
 σϓϩΠ w ຊ൪؀ڥ 1SPEVDUJPO  2"؀ڥ 2"  w

    2"ॱ൪଴ͪ໰୊ w 2"ͭͩͱ଍Γͳ͘ͳ͖ͬͯͨ w ࣗ෼ͷϒϥϯνΛؾܰʹσϓϩΠͰ͖Δ
 ؀ڥ͕΄͍͠
  12. ϚΠΫϩαʔϏεج൫ w ੲ͔Β3BJMTͰ࣮૷͞Ε͍ͯͨػೳʹ͍ͭͯɺ ύϑΥʔϚϯε޲্ͷͨΊʹ(PͰ࠶࣮૷
 ͢Δྫ͕͋Δ w 4ZOD΍8BOUFEMZ5PPMTͷΑ͏ʹɺຊମͱ ͸ҟͳΔαʔϏεΛϦϦʔε͢Δػձ͕
 ૿͑ͨ w

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

  14. Docker Ͱ؀ڥߏங w ΞϓϦ୯ҐͰ؀ڥΛಠཱͤ͞ΒΕΔ w ͜ͷαʔϏεͰ͸&Tɺ͋ͷαʔϏεͰ ͸&TʜͰ΋ॊೈʹରԠՄೳ w ຊ൪؀ڥΛखݩͰ༰қʹ࠶ݱՄೳ w

    ΞʔΩςΫνϟґଘ͔Β୤٫Ͱ͖Δ w ඇΠϯϑϥͰ΋%PDLFSpMFΛॻ͖΍͍͢ w ͳͲͳͲ
  15. Docker Ͱ؀ڥߏங w ෳ਺ίϯςφͷ؅ཧ͕໘౗ w 3BJMT͸൪ϙʔτͰཱͨͤͯɺ 1PTUHSF42-ͱMJOLͰͭͳ͛ͯʜ w ্ཱͪ͛ॱং΋ߟྀ͠ͳ͍ͱ͍͚ͳ͍ 1PTUHSF42-

    3FEJT 3BJMT 3000/tcp link db:postgres link redis:redis
  16. Docker Compose

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

    runͷΦϓγϣϯΛߏ଄తʹ
 ॻ͚ΔͨΊɺ୯ҰίϯςφͰ΋ศར w ίϚϯυҰൃͰΞϓϦͷVQEPXO
 TDBMJOH͕Մೳ
  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
  19. 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
  20. $ git clone \ https://github.com/dtan4/docker-service-rails.git $ git checkout step0 Docker

    Compose Ͱ
 Rails ΛݐͯͯΈΑ͏
  21. w ·ͣ1PTUHSF42-ΛཱͯΔهड़ Docker Compose Ͱ
 Rails ΛݐͯͯΈΑ͏ db: image: postgres:9.4

  22. 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
  23. 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 ͰίϯςφΛফͯ͠΍Γ௚͢
  24. w ΞΫηεͯ͠Έ·͠ΐ͏ʂ Docker Compose Ͱ
 Rails ΛݐͯͯΈΑ͏ $ open http://$(docker-machine

    ip dev):3000 # Linux など直接 Docker が稼働しているなら $ open http://localhost:3000 EFGBVMU EPDLFSWNͳͲ खݩͷ؀ڥʹ߹Θ͍ͤͯͩ͘͞
  25. αʔόىಈޙͷॳظԽॲཧ͸ɺ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]
  26. 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
  27. 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
  28. w ىಈʂ w ઃఆͨ͠ϗετ໊ͰΞΫηε w /etc/hostsΛॻ͖׵͑ͯϒϥ΢βͰ΋ nginx ΋૊Έ߹ΘͤΑ͏ $ docker-compose

    up $ curl -H "Host: docker.sokushu.dev" \ http://$(docker-machine ip docker-vm)
  29. σϓϩΠ

  30. None
  31. None
  32. Heroku w git push͢Δ͚ͩͰΞϓϦ͕σϓϩΠ͞Εɺ
 ࣗಈͰ63-΋ׂΓৼΒΕΔ w 1PTUHSF42- 3FEJT .FNDBDIFE͸ඪ४Ͱϓ ϥάΠϯ͕༻ҙ͞Ε͍ͯΔ

    w ͠͹Β͘લʹ%PDLFSରԠ΋ͨ͠ w खݩͰಉ͡Α͏ͳߏ੒Λ࠶ݱͰ͖Δ w ࣗલOHJOY &MBTUJDTFBSDIͳͲ͸ݐͯΒΕͳ͍
  33. Paus w ΠϯυωγΞޠͰʮ͘͡Βʯ w docker-compose.ymlॻ͍ͯgit push͢Δ
 ͚ͩͰɺΫϥ΢υ্ʹΞϓϦέʔγϣϯ͕
 ىಈ͢Δ؆қ1BB4 w $PSF04

    %PDLFS EUBOQBVT
  34. 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
  35. Paus ʹσϓϩΠ͠Α͏ w IUUQQBVTXBOUFEMZDPNͰެ։伴ొ࿥ ࣗ෼ͷϢʔβ໊ 44)ެ։伴

  36. Paus ʹσϓϩΠ͠Α͏ • ~/.ssh/configʹҎԼΛ௥Ճ Host paus HostName paus.wantedly.com User git

    Port 2222 IdentityFile ~/.ssh/id_rsa ઌ΄Ͳొ࿥ͨ͠ެ։伴ͷ ϖΞͱͳΔൿີ伴
  37. Paus ʹσϓϩΠ͠Α͏ w ϦϞʔτϦϙδτϦpausΛ௥Ճ $ git remote add paus \

    paus:<username>/<repository>
  38. Paus ʹσϓϩΠ͠Α͏ w git pushͯ͠σϓϩΠʂ $ git push paus master

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

  40. Paus ΞʔΩςΫνϟ %PDLFS
 $PNQPTF FUDE HJUSFDFJWFE git push docker-compose
 up

    etcdctl set 7VMDBOE http://dtan4-rails-ffe7rf8.example.com XFC EC XFC EC
  41. Docker PaaS / IaaS

  42. Docker PaaS / IaaS w )FSPLVΫϩʔϯ w EPLLV BMU 'MZOO

    %FJT &NQJSF w ಠࣗܗࣜ w ,VCFSOFUFT "NB[PO&$4 w Πϯϑϥ͝ͱ؅ཧ w 5VUVN %$)2JP
  43. ·ͱΊ

  44. ·ͱΊ w 8BOUFEMZͷ։ൃݱ৔Ͱͷॾ໰୊ʹର͢Δ
 ղܾࡦͱͯ͠ɺ%PDLFSΛ༻͍ͨ؀ڥߏஙΛ ঺հ͠·ͨ͠ w %PDLFS$PNQPTFΛ༻͍ͯ3BJMTͱෳ਺ͷ αʔϏεΛ૊Έ߹ΘͤͨΞϓϦ͕ߏஙͰ͖Δ Α͏ʹͳΓ·ͨ͠ w

    1BB4*BB4ͷ܈༤ׂڌ࣌୅