[RUBY] Docker x Rails 6 (memo)

Advance preparation

Environment

Prepare Docker related files

FROM ruby:2.6.3-alpine

ENV LANG=ja_JP.UTF-8
ENV TZ=Asia/Tokyo
ENV ROOT=/myapp \
    GEM_HOME=/bundle \
    BUNDLE_PATH=$GEM_HOME
ENV BUNDLE_BIN=$BUNDLE_PATH/bin
ENV PATH /app/bin:$BUNDLE_BIN:$PATH


WORKDIR $ROOT

RUN apk update && \
    apk upgrade && \
    apk add --no-cache \
        gcc \
        g++ \
        libc-dev \
        libxml2-dev \
        linux-headers \
        make \
        nodejs \
        postgresql \
        postgresql-dev \
        tzdata \
        imagemagick \
        yarn && \
    apk add --virtual build-packs --no-cache \
        build-base \
        curl-dev

COPY Gemfile $ROOT
COPY Gemfile.lock $ROOT

RUN bundle install -j4
#Delete unnecessary files
RUN rm -rf /usr/local/bundle/cache/* /usr/local/share/.cache/* /var/cache/* /tmp/* && \
apk del build-packs

COPY . $ROOT

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

docker-compose.yml


version: "3.8"

services:
  db:
    image: postgres:11.0-alpine
    volumes:
      - postgres:/var/lib/postgresql/data:cached
    ports:
      - "5432:5432"
    environment:
      PGDATA: /var/lib/postgresql/data/pgdata
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: password
      POSTGRES_INITDB_ARGS: "--encoding=UTF-8 --locale=ja_JP.UTF-8"
      TZ: Asia/Tokyo
  app:
    build: .
    command: ash -c "rm -f tmp/pids/server.pid && ./bin/rails s -p 3000 -b '0.0.0.0'"
    volumes:
      - .:/myapp:cached
      - rails_cache:/myapp/tmp/cache
      - node_modules:/myapp/node_modules:cached
      - bundle:/bundle:cached
    tmpfs:
      - /tmp
    tty: true
    stdin_open: true
    ports:
      - "3000:3000"
    environment:
      RAILS_ENV: development
      NODE_ENV: development
      DATABASE_HOST: db
      DATABASE_PORT: 5432
      DATABASE_USER: postgres
      DATABASE_PASSWORD: password
      WEBPACKER_DEV_SERVER_HOST: webpacker
    depends_on:
      - db
      - webpacker

  webpacker:
    build: .
    command: ./bin/webpack-dev-server
    volumes:
      - .:/myapp:cached
      - node_modules:/myapp/node_modules:cached
    environment:
      RAILS_ENV: development
      NODE_ENV: development
      WEBPACKER_DEV_SERVER_HOST: 0.0.0.0
    tty: false
    stdin_open: false
    ports:
      - "3035:3035"

volumes:
  rails_cache:
  node_modules:
  postgres:
  bundle:
source 'https://rubygems.org'
gem 'rails',      '6.0.3'
gem 'devise'

# to upload images
gem 'carrierwave', '~> 2.0'
gem "mini_magick"

Gemfile.lock(empry)




#### **`entrypoint.sh`**
```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 "$@"

cmd

docker-compose run app rails new . --force --no-deps --database=postgresql --skip-bundle

Edit the Gemfile

source 'https://rubygems.org'
 git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.6.3'

 Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 6.0.3', '>= 6.0.3.4'
 Use postgresql as the database for Active Record
gem 'pg', '>= 0.18', '< 2.0'
 Use Puma as the app server
gem 'puma', '~> 4.1'
 Use SCSS for stylesheets
gem 'sass-rails', '>= 6'
 Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker
gem 'webpacker', '~> 4.0'
 Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
 Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.7'

gem 'devise'
 Use Redis adapter to run Action Cable in production
 gem 'redis', '~> 4.0'
 Use Active Model has_secure_password
 gem 'bcrypt', '~> 3.1.7'

 Use Active Storage variant
 gem 'image_processing', '~> 1.2'

 Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.4.2', require: false

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
end

group :development do
  # Access an interactive console on exception pages or by calling 'console' anywhere in the code.
  gem 'web-console', '>= 3.3.0'
  gem 'listen', '~> 3.2'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
end

group :test do
  # Adds support for Capybara system testing and selenium driver
  gem 'capybara', '>= 2.15'
  gem 'selenium-webdriver'
  # Easy installation and use of web drivers to run system tests with browsers
  gem 'webdrivers'
end

 Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

 to upload images
gem 'carrierwave', '~> 2.0'

gem "mini_magick"

Subsequent commands

docker-compose run app bundle update
docker-compose run app rails webpacker:install

database.Fix yml

config/database.yml


default: &default
  adapter: postgresql
  encoding: unicode
  host: db
  username: postgres
  password: password
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
...

Troubleshooting

check when up_yarn_If you get an integrity related error, webpacker.Fix yml

config/webpacker.yml


...
development:
  <<: *default
  compile: true 

  # Verifies that correct packages and versions are installed by inspecting package.json, yarn.lock, and node_modules
 check_yarn_integrity: false # true-> changed to false
...
docker-compose build
docker-compose up -d
docker-compose run app rake db:create

Also when up"webpack-dev-server"If not found appears

docker-compose run app yarn add webpack-dev-server

Recommended Posts

Docker x Rails 6 (memo)
docker memo
docker tutorial (memo)
Docker operation memo
Docker command memo
[Memo] docker summary
Rails Docker ~ Part 1 ~
Rails Docker ~ Part 2 ~
Rails Docker environment construction
Introducing Rspec with Ruby on Rails x Docker
Rails deploy with Docker
docker inspect --format memo
Rails + ElasticSearch Survey Memo
Docker Machine command memo
MEMO about Rails 6 series
Build Rails (API) x MySQL x Nuxt.js environment with Docker
MySQL 5.7 (Docker) environment construction memo
[Vscode xdebug3.0 x PHP x Docker]
Run Rails whenever with docker
Redmine (Docker) environment construction memo
[Docker] Rails 5.2 environment construction with docker
[Docker] Use whenever with Docker + Rails
Installing and building Docker (memo)
Rails + MySQL environment construction with Docker
[Devise] rails memo basic setting initial setting
Build environment with vue.js + rails + docker
Docker + rails + Vue.js localhost connection refused
Build Rails environment with Docker Compose
[My style] rails Initial setting memo
Build Clang x VSCode on Docker (1)
Ruby on Rails 6.0 environment construction memo
Rails on Docker environment construction procedure
[Environment construction with Docker] Rails 6 & MySQL 8
Deploy Rails on Docker to heroku
Docker execution memo summarized for myself
"Rails 6 x MySQL 8" Docker environment construction procedure for sharing with teams
[Rails] Comment function implementation procedure memo
Rails Tutorial (4th Edition) Memo Chapter 6
docker
[Rails API x Docker] Easy environment construction with shell & operation check with Flutter
How to build a Ruby on Rails development environment with Docker (Rails 6.x)
How to build a Ruby on Rails development environment with Docker (Rails 5.x)
Deployed using Docker + Rails + AWS (EC2 + RDS)
Ruby on Rails6 Practical Guide cp13 ~ cp15 [Memo]
Rails environment construction with Docker (personal apocalypse)
Building Rails 6 and PostgreSQL environment with Docker
Create Rails 6 + MySQL environment with Docker compose
[Rails] How to use rails console with docker
Migrate existing Rails 6 apps to Docker environment
Deploy to heroku with Docker (Rails 6, MySQL)
Ruby on Rails6 Practical Guide cp7 ~ cp9 [Memo]
[Docker] Development environment construction Rails6 / Ruby2.7 / MySQL8
[Rails] [Memo] When to add = to <%%> and when not
Ruby on Rails6 Practical Guide cp4 ~ cp6 [Memo]
[Rails 5.x] How to introduce free fonts
Show Better Errors in Rails + Docker environment
Install Docker with WSL2 Memo ([Part 2] Docker introduction)
Ruby on Rails6 Practical Guide cp10 ~ cp12 [Memo]
Ruby on Rails6 Practical Guide cp16 ~ cp18 [Memo]
CentOS8.2 (x86_64) + ruby2.5 + Rails5.2 + MariaDB (10.3.17) environment construction
How to build Rails 6 environment with Docker