As a memorandum of my own, I will leave an article on how to build an environment with Rails 6 + PostgreSQL with Docker. This time, we will create the app name as shopping_app.
 .
 ├── Dockerfile
 ├── docker-compose.yml
 └── shopping_app
     ├── Gemfile
     └── Gemfile.lock
First, create a Dockerfile.
FROM ruby:2.7.2
ENV LANG C.UTF-8
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 build-essential \
            libpq-dev \
            nodejs \
            postgresql-client yarn
RUN mkdir /app
RUN mkdir /app/shopping_app
ENV APP_ROOT /app/shopping_app
WORKDIR $APP_ROOT
ADD ./shopping_app/Gemfile $APP_ROOT/Gemfile
ADD ./shopping_app/Gemfile.lock $APP_ROOT/Gemfile.lock
RUN bundle install
ADD . $APP_ROOT
Create docker-compose.yml. This time the port is set to 1501.
version: '3'
services:
  postgres:
    image: postgres
    ports:
      - "3306:3306"
    volumes:
      - ./tmp/db:/var/lib/postgresql/data #For MacOS
    environment:
      POSTGRES_USER: 'admin'
      POSTGRES_PASSWORD: 'admin-pass'
    restart: always
  app:
    build: .
    image: rails
    container_name: 'app'
    command: bundle exec rails s -p 1501 -b '0.0.0.0'
    ports:
      - "1501:1501"
    environment:
      VIRTUAL_PORT: 80
    volumes:
      - ./shopping_app:/app/shopping_app
    depends_on:
      - postgres
    restart: always
volumes:
  app_postgre:
    external: true
source 'https://rubygems.org'
gem 'rails', '6.0.3'
You can leave Gemfile.lock empty.
$ docker-compose run app rails new . --force --database=postgresql --skip-bundle
docker-compose run app rails webpacker:install
Set the database.yml of the created app.
default: &default
  adapter: postgresql
  encoding: unicode
  # For details on connection pooling, see Rails configuration guide
  # https://guides.rubyonrails.org/configuring.html#database-pooling
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  #Describe the following
  user: admin
  password: admin-pass
  host: postgres
docker-compose build
docker-compose run app rails db:create
This completes the construction. Launch the app with the following command
docker-compose up
http://localhost:1501/ If you access, it will be displayed on the following screen.

that's all.
Recommended Posts