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ೖͬͯ·͔͢ʁಈ͖·͔͢ʁ 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 ʢόʔδϣϯ͸ಛʹؾʹ͠ͳ͍Ͱ͢ɻ΋ͪΖΜ࠷৽Ͱ͋Δ΄ͲΑ͍ʣ
  2. www.wantedly.com Λ
 ࢧ͑Δٕज़ w 3BJMTͷ΄͔ʹʜ w 1PTUHSF42- w &MBTUJDTFBSDI w

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


    όʔδϣϯΛγϏΞʹ߹Θͤͳ͍ͱμϝ w ຊ൪ͷ&MBTUJDBDIF͸3FEJTYͷΈ w ΠϯετʔϧεΫϦϓτͰແཧ΍Γ
 όʔδϣϯΛݻఆʜ w bundle install͕ࣦഊʢಛʹ$֦ுHFNʣ
  4. ϑΟʔνϟϒϥϯνͷ
 σϓϩΠ w ຊ൪؀ڥ 1SPEVDUJPO  2"؀ڥ 2"  w

    2"ॱ൪଴ͪ໰୊ w 2"ͭͩͱ଍Γͳ͘ͳ͖ͬͯͨ w ࣗ෼ͷϒϥϯνΛؾܰʹσϓϩΠͰ͖Δ
 ؀ڥ͕΄͍͠
  5. EPDLFSDPNQPTF Docker Compose w ෳ਺ͷ%PDLFSίϯςφͰߏ੒͞ΕΔΞϓϦ Λ؆୯ʹ؅ཧ͢ΔͨΊͷπʔϧ w :".-Ͱ֤ίϯςφͷىಈߏ੒ͱ࿈ܞΛهड़ w docker

    runͷΦϓγϣϯΛߏ଄తʹ
 ॻ͚ΔͨΊɺ୯ҰίϯςφͰ΋ศར w ίϚϯυҰൃͰΞϓϦͷVQEPXO
 TDBMJOH͕Մೳ
  6. 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
  7. 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
  8. 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
  9. 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 ͰίϯςφΛফͯ͠΍Γ௚͢
  10. w ΞΫηεͯ͠Έ·͠ΐ͏ʂ Docker Compose Ͱ
 Rails ΛݐͯͯΈΑ͏ $ open http://$(docker-machine

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

    up $ curl -H "Host: docker.sokushu.dev" \ http://$(docker-machine ip docker-vm)
  13. Heroku w git push͢Δ͚ͩͰΞϓϦ͕σϓϩΠ͞Εɺ
 ࣗಈͰ63-΋ׂΓৼΒΕΔ w 1PTUHSF42- 3FEJT .FNDBDIFE͸ඪ४Ͱϓ ϥάΠϯ͕༻ҙ͞Ε͍ͯΔ

    w ͠͹Β͘લʹ%PDLFSରԠ΋ͨ͠ w खݩͰಉ͡Α͏ͳߏ੒Λ࠶ݱͰ͖Δ w ࣗલOHJOY &MBTUJDTFBSDIͳͲ͸ݐͯΒΕͳ͍
  14. 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
  15. Paus ʹσϓϩΠ͠Α͏ • ~/.ssh/configʹҎԼΛ௥Ճ Host paus HostName paus.wantedly.com User git

    Port 2222 IdentityFile ~/.ssh/id_rsa ઌ΄Ͳొ࿥ͨ͠ެ։伴ͷ ϖΞͱͳΔൿີ伴
  16. Paus ΞʔΩςΫνϟ %PDLFS
 $PNQPTF FUDE HJUSFDFJWFE git push docker-compose
 up

    etcdctl set 7VMDBOE http://dtan4-rails-ffe7rf8.example.com XFC EC XFC EC
  17. Docker PaaS / IaaS w )FSPLVΫϩʔϯ w EPLLV BMU 'MZOO

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