Skip to content

Instantly share code, notes, and snippets.

@iEv0lv3
Last active May 11, 2021 11:33
Show Gist options
  • Save iEv0lv3/17e33bc8d3ad4132d317ba5a1cf62d9b to your computer and use it in GitHub Desktop.
Save iEv0lv3/17e33bc8d3ad4132d317ba5a1cf62d9b to your computer and use it in GitHub Desktop.
Docker setup for Rails and PostgreSQL

Docker :: Rails

Setup Process

Step 1 :: Install Docker Desktop

Step 2 :: Add or Edit files

  • Add or edit the files listed below in the Files section
  • Replace my_app with your application root directory name

Step 3 :: Disable Yarn Integrity

  • Path
    • config/environments/development.rb
  • Command to Add
    • config.webpacker.check_yarn_integrity = false
  • Result
    • Solves yarn not found error

Step 4 :: Build Docker Images

  • Build the application in Docker
    • Ensure your current terminal location is the my_app project root directory
    • $ docker-compose build
  • When build is complete activate the application containers in Docker
    • $ docker-compose up
  • Create the PostgreSQL database and run migrations. Open a new terminal
    • $ docker-compose run web rake db:create db:migrate

Step 5 :: RSpec Tests

  • The application should be running locally on Docker. If not please check the steps above, or start them with
    • docker-compose up
  • In a new terminal run this command
    • docker-compose exec web rspec
  • Or this alternative command (if necessary)
    • docker-compose run -e "RAILS_ENV=test" web rspec
  • Result
    • Solves not being able to run RSpec tests using bundle exec rspec or rspec
    • This will run RSpec tests using the active app and db containers

Step 6 (optional)

Files

Dockerfile :: Add to my_app root directory

# Replace Ruby version below with your application's Ruby version. Delete this comment.
FROM ruby:2.7.1
RUN apt-get update -qq && apt-get install -y nodejs postgresql-client
RUN mkdir /my_app
WORKDIR /my_app
COPY Gemfile /my_app/Gemfile
COPY Gemfile.lock /my_app/Gemfile.lock
RUN bundle install
COPY . /my_app

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

# Start the main process.
CMD ["rails", "server", "-b", "0.0.0.0"]

docker-compose.yml :: Add to my_app root directory

version: '3.1'
services:
  db:
    image: postgres
    restart: always
    volumes:
      - ./tmp/db:/var/lib/postgresql/data
    environment:
      POSTGRES_PASSWORD: password
  web:
    build: .
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    volumes:
      - .:/my_app
    ports:
      - "3000:3000"
    depends_on:
      - db

database.yml :: Edit file located at config/database.yml

default: &default
  adapter: postgresql
  encoding: unicode
  pool: 5
  username: postgres
  password: password
  host: db
  port: 5432

entrypoint.sh :: Add to my_app root directory

#!/bin/bash
set -e

# Remove a potentially pre-existing server.pid for Rails.
rm -f /my_app/tmp/pids/server.pid

# Then exec the container's main process (what's set as CMD in the Dockerfile).
exec "$@"
@iEv0lv3
Copy link
Author

iEv0lv3 commented Aug 31, 2020

With your bash command in docker-compose, you don't need the entrypoint.sh in my_app right? Seems like one or the other??? Fighting with similar issue.

@epugh, that is correct, and I need to update that line; great catch! I am now exclusively using an entrypoint.sh file in both development and production instead of a CMD line in the Dockerfile. I find using the bash script produces consistent results when I deploy to GCP Cloud Run and Kubernetes.

Copy link

ghost commented Mar 12, 2021

I was having issues with other guides on dockerising Rails but this one worked perfectly and was clear and easy to follow, thank you!

@zainonrails
Copy link

Hi @iEvolv3
great guide, just had a question as to why are we copying the gemfile and gemfile.lock separately and then copying the entire application directory? and same goes for `entry point file.
Why don't we just copy the app directory that contains all these files?

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