ããã«ã¡ã¯ãèã®ç©´ã©ãNSSã§ãã
ä»ã¾ã§ç§ã¯ããµã¼ãã¼ã¬ã¹ã®åå¼·ã®ä¸ç°ã¨ãã¦ãAWS Lambdaã使ã£ãWebã¢ããªã±ã¼ã·ã§ã³ã®ä½ææ¹æ³ããæ¬ããã°ãã¨ãã©ãã®å人èªçã§ç´¹ä»ãã¦ãã¾ããã
ãããããµã¼ãã¼ã¬ã¹ã«é¢ãããµã¼ãã¹ã¯AWS Lambdaã ãã§ã¯ãªããä»ã«ãããããããã¾ãã
ãã®ä¸ã§ä»åã¯ãAWS Fargateï¼ä»¥ä¸Fargateï¼ã使ã£ã¦ã2020å¹´5ææç¹ã®ææ°Railséçºç°å¢ãFargateã§ä½ã£ã¦è¦ããã¨æãã¾ãã
...ã¨æã£ã¦ããã®ã§ãããFargateãå©ç¨ããã«ã¯ãããã¤ãåæã¨ãªãç¥èãå¿ è¦ã«ãªããã¨ããããã¾ããã
åæã¨ãªãç¥èã¯ã
- ã³ã³ããã»Dockerã®ç¥è
- Amazon ECRãECSã®ç¥è
- Fargateã®ç¥è
ãªã©ã§ãã
å
¨ã¦ã説æããã¨é·ããªã£ã¦ãã¾ãã®ã§ãä½åãã«åãã¦èª¬æãã¦ããããã¨æãã¾ãã
ä½åã«ãªãããããã¾ããããæçµçã«ã¯Fargateã§Railsã®ã¢ããªã±ã¼ã·ã§ã³ãå
¬éãããã¨ãç®æãã¾ãã
ä»åã®è¨äºã§ã¯ã第ä¸åã¨ãã¦Dockerã§Railsç°å¢ãæ§ç¯ããæé ããç´¹ä»ãããã¨æãã¾ãã
1. AWS Fargateã¨ã¯
AWS Fargateã¯ããµã¼ãã¼ãã¯ã©ã¹ã¿ã¼ã®ç®¡çãã»ã¨ãã©å¿
è¦ã¨ããã«ã³ã³ãããå®è¡ããããã®ãµã¼ãã¹ã§ãã
ã³ã³ãããå®è¡ãããµã¼ãã¹ãªã®ã§ãåæã¨ãã¦ã³ã³ããã»Dockerã®ç¥èãå¿
è¦ã«ãªãããã§ãã
2. Dockerã¨ã¯
Dockerã¨ã¯ã³ã³ããæè¡ãå©ç¨ããä»®æ³åæè¡ã®ä¸ã¤ã§ããã³ã³ããä¸ã§ã¢ããªã±ã¼ã·ã§ã³ã®éçºã»å®è¡ããããªã¼ãã³ã½ã¼ã¹ã½ããã¦ã§ã¢ã§ãã
2020å¹´5ææç¹ã«ããã¦ããã³ã³ããã¨ããã°ï¼âDockerãã¨ãããããã¡ã¸ã£ã¼ãªã½ããã§ãã
3. Dockerã®è¨å®
æ©éãDockerã®è¨å®ããã¦ããã¾ãã
ä»åã®å®è¡ç°å¢ã¯ä»¥ä¸ã®éãã§ãã
å®è¡ç°å¢
- macOS Mojave 10.14.6
- Docker 19.03.5
- docker-compose 1.25.2
æ§ç¯ããç°å¢
- Ruby 2.7
- Rails 6
- MySQL 8.0
ã¾ããDockerãã¤ã³ã¹ãã¼ã«ãã¾ãã
Macã§ããã°ãDocker Desktop for Macãã¤ã³ã¹ãã¼ã«ããã°OKã§ãã
ã¤ã³ã¹ãã¼ã«ã§ããããä»åã®ã¢ããªã±ã¼ã·ã§ã³ãä½æãããã£ã¬ã¯ããªãä»»æã®å ´æã«ä½æãã¾ãã
$ mkdir docker_rails
ç¶ãã¦ãåè¨å®ãã¡ã¤ã«ãä½æãã¦ããã¾ãã
Dockerfile
Dockerã¤ã¡ã¼ã¸ãä½æããããã®è¨å®ãã¡ã¤ã«ã§ãã
è¡ã®å
é ã®å¤§æåãå½ä»¤ã§ãç¶ãå°æåãå¼æ°ã§ãã
å½ä»¤ã¯å°æåã§ãæ¸ãã¾ãããåºå¥ããããã«å¤§æåã§æ¸ãã®ãéä¾ã®ããã§ãã
FROM ruby:2.7 RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - \ && echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list RUN apt-get update -qq \ && apt-get install -y nodejs yarn \ && mkdir /docker_rails WORKDIR /docker_rails COPY Gemfile /docker_rails/Gemfile COPY Gemfile.lock /docker_rails/Gemfile.lock RUN bundle install COPY . /docker_rails COPY entrypoint.sh /usr/bin/ RUN chmod +x /usr/bin/entrypoint.sh ENTRYPOINT ["entrypoint.sh"] EXPOSE 3000 CMD ["rails", "server", "-b", "0.0.0.0"]
Rails6ã®ç¹å¾´ã¨ãã¦ãWebpackerãæ¨æºã®JavaScriptã³ã³ãã¤ã©ã¨ãªã£ãããã
Webpackerãã¤ã³ã¹ãã¼ã«ããããã®yarnã
yarnãã¤ã³ã¹ãã¼ã«ããããã®node.jsãã¤ã³ã¹ãã¼ã«ããå¿
è¦ãããã¾ãã
Dockerfileã®è¨è¼ã«ã¤ãã¦ã¯ãã¡ãã®Dockerfileãªãã¡ã¬ã³ã¹ãåç
§ããã¨ç解ãæ·±ã¾ãã¾ãã
Gemfile
Railsã§ã¯ããªãã¿ã®ãã¡ã¤ã«ã§ããã¾ãã¯Railsãå ¥ãããã ããªã®ã§æ¬¡ã®ããã«ãè¨è¼ãã¾ãã
source 'https://rubygems.org' gem 'rails', '~>6'
Gemfile.lock
ãã®æç¹ã§ã¯ã空ã®ãã¡ã¤ã«ã®ã¿ä½æãã¾ãã
空ãã¡ã¤ã«
entrypoint.sh
ã³ã³ãããå®è¡ããããã³ã«èµ·åããã·ã§ã«ã§ãã
server.pidã¨ãããã¡ã¤ã«ãåå¨ããã¨Railsãåèµ·åããªããããåé¤ãã¾ãã
#!/bin/bash set -e rm -f /docker_rails/tmp/pids/server.pid exec "$@"
docker-compose.yml
Docker Composeã¯ãMySQLãRedisãªã©ã®ã¢ããªã±ã¼ã·ã§ã³ãæ§æããè¤æ°ã®ãµã¼ãã¹ãä¸æ¬ã§å®ç¾©ã»å®è¡ãããã¼ã«ã§ãã
docker-compose.ymlã«ãã¢ããªã±ã¼ã·ã§ã³ãæ§æããåãµã¼ãã¹ãå®ç¾©ãããã¨ã§ã
ç¬ç«ãããµã¼ãã¹ãä¸æã«å®è¡ã§ãã¾ãã
version: '3' services: db: image: mysql:8.0 environment: MYSQL_ROOT_PASSWORD: password ports: - '3306:3306' command: --default-authentication-plugin=mysql_native_password volumes: - mysql-data:/var/lib/mysql web: build: . command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'" volumes: - .:/docker_rails ports: - "3000:3000" depends_on: - db stdin_open: true tty: true command: bundle exec rails server -b 0.0.0.0 volumes: mysql-data: driver: local
4. Dockerã¤ã¡ã¼ã¸ã®ä½æ
ããã¾ã§ã§ãããã次ã®ã³ãã³ããå®è¡ãã¦Railsã®ã¢ããªã±ã¼ã·ã§ã³ãæ°è¦ã«çæãã¾ãã
$ docker-compose run web rails new . --force --no-deps --database=mysql --skip-test --webpacker
docker-compose run
ã§ãæå®ãããµã¼ãã¹ã«å¯¾ãã¦ãã³ãã³ãã1åå®è¡ã§ãã¾ãã
- --force : ãã¡ã¤ã«ãä¸æ¸ããã¾ã
- --no-deps : ãªã³ã¯ããä»ã®ãµã¼ãã¹ãèµ·åããªãããã«ãã¾ã
- --database=mysql : DBã«MySQLãæå®ãã¾ã
- --skip-test : Railsã®ããã©ã«ããã¹ããã¼ã«
Minitest
ãçæããªãããã«ãã¾ã - --webpacker : Webpackerã«å¯¾å¿ããã¢ããªã±ã¼ã·ã§ã³ãçæãã¾ã1
ç¶ãã¦æ¬¡ã®ã³ãã³ããå®è¡ãã¦ãDockerã¤ã¡ã¼ã¸ãæ§ç¯ãã¾ãã
$ docker-compose build
5. Dockerã³ã³ããã®å®è¡
rails new
ã§çæãããdatabase.yml
ã次ã®ããã«ä¿®æ£ãã¾ãã
- database.yml
default: &default adapter: mysql2 encoding: utf8mb4 pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> username: <%= ENV.fetch("MYSQL_USERNAME", "root") %> password: <%= ENV.fetch("MYSQL_PASSWORD", "password") %> host: <%= ENV.fetch("MYSQL_HOST", "db") %> development: <<: *default database: docker_rails_development test: <<: *default database: docker_rails_test production: <<: *default database: docker_rails_production username: docker_rails password: <%= ENV['DOCKER_RAILS_DATABASE_PASSWORD'] %>
ä¿®æ£ãããã次ã®ã³ãã³ãã§ãã¼ã¿ãã¼ã¹ãä½æãã¾ãã
$ docker-compose run web rake db:create
次ã®ã³ãã³ãã§ã³ã³ããã®ä½æã»å®è¡ãã¾ãã
$ docker-compose up
docker-compose.ymlã§è¨è¼ããDBã¨Webã¢ããªã±ã¼ã·ã§ã³ã®ã³ã³ãããé ã«å®è¡ããã¾ãã
localhost:3000ã«ã¢ã¯ã»ã¹ãã¾ãã ãã¾ããã£ã¦ããã°ãRailsã®ç»é¢ã表示ããã¦ãã¾ãã
6. ã¾ã¨ã
ä»åã¯Dockerã使ã£ãRails6ï¼MySQL8.0ç°å¢ã®æ§ç¯ããç´¹ä»ãã¾ããã
ãã¾ã¾ã§ã¯ãæ°ããã¢ããªã±ã¼ã·ã§ã³ãä½æãããã³ã«ç°å¢æ§ç¯ãèããªããã°ãªãã¾ããã§ãããã
ä»åã®æ¹æ³ã使ãã°ãä¸é¨ã®ãã¡ã¤ã«ãã³ãã¼ãä¿®æ£ããã ãã§ã
ããç¨åº¦ã®ç°å¢ãæ§ç¯ãããã¨ãã§ãã¾ãã
ã¨ã¯ãããã´ã¼ã«ã¯Fargateã«ãããã¤ãã¦ã¢ããªã±ã¼ã·ã§ã³ãå
¬éãããã¨ãªã®ã§ã
å¼ãç¶ããåå¼·ãã¦ããããã¨æãã¾ãã
åèã«ããã¦ããã ãããµã¤ãã»è¨äº
P.S.
èã®ç©´ã©ãã§ã®éçºã«å°ãã§ãèå³ãæã£ã¦ããã ããæ¹ã¯ãæ¡ç¨èª¬æä¼ãã«ã¸ã¥ã¢ã«é¢è«ã¨ããå ´ã§ãã£ã¨æ·±ãã話ããããã¨ãã§ãã¾ãããã²ãæ°è»½ã«ç³ãè¾¼ã¿ããã ããã°å¹¸ãã§ãã
ã«ã¸ã¥ã¢ã«é¢è«ã§ã¯èã®ç©´ã©ãã®ã¨ã³ã¸ãã¢ããéçºããã»ã¹ã®å
容ã§ãã£ããããä»æä½è¦ã¾ããï¼ãã¨ãã£ããªã¿ã¯ãã¼ã¯ããæ¥åã®è©±ã¾ã§ä½ã§ããå¿ããã¾ãã
ã«ã¸ã¥ã¢ã«é¢è«ãæ¡ç¨æ
å ±ã¯ãã¡ããã確èªãã ããã
yumenosora.co.jp
ã¾ããæ¯é±ç«æãæ¨æã«ã¯Tora-Lab Meetup!ã¨ç§°ãã¦èã®ç©´ã©ãã®ã¨ã³ã¸ãã¢ã»æ¡ç¨æ
å½ã¨ã話ã§ããæ©ä¼ãè¨ãããã¦ããã ããã¨ã«ãªãã¾ããã
èã®ç©´ã©ãã«èå³ããããã¨ã³ã¸ãã¢ãæ¡ç¨æ
å½ã«è³ªåããããã¨ãããããªã©ã©ãªãã§ããåå ä¸ããã
news.toranoana.jp
6æ12æ¥ã«ãããªã³ã©ã¤ã³éå¬ãã¨ãã®ããªæ¡ç¨èª¬æä¼ 6/12 ãªã¿ã¯ä¼æ¥ã§åãã¨ã³ã¸ãã¢ã®é
åã«ã¤ãã¦ããéå¬ãã¾ãã
ã¨ãã®ããªã©ãã®ã¨ã³ã¸ãã¢ã®åãæ¹ãªã©èª¬æãã¾ãããã²å¥®ã£ã¦ãåå ãã ããã
yumenosora.connpass.com
ããã«ãå¼ç¤¾ã§ã¯æ°åã³ããã¦ã¤ã«ã¹ææççµæ¯å¾ããã«ãªã¢ã¼ããç¶ç¶å°å
¥ãããã¨ã«ãªãã¾ããï¼
prtimes.jp
-
ä»åã¨ãã¾ãé¢ä¿ããã¾ããããæ°è¦ã®ããã¸ã§ã¯ãã«ReactãVueãªã©ã使ç¨ããå ´åã–webpack=reactãªã©ã¨è¨å®ããããã§ãã↩