[Ruby] Build Docker + Rails6 + Postgresql development environment

1 minute read

Premise

We introduced Docker so that team members who do not have a Rails environment can easily run a project locally without having to worry about environment construction. Since I am building the Rails environment locally, I created the project locally so that both Docker and local can work. It is assumed that Docker is already installed.

Environment

  • macOS Catalina 10.15.4
  • Ruby 2.7.1
  • Rails 6.0.3.2
  • PostgreSQL 12.3
  • Docker version 19.03.12

Create new project

$ mkdir myapp; cd $_
$ rails new .--skip-coffee --skip-turbolinks --database=postgresql

Introducing dotenv-rails to manage environment variables

Depending on the environment variable, you can choose whether to run it with Docker or directly with local.

Gemfile


gem'dotenv-rails'

install gem

$ bundle install

The following is an example of environment variable settings when running with Docke.

.env


DATABASE_HOST=db
DATABASE_USER=postgres
DATABASE_PASSWORD=secret

If you want to run it locally, set DATABASE_HOST=localhost.

Create/edit files for Docker

docker-compose.yml


version: '3'
services:
  db:
    image: postgres
    volumes:
      -./tmp/db:/var/lib/postgresql/data
    ports:
      -"5433:5432"
    environment:
      POSTGRES_USER:'postgres'
      POSTGRES_PASSWORD:'secret'
      POSTGRES_DB:'db'

  webpacker:
    build:.
    command: bundle exec bin/webpack-dev-server
    volumes:
      -.:/myapp
    ports:
      -"8080:8080"

  web:
    build:.
    command: /bin/sh -c "rm -f /myapp/tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    volumes:
      -.:/myapp
    ports:
      -"3000:3000"
    depends_on:
      -db
      -webpacker

dockerfile


FROM ruby:2.7.1
 
ENV LANG C.UTF-8

# Install required libraries
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs

# yarn package management tool installation
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

# Work directory setting
RUN mkdir /myapp
WORKDIR /myapp
ADD Gemfile /myapp/Gemfile
ADD Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
COPY ./myapp
EXPOSE 3000

Add host, username, and passwrd settings to database.yml

/config/database.yml


: Omitted

default: &default
  adapter: postgresql
  encoding: unicode
  host: <%= ENV.fetch("DATABASE_HOST") {"127.0.0.1"} %> # Added
  username: <%= ENV.fetch("DATABASE_USER") {"postgres"} %> # Added
  password: <%= ENV.fetch("DATABASE_PASSWORD") {""} %> # Added
  # For details on connection pooling, see Rails configuration guide
  # https://guides.rubyonrails.org/configuring.html#database-pooling
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") {5} %>

: Omitted

image construction

$ docker-compose build

Package installation

$ docker-compose run --rm web bin/yarn install

Create database

$ docker-compose run --rm web rails db:create

Start-up

$ docker-compose up

You can access http://localhost:3000/.

If you want to move directly to the local

Edit .env and start it with yarn install, rails db:create, rails s.