Skip to content

Instantly share code, notes, and snippets.

@Hunk13
Created February 18, 2022 14:09
Show Gist options
  • Select an option

  • Save Hunk13/eda27d44f5f40d9c18c91c1f6a4466ad to your computer and use it in GitHub Desktop.

Select an option

Save Hunk13/eda27d44f5f40d9c18c91c1f6a4466ad to your computer and use it in GitHub Desktop.
  1. Create simple Gemfile
source 'https://rubygems.org'
gem 'rails', '~> 6.1`
  1. Create empty Gemfile.lock
touch Gemfile.lock
  1. Create Dockerfile
FROM ruby:2.6.8

RUN apt-get update -qq && apt-get install -y build-essential libpq-dev
RUN curl -sL https://deb.nodesource.com/setup_14.x | bash - && \
    apt-get install -y -qq nodejs && npm install --global yarn

RUN mkdir /app
WORKDIR /app

ADD Gemfile /app/Gemfile
ADD Gemfile.lock /app/Gemfile.lock

RUN bundle install

ADD . /app
  1. Create docker-compose.yml
version: '3.3'

services:
  db:
    image: postgres:12-alpine
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: app_database
      POSTGRES_HOST_AUTH_METHOD: trust
    ports:
      - 5432:5433
  app:
    build: .
    command: bundle exec rails s -p 3000 -b 0.0.0.0
    environment:
      POSTGRES_HOST: db
    volumes:
      - '.:/app'
    ports:
      - 3001:3000
    depends_on:
      - db
    links:
      - db
  1. Inside folder run command
docker-compose run app rails new . --force --database=postgresql --skip-bundle
  1. After creating app edit config/database.yml
default: &default
  adapter: postgresql
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development:
  <<: *default
  database: <%= ENV['PG_DB'] %>
  username: <%= ENV['PG_USER'] %>
  password: <%= ENV['PG_PASSWORD'] %>
  host: <%= ENV['PG_HOST'] %>

test:
  <<: *default
  database: app_test

production:
  <<: *default
  database: app_production
  username: app
  password: <%= ENV['APP_DATABASE_PASSWORD'] %>
  1. Add environment variables to docker-compose.yml in app: part
environment:
  PG_DB: app_database
  PG_USER: postgres
  PG_PASSWORD: postgres
  PG_HOST: db
  1. Run docker-compose build. This install all gems. NOTE: If there are some problems with rights need to run command sudo chown -R $USER:$USER /path/to/app

  2. Run migrations, generators, etc. For example run migrations.

docker-compose run --rm app rake db:migrate
  1. Install webpacker
docker-compose run --rm app rails webpacker:install
  1. Up application.
docker-compose up

and open page http://localhost:3001

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment