Skip to content

Instantly share code, notes, and snippets.

@yannski
Last active May 4, 2021 14:40
Show Gist options
  • Save yannski/8504cc6e77bf19a82219e07526b888de to your computer and use it in GitHub Desktop.
Save yannski/8504cc6e77bf19a82219e07526b888de to your computer and use it in GitHub Desktop.
Using docker and docker-compose for Ruby on Rails development, no database version

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.

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 .
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 gitignore

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

Launch local stack

docker-compose up
version: "2"
services:
web:
build:
context: .
args:
USER_ID: ${USER_ID:-1000}
GROUP_ID: ${GROUP_ID:-1000}
volumes:
- .:/usr/src/app:rw
environment:
WEBPACKER_DEV_SERVER_HOST: 172.17.0.1
links:
- 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:
RAILS_ENV: test
command: "tail -F /dev/null"
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
# 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