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"
ENV PATH $GEM_HOME/bin:$GEM_HOME/gems/bin:$PATH

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
# ENV NODE_VERSION 14.16.1
# RUN cd /opt && \
#     curl -L "https://nodejs.org/dist/v$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 https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
RUN echo "deb https://dl.yarnpkg.com/debian/ 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 entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
# Instructions

## Base files structure:

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

Then copy/paste the corresponding files below.

## Build the containers

```sh
docker-compose build
```

## Install rails

```sh
docker-compose run web bash
# you're now running a bash terminal inside the container
gem install rails
rails new .
exit
# 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 `0.0.0.0` (`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

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

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

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

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

Leave a Reply

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