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

Dockerで作るRails実行環境

 Dockerで作るRails実行環境

Tetsushi Hasesaku

November 09, 2018
Tweet

More Decks by Tetsushi Hasesaku

Other Decks in Technology

Transcript

  1. w ੖ࠤٱ఩࢜ w ϐΫγϒגࣜձࣾ w ͳΜͰ΋͢ΔΤϯδχΞ w αʔόαΠυ 3VCZ 1)1

    $  w ϑϩϯτΤϯυ 3FBDU 5ZQFTDSJQU  w ؀ڥߏங "84 7BHSBOU %PDLFS  w ςετ 4FMFOJVN "QQJVN  w 6OJUZ w झຯ͸ϓϩάϥϛϯάͱ$(࡞੒  ࣗݾ঺հ
  2. ίϯςφܕԾ૝؀ڥɹ w ϗετ04 -JOVY ͱΧʔωϧ෦෼Λڞ༗ͨ͠Ծ૝؀ڥ w Χʔωϧ෦෼͕ڞ༗͞ΕΔͨΊಉҰͷ04 -JOVY ্Ͱ͔͠ಈ͔͢ ͜ͱ͕Ͱ͖ͳ͍

    w ैདྷͷԾ૝؀ڥ ϗετܕ ͱҧ͍ɺԾ૝Ϛγϯ෦෼ΛͨͯΔඞཁ͕ ͳ͍ͨΊɺΦʔόϔου͕গͳ͍ 
  3. %PDLFSΛ৮ͬͯΈΑ͏ • docker run -it ruby:2.5.3 /bin/bash • docker run

    %PDLFSίϯςφͷىಈ • -itΦϓγϣϯlJz͸zJOUFSBDUJWFz lUz͸zUUZz • ruby:2.5.3 ࣮ߦ͢Δ%PDLFSΠϝʔδ • /bin/bashίϯςφ্Ͱ࣮ߦ͢ΔίϚϯυ 
  4. 

  5.  curl -sL https://deb.nodesource.com/setup_10.x | bash - apt-get update -qq

    apt-get install -y build-essential libpq-dev nodejs npm install -g yarn mkdir -p /usr/src/rails-server
  6. 

  7. 

  8. %PDLFSΛ৮ͬͯΈΑ͏ • docker commit 7e7d0dcf9978 prepare-rails • docker commit: ίϯςφ͔Β৽͍͠ΠϝʔδΛ࡞੒

    • 7e7d0dcf9978: ϕʔεͱͳΔίϯςφID • prepare-rails: ࡞੒͢ΔΠϝʔδ໊ 
  9. 

  10. %PDLFSpMFͷ࡞੒ • docker build —-tag author/rails-server:latest . • docker build:

    Dockerfile͔ΒΠϝʔδΛ࡞੒ • —-tag: ΠϝʔδʹλάΛ͚ͭΔ • author/rails-server:latest: λά໊ • . : Dockerfile͕͋ΔσΟϨΫτϦ 
  11. 3BJMTΞϓϦέʔγϣϯߏங  CJOCBTIDCVOEMFFYFDSBJMTOFXNZBQQ5GXFCQBDLSFBDU EBUBCBTFNZTRMTLJQDP⒎FFTLJQTQSPDLFUT w 3BJMTΞϓϦͷ࡞੒ίϚϯυ w 5σϑΥϧτͷNJOJUFTUΛ࡞Βͳ͍ w G(FNpMF͕͋ͬͯ΋্ॻ͖͢Δ

    w ŠXFCQBDLSFBDUXFCQBDLͰ3FBDUΛ࢖͏ w ŠEBUBCBTFNZTRM.Z42-Λσʔλϕʔεʹ͢Δ w ŠTLJQDP⒎FF$P⒎FF4DSJQUΛ࡞Βͳ͍ w ŠTLJQTQSPDLFUT4QSPDLFUTΛΠϯετʔϧ͠ͳ͍
  12. 3BJMTΞϓϦέʔγϣϯߏங  DENZBQQCVOEMFFYFDSBLFXFCQBDLFSJOTUBMMCVOEMF FYFDSBLFXFCQBDLFSJOTUBMMSFBDUCVOEMFFYFDSBLF XFCQBDLFSJOTUBMMUZQFTDSJQU w 8FCQBDLFSͷઃఆ w DENZBQQSBJMTOFXͰ࡞੒ͨ͠3BJMTΞϓϦ·ͰҠಈ w

    CVOEMFFYFDSBLFXFCQBDLFSJOTUBMM8FCQBDLFSͷΠϯετʔϧ w CVOEMFFYFDSBLFXFCQBDLFSJOTUBMMSFBDU8FCQBDLFSʹSFBDUΛೖΕΔ w CVOEMFFYFDSBLFXFCQBDLFSJOTUBMMUZQFTDSJQU8FCQBDLFSʹ UZQFTDSJQUΛೖΕΔ
  13. 

  14. 3BJMTαʔόͷىಈ  ҎԼɺ%PDLFSpMFΛ)0.&TIBSFSBJMTBQQNZBQQ্ʹ࡞੒͠ɺ Ϗϧυ FROM ruby:2.5.3 ENV APP_DIR /usr/src/myapp RUN

    mkdir -p ${APP_DIR} WORKDIR $APP_DIR RUN curl -sL https://deb.nodesource.com/setup_10.x | bash - && \ apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs mysql-client &&\ npm install -g yarn COPY . $APP_DIR RUN bundle install --path vendor/bundle CMD ["bundle", "exec", "rails", "server", "-b", "0.0.0.0"]
  15. EPDLFSDPNQPTF w 3BJMTଆͷ%#ઃఆΛमਖ਼ w )0.&TIBSFSBJMTTFSWFSNZBQQDPOpHEBUBCBTFZNMͷEFGBVMU෦ ෼ΛҎԼͷΑ͏ʹमਖ਼  default: &default adapter:

    mysql2 encoding: utf8 pool: 5 username: root password: <%= ENV['MYAPP_DATABASE_PASSWORD'] %> host: <%= ENV['MYAPP_DATABASE_HOST'] %>
  16. EPDLFSDPNQPTF  version: '2' services: db: image: mysql:5.7 environment: -

    MYSQL_ROOT_PASSWORD=mysql ports: - 3306:3306 networks: - default web: environment: - MYAPP_DATABASE_PASSWORD=mysql - MYAPP_DATABASE_HOST=db build: rails-app/myapp ports: - 3000:3000 networks: - default depends_on: - db networks: default: driver: bridge w EPDLFSDPNQPTFZNMΛ)0.&TIBSFʹ ࡞੒ w ͦͷޙɺdocker-compose upͰىಈ͢Δ
  17. EPDLFSDPNQPTF  w .Z42-ͷ্ཱ͕ͪΓΛ؂ࢹͯ͠ ࣮ߦ͢ΔγΣϧεΫϦϓτΛ࡞ ੒ w ͜ΕΛɺ)0.&TIBSFSBJMT TFSWFSNZBQQʹ഑ஔ͓ͯ͘͠ #!/bin/sh

    set -e host="$1" shift user="$1" shift password="$1" shift cmd="$@" echo "Waiting for mysql" until mysql -h"$host" -u"$user" -p"$password" &> /dev/null do >$2 echo -n "." sleep 1 done bundle exec rake db:create bundle exec rake db:migrate exec $cmd
  18.  FROM <image> ϕʔεʹͳΔΠϝʔδΛઃఆ USER <user> ࣮ߦϢʔβͷ੾Γସ͑ ENV <key> <value>

    ΠϝʔδϏϧυ࣌ʹ࢖༻͢Δ؀ڥม਺Λઃఆ RUN <command> ΠϝʔδϏϧυ࣌ʹ࣮ߦ͢ΔίϚϯυΛઃఆ CMD <command> ίϯςφ࡞੒࣌ʹ࣮ߦ͢ΔίϚϯυΛઃఆ COPY <src>... <dest> <src>͔Β<dest>ʹϑΝΠϧΛίϐʔ͢Δ ADD <src>... <dest> tarϑΝΠϧͷల։΍ɺϦϞʔτURL͔ΒͷऔಘΛߦ ͍͍ͨ৔߹ͷΈ࢖༻͢Δ WORKDIR <directory> ΠϝʔδϏϧυ࣌ʹ࣮ߦ͢ΔϫʔΩϯάσΟϨΫτ ϦΛઃఆ͢Δ ͓·͚%PDLFSpMFνʔτγʔτ