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

Railsエンジニア向けDocker入門 / Docker description for Rails engineers

Railsエンジニア向けDocker入門 / Docker description for Rails engineers

Ecad9d801d79f6c6e5df93094690685e?s=128

Takumi Shotoku

December 02, 2021
Tweet

Transcript

  1. RailsΤϯδχΞ޲͚Dockerೖ໳ Omotesando.rb #68 2021/12/02 1

  2. ࣗݾ঺հ • ໊લ: ਖ਼ಙ ޼(aka: ਆ଎) • ձࣾ: ϝυϐΞגࣜձࣾ •

    ॴଐ: CTOࣨSRE • GitHub: @sinsoku (ը૾ӈ্) • Twitter: @sinsoku_listy (ը૾ӈԼ) 2
  3. ࠷ߴͷϒϩάΞϓϦΛ࡞Δ (3min) $ rails new omotesando_blog $ cd omotesando_blog $

    bin/rails g scaffold blog title body:text $ bin/rails db:prepare $ bin/rails s 3
  4. ϒϩά͕౤ߘͰ͖Δʂ͍͢͝ʂ 4

  5. ଞPCͰ؆୯ʹಈ͘Α͏ʹ͍ͨ͠ 5

  6. Dockerͷ༻్ ͦͷ1 ΞϓϦ ͷίϯςφԽ 6

  7. ΞϓϦͷίϯςφԽ omotesando_blog ͱ͍͏όΠφϦΛ࡞Δͷʹ͍ۙɻ $ docker run omotesando_blog bin/rails --version $

    docker run omotesando_blog bin/rails stats omotesando_blog ͷDockerΠϝʔδʹ͸ιʔείʔυɺRubyɺ gemɺnpmؚ͕·Ε͍ͯΔͷͰɺར༻ऀ͸Docker͚ͩ༻ҙ͢Ε͹ ྑ͍ɻ 7
  8. Dockerfile ͷྫ1 FROM ruby:3.0.3 WORKDIR /workspace # Install Node.js, Yarn

    RUN curl -fsSL https://deb.nodesource.com/setup_14.x | bash - RUN apt-get update && apt-get install -y nodejs RUN npm install --global yarn # ιʔείʔυΛ௥Ճ͢Δ COPY . /workspace # gem΍npmΛ௥Ճ͢Δ RUN bin/setup 8
  9. Ϗϧυͯ͠ࢼ͢ $ docker build -t omotesando_blog . $ docker run

    -p 3000:3000 \ omotesando_blog bin/rails s -b 0.0.0.0 9
  10. Dockerศར 10

  11. ։ൃ؀ڥ΋ ! ʹ͢Ε͹ศར...ʁ 11

  12. Dockerfile ͷྫ1ʢ࠶ܝʣ FROM ruby:3.0.3 WORKDIR /workspace # Install Node.js, Yarn

    RUN curl -fsSL https://deb.nodesource.com/setup_14.x | bash - RUN apt-get update && apt-get install -y nodejs RUN npm install --global yarn # ιʔείʔυΛ௥Ճ͢Δ COPY . /workspace # gem΍npmΛ௥Ճ͢Δ RUN bin/setup 12
  13. ίʔυมߋ͢Δ౓ʹϏϧυ͕ඞཁ 13

  14. Dockerͷ༻్ ͦͷ2 ؀ڥ ͷίϯςφԽ 14

  15. Dockerfile ͷྫ2 FROM ruby:3.0.3 WORKDIR /workspace # Install Node.js RUN

    curl -fsSL https://deb.nodesource.com/setup_14.x | bash - RUN apt-get update && apt-get install -y nodejs # Install Yarn RUN curl -o- -L https://yarnpkg.com/install.sh | bash # Set env to install gems into /workspace/vendor/bundle ENV BUNDLE_PATH vendor/bundle 15
  16. Ϗϧυͯ͠ࢼ͢ $ docker build -t omotesando_blog:v2 . $ docker run

    -v `pwd`:/workspace \ omotesando_blog:v2 bin/setup $ docker run -v `pwd`:/workspace -p 3000:3000 \ omotesando_blog:v2 bin/rails s -b 0.0.0.0 omotesando_blog:v2 ʹ͸RubyɺNode.jsɺYarn͸ؚ·Ε͍ͯΔɻ ։ൃதʹมߋ͢Δ͜ͱͷ͋Δίʔυɺgemɺnpm͸Πϝʔδʹؚ Ίͳ͍ɻ 16
  17. ·ͱΊ Dockerͷ༻్͸2ͭ͋Δɻ 1. ΞϓϦͷίϯςφԽ 2. ؀ڥͷίϯςφԽ ༻్ʹΑͬͯDockerfileͰؾΛ͚ͭΔ఺͕มΘΔͷͰ஫ҙ͕͍Δɻ 17