0
0

More than 3 years have passed since last update.

Rails6でDockerを用いてrails newするまで

Posted at

本記事の目的

タイトルにある通り、Rails6でDockerを使用し環境構築を行う。
今までRails5をメインに使用してきたのでその忘備録も兼ねて執筆。
※Webpackerとは
https://railsguides.jp/webpacker.html

環境構築手順

どうやらRails6からWebpackerがデフォルトとなり、環境構築が一筋縄ではいかないらしい。
Dockerの公式マニュアルを読みながら、作業開始!
https://docs.docker.com/samples/rails/

手順1.

まずはターミナルを開き

% mkdir <app名>
% cd <app名>

でアプリを作成するディレクトリの作成と作業ディレクトリへの移動。

手順2.

% touch Dockerfile
% touch Gemfile
% touch Gemfile.lock
% touch entrypoint.sh
% touch docker-compose.yml

でDockerコンテナ作成のためのファイルを用意。

手順3.

先程作成した5つのファイルに設定を記述していく。
実際はdockerの公式をコピペすればよい。
まずはDockerfileから
公式から引っ張ってこれば良いがこれに関しては追記が必要。

Dockerfile
# syntax=docker/dockerfile:1
FROM ruby:2.5
RUN apt-get update -qq && apt-get install -y nodejs postgresql-client
# 以下追記
RUN apt-get update && apt-get install -y curl apt-transport-https wget && \
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 && \
apt-get update && apt-get install -y yarn
RUN curl -sL https://deb.nodesource.com/setup_7.x | bash - && \
apt-get install nodejs
# ここまで

WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle install

# Add a script to be executed every time the container starts.
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000

# Configure the main process to run when running the image
CMD ["rails", "server", "-b", "0.0.0.0"]

以上の様に記入。
続いて、Gemfile

Gemfile
source 'https://rubygems.org'
gem 'rails', '~>5'

Gemfile.lockには何も書かない。
次に、entrypoint.sh

entrypoint.sh
#!/bin/bash
set -e

# Remove a potentially pre-existing server.pid for Rails.
rm -f /myapp/tmp/pids/server.pid

# Then exec the container's main process (what's set as CMD in the Dockerfile).
exec "$@"

次に、docker-compose.yml

docker-compose.yml
version: "3.9"
services:
  db:
    image: postgres
    volumes:
      - ./tmp/db:/var/lib/postgresql/data
    environment:
      POSTGRES_PASSWORD: password
  web:
    build: .
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    volumes:
      - .:/myapp
    ports:
      - "3000:3000"
    depends_on:
      - db

手順4.

設定の作成が終わったのでrails newをしていく
ターミナルにて、

% docker-compose run --no-deps web rails new . --force --database=postgresql

を実行。
次々とファイルが作成されていく。
終わったら次のステップへGo

手順5.

ターミナルにて、

docker-compose build

でコンテナの作成。
続いて、databaseとの接続
rails newした時にできたconfig/database.ymlを

config/database.yml
default: &default
  adapter: postgresql
  encoding: unicode
  host: db
  username: postgres
  password: password
  pool: 5

development:
  <<: *default
  database: myapp_development


test:
  <<: *default
  database: myapp_test

に書き換える。

手順6.

最後にターミナルで

% docker-compose up

を実行。
自分はこの段階でエラーが発生したので、
https://qiita.com/kurawo___D/items/a6c5d5f71f2b5ac83b58
を参考にさせていただき、解決。

dockerの起動が終わったら、localhost:3000にアクセスして
image.png

が出れば成功!!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0