Skip to content

Instantly share code, notes, and snippets.

@yannski
Last active May 5, 2021 09:33
Show Gist options
  • Save yannski/f778946975a0649803901b5a27df2e50 to your computer and use it in GitHub Desktop.
Save yannski/f778946975a0649803901b5a27df2e50 to your computer and use it in GitHub Desktop.
Using docker and docker-compose for Ruby on Rails development with PostgreSQL

Instructions

Setup Base file structure:

Create the directory that will contain your project:

mkdir my-new-app
cd my-new-app

Create 3 blank files:

touch Dockerfile
touch docker-compose.yml
touch entrypoint.sh

Then copy/paste the corresponding files below.

You may want to replace my_new_app by the name of your app in the file docker-compose.yml.

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 -d postgresql .
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.

Update gitignore

echo "/docker" >> .gitignore
echo "/bundle" >> .gitignore

Launch local stack

docker-compose up

Create database

docker-compose run web bundle exec rails db:create
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'] %>
version: "2"
services:
web:
build:
context: .
args:
USER_ID: ${USER_ID:-1000}
GROUP_ID: ${GROUP_ID:-1000}
volumes:
- .:/usr/src/app:rw
environment:
DATABASE_URL: postgresql://admin:admin-secret@postgresql:5432/my_new_app_development
DATABASE_URL_TEST: postgresql://admin:admin-secret@postgresql:5432/my_new_app_test
WEBPACKER_DEV_SERVER_HOST: 172.17.0.1
links:
- postgresql
- webpack
ports:
- 3000:3000
command: bundle exec rails server -b 0.0.0.0 -p 3000 -e development
stdin_open: true
tty: true
webpack:
build:
context: .
args:
USER_ID: ${USER_ID:-1000}
GROUP_ID: ${GROUP_ID:-1000}
ports:
- 3035:3035
volumes:
- ./:/usr/src/app
command: ./bin/webpack-dev-server
environment:
WEBPACK_HOST: "172.17.0.1:3035"
specs:
build:
context: .
args:
USER_ID: ${USER_ID:-1000}
GROUP_ID: ${GROUP_ID:-1000}
volumes:
- ./:/usr/src/app/
environment:
DATABASE_URL_TEST: postgresql://admin:admin-secret@postgresql:5432/my_new_app_test
RAILS_ENV: test
links:
- postgresql
command: "tail -F /dev/null"
postgresql:
image: scalingo/postgresql:12.2.0-6
ports:
- 5432:5432
environment:
DB_USER_ID: 1000
volumes:
- ./docker/postgresql-data:/var/lib/postgresql:rw
command: /postgresql
FROM ruby:3.0.1
ARG USER_ID
ARG GROUP_ID
RUN addgroup --gid $GROUP_ID user
RUN adduser --disabled-password --gecos '' --uid $USER_ID --gid $GROUP_ID user
RUN apt-get update -qq
ENV GEM_HOME="/usr/src/app/bundle"
ENV PATH $GEM_HOME/bin:$GEM_HOME/gems/bin:$PATH
RUN apt-get update -qq
# PostgreSQL client lib
RUN apt-get install libpq-dev
# Install NodeJS
RUN apt-get install -y nodejs
# 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
USER $USER_ID
ENTRYPOINT ["entrypoint.sh"]
#!/bin/sh
set -e
if [ -f tmp/pids/server.pid ]; then
rm tmp/pids/server.pid
fi
exec $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment