Using docker and docker-compose for Ruby on Rails development with PostgreSQL

Home   »   Using docker and docker-compose for Ruby on Rails development with PostgreSQL

FROM ruby:3.0.1

RUN apt-get update

ENV GEM_HOME="/usr/src/app/bundle"

RUN apt-get update -qq

RUN apt-get install postgresql-client

# Install NodeJS
# If you don't neet any specific NodeJS version use the Ubuntu package
RUN apt-get install -y nodejs
# If you'd like to install a specific NodeJS version, comment the line above and
# uncomment the lines below
# RUN apt-get install apt-transport-https
# RUN cd /opt && \
#     curl -L "$NODE_VERSION/node-v$NODE_VERSION-linux-x64.tar.xz" | tar -xJf - && \
#     mv -v node-v$NODE_VERSION-linux-x64 node
# ENV PATH /node_modules/.bin:/opt/node/bin:$PATH

# Install yarn
RUN curl -sS | apt-key add -
RUN echo "deb stable main" | tee /etc/apt/sources.list.d/yarn.list
RUN apt-get update -qq
RUN apt-get install -y yarn

WORKDIR /usr/src/app

# Add a script to be executed every time the container starts.
COPY /usr/bin/
RUN chmod +x /usr/bin/
# Instructions

## Base files structure:

mkdir my-new-app
cd my-new-app
touch Dockerfile
touch docker-compose.yml

Then copy/paste the corresponding files below.

## Build the containers

docker-compose build

## Install rails

docker-compose run web bash
# you're now running a bash terminal inside the container
gem install rails
rails new .
# you're now out of the container

## Update webpack config file

In `config/webpacker.yml` replace the key `development/dev_server/host` with the value `` (`localhost` is the default value and we don't want it).

## Update database.yml file

Replace your default database.yml file with the one provided here. 

## Launch local stack

docker-compose up
default: &default
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

  <<: *default
  url: <%= ENV['DATABASE_URL'] %>

  <<: *default
  url: <%= ENV['DATABASE_URL_TEST'] %>

  <<: *default
  url: <%= ENV['DATABASE_URL'] %>

Leave a Reply

Your email address will not be published. Required fields are marked *