LoginSignup
4
0

More than 1 year has passed since last update.

Docker + Rails7 + bootstrap5

Last updated at Posted at 2022-02-21

Docker で Rails7 の環境を作ったときのメモ。
欲張って Bootstrap5 も入れてみた。

環境

  • WSL2
  • Ruby 3.1.1
  • Ruby on Rails 7.0.2
  • PostgreSQL 14.2
  • Bootstrap 5
  • Docker 20.10.10
  • Docker Compose 2.2.1

用意するファイル

Gemfile.lock は空のファイルを用意する。
あと asdf を使っているなら .tool-versions もあるといいかもしれない。

Dockerfile
FROM ruby:3.1.1

RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -\
    && echo 'deb http://dl.yarnpkg.com/debian/ stable main' > /etc/apt/sources.list.d/yarn.list

RUN curl -sL https://deb.nodesource.com/setup_16.x | bash -
RUN apt update -qq && apt install -y nodejs build-essential postgresql-client yarn \
    curl dirmngr apt-transport-https lsb-release ca-certificates

ENV APP_ROOT /app
RUN mkdir ${APP_ROOT}
WORKDIR ${APP_ROOT}
ADD ./Gemfile ${APP_ROOT}/Gemfile
ADD ./Gemfile.lock ${APP_ROOT}/Gemfile.lock

RUN bundle install

ADD . ${APP_ROOT}
Gemfile
source 'https://rubygems.org'
gem 'rails'
Gemfile.lock
docker-compose.yml
version: '3'
services:
  db:
    image: postgres:14.2
    volumes:
      - ./tmp/db:/var/lib/postgresql/data
    environment:
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_USER=postgres
  web:
    build: .
    environment:
      RAILS_ENV: development
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    volumes:
      - .:/app
    ports:
      - '3000:3000'
    depends_on:
      - db

Rails アプリ作成

rails new のときに --css=bootstrap を付けると、Bootstrap がいっしょにインストールされる。

docker compose build
docker compose run --rm --no-deps web rails new . --force --css=bootstrap -d=postgresql

package.json 編集

Docker で rails new すると package.json に入れるべき内容が落ちてしまうようなので、自分で scripts を付け足してやる必要がある。

package.json
{
  ...
  "scripts": {
    "build": "esbuild app/javascript/*.* --bundle --sourcemap --outdir=app/assets/builds",
    "build:css": "sass ./app/assets/stylesheets/application.bootstrap.scss ./app/assets/builds/application.css --no-source-map --load-path=node_modules"
  },
  ...
}

database.yml 編集

host, username, password の 3 行を追加する。

config/database.yml
default: &default
  adapter: postgresql
  encoding: unicode
  host: db           # 追加
  username: postgres # 追加(docker-compose.ymlに設定したのと同じ値にする)
  password: postgres # 追加(docker-compose.ymlに設定したのと同じ値にする)
  # For details on connection pooling, see Rails configuration guide
  # https://guides.rubyonrails.org/configuring.html#database-pooling
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

bundle install

DB を作成しようとしたら Run `bundle install` to install missing gems. というエラーが出たので、もう一度 build する。

docker compose build

DB 作成

docker compose run --rm web rake db:create

CSS precompile

Sprockets::Rails::Helper::AssetNotFound in Users#index
The asset "application.css" is not present in the asset pipeline.
というエラーが出るので precompile してやる。

docker compose run --rm web rails assets:precompile
4
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
4
0