Skip to content

Instantly share code, notes, and snippets.

@exegeteio
Created July 24, 2021 14:36
Show Gist options
  • Save exegeteio/e1455c587c8518a7e6f42b3b254874e6 to your computer and use it in GitHub Desktop.
Save exegeteio/e1455c587c8518a7e6f42b3b254874e6 to your computer and use it in GitHub Desktop.
Personal Rails Docker Setup
# See https://help.github.com/articles/ignoring-files for more about ignoring files.
#
# If you find yourself ignoring temporary files generated by your text editor
# or operating system, you probably want to add a global ignore instead:
# git config --global core.excludesfile '~/.gitignore_global'
# Ignore bundler config.
/.bundle
# Ignore the default SQLite database.
/db/*.sqlite3
/db/*.sqlite3-journal
/db/*.sqlite3-*
# Ignore all logfiles and tempfiles.
/log/*
/tmp/*
!/log/.keep
!/tmp/.keep
# Ignore pidfiles, but keep the directory.
/tmp/pids/*
!/tmp/pids/
!/tmp/pids/.keep
# Ignore uploaded files in development.
/storage/*
!/storage/.keep
/public/assets
.byebug_history
# Ignore master key for decrypting credentials and more.
/config/master.key
vendor/bundle
/public/packs
/public/packs-test
/node_modules
/yarn-error.log
yarn-debug.log*
.yarn-integrity
# Aliases for running commands within the app.
alias rails="docker-compose run --rm web bundle exec rails"
alias bundle="docker-compose run --rm web bundle"
alias rake="docker-compose run --rm web bundle exec rake"
alias start="docker-compose up --build --force-recreate"
version: '3.8'
services:
postgres:
image: postgres:9
environment:
POSTGRES_USER: 'root'
POSTGRES_PASSWORD: 'abc123'
volumes:
- pg-data:/var/lib/postgresql/data
redis:
image: redis
web:
build:
context: ./
target: rails-builder
stdin_open: true
tty: true
ports:
- "${PORT:-3000}:5000"
volumes:
- ./:/app/
depends_on:
- postgres
- redis
links:
- postgres
- redis
environment:
DATABASE_URL: postgres://postgres:postgres@postgres:5432/
DATABASE_CLEANER_ALLOW_REMOTE_DATABASE_URL: "true"
REDIS_URL: redis://redis:6379
restart: always
volumes:
pg-data:
#### Base rails image, used for `rails new` and other commands.
FROM ruby:2.7-alpine AS rails
RUN apk add --update --no-cache --quiet \
build-base \
nodejs-current \
postgresql-dev \
sqlite-dev \
tzdata \
yarn
RUN echo -e "source 'https://rubygems.org'\ngem 'rails'" > /Gemfile
RUN bundle install --quiet
FROM rails as rails-builder
RUN apk add --update --no-cache --quiet \
build-base \
nodejs-current \
postgresql-dev \
sqlite-dev \
tzdata \
yarn
WORKDIR /app/
COPY Gemfile Gemfile.lock /app/
RUN bundle config --global frozen 1 \
&& bundle install --quiet -j4 --retry 3 \
# Remove unneeded files (cached *.gem, *.o, *.c)
&& rm -rf /usr/local/bundle/cache/*.gem \
&& find /usr/local/bundle/gems/ -name "*.c" -delete \
&& find /usr/local/bundle/gems/ -name "*.o" -delete
EXPOSE 5000
CMD bundle exec rails server -b 0.0.0.0 -p 5000
#### Builder is used to build assets and delete side effects.
FROM rails-builder AS builder
COPY Gemfile Gemfile.lock /app/
RUN rm -rf /usr/local/bundle \
&& bundle config --global frozen 1 \
&& bundle config set without 'development test' \
&& bundle install --quiet -j4 --retry 3 \
# Remove unneeded files (cached *.gem, *.o, *.c)
&& rm -rf /usr/local/bundle/cache/*.gem \
&& find /usr/local/bundle/gems/ -name "*.c" -delete \
&& find /usr/local/bundle/gems/ -name "*.o" -delete
COPY package.json yarn.lock /app/
RUN yarn install
COPY ./ /app/
RUN RAILS_ENV=production SECRET_KEY_BASE=not_for_prod bundle exec rake assets:precompile
RUN rm -rf node_modules tmp/cache app/assets vendor/assets lib/assets spec
# Final image
FROM ruby:2.7-alpine AS final
# Set Rails env
ENV RAILS_LOG_TO_STDOUT true
ENV RAILS_SERVE_STATIC_FILES true
ENV EXECJS_RUNTIME Disabled
RUN apk add --update --no-cache postgresql-client
# Add user
RUN addgroup -g 1000 -S app \
&& adduser -u 1000 -S app -G app
USER app
WORKDIR /app
# Copy app with gems from former build stage
COPY --from=builder /usr/local/bundle/ /usr/local/bundle/
COPY --from=builder --chown=app:app /app /app
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment