Prepare the following files. After that, please change "app" as appropriate.
Dockerfile
FROM ruby:2.6.6
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 postgresql-client yarn && apt-get install -y vim
RUN mkdir /app
WORKDIR /app
COPY Gemfile /app/Gemfile
COPY Gemfile.lock /app/Gemfile.lock
RUN bundle install
COPY . /app
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"]
Gemfile
source 'https://rubygems.org'
gem 'rails', '6.0.3'
Gemfile.lock (empty)
entrypoint.sh
#!/bin/bash
set -e
rm -f /app/tmp/pids/server.pid
exec "$@"
docker-compose.yml
version: '3'
services:
  db:
    image: postgres
    volumes:
      - ./volumes/db/log:/var/log/postgresql
    environment:
      - POSTGRES_PASSWORD=password
  rails:
    build: .
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    volumes:
      - .:/app
    environment:
      - EDITOR=vim
    ports:
      - "3000:3000"
    depends_on:
      - db
Change the db password accordingly.
docker-compose run rails rails new . --force --database=postgresql
docker-compose build
config/database.yml
default: &default
  adapter: postgresql
  encoding: unicode
  host: db
  username: postgres
  password: password
  pool: 5
development:
  <<: *default
  database: app_development
test:
  <<: *default
  database: app_test
production:
  <<: *default
  database: app_production
  username: app
  password: <%= ENV['APP_DATABASE_PASSWORD'] %>
Change the db password accordingly. Keep in mind that it should be combined with docker-compose.yml.
docker-compose up -d
docker-compose run rails rails db:create
Access the following and if the usual "Yay! You're on Rails!" Is displayed, you're done.
http://localhost:3000
        Recommended Posts