Template to create applications with Rails 7 and Docker (ruby-3.2.0)
Used 34 times
M
Marcelo Moraes
Usage
Run this command in your Rails app directory in the terminal:
rails app:template LOCATION="https://railsbytes.com/script/zr4smR"
Template Source
Review the code before running this template on your machine.
# Update .gitignore
file ".gitignore", <<-CODE
*.rbc
capybara-*.html
.rspec
/db/*.sqlite3
/db/*.sqlite3-journal
/db/*.sqlite3-[0-9]*
/public/system
/coverage/
/spec/tmp
*.orig
rerun.txt
pickle-email-*.html
# Ignore all logfiles and tempfiles.
/log/*
/tmp/*
!/log/.keep
!/tmp/.keep
/tmp/pids/*
!/tmp/pids
!/tmp/pids/.keep
# TODO Comment out this rule if you are OK with secrets being uploaded to the repo
config/initializers/secret_token.rb
config/master.key
# Only include if you have production secrets in this file, which is no longer a Rails default
# config/secrets.yml
# dotenv
.env
.env.test
## Environment normalization:
/.bundle
/vendor/bundle
# these should all be checked in to normalize the environment:
# Gemfile.lock, .ruby-version, .ruby-gemset
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
.rvmrc
# if using bower-rails ignore default bower_components path bower.json files
/vendor/assets/bower_components
*.bowerrc
bower.json
# Ignore pow environment settings
.powenv
# Ignore Byebug command history file.
.byebug_history
# Ignore node_modules
node_modules/
# Ignore precompiled javascript packs
/public/packs
/public/packs-test
/public/assets
# Ignore yarn files
/yarn-error.log
yarn-debug.log*
.yarn-integrity
# Ignore uploaded files in development
/storage/*
!/storage/.keep
/config/master.key
CODE
# Docker setup
run "touch .dockerignore"
# Setup Dockerfile
file 'Dockerfile', <<-CODE
FROM ruby:3.2.0-slim
RUN apt-get update -qq && apt-get install -yq --no-install-recommends \
build-essential \
gnupg2 \
less \
git \
libpq-dev \
postgresql-client \
libvips42 \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
ENV LANG=C.UTF-8 \
BUNDLE_JOBS=4 \
BUNDLE_RETRY=3
RUN gem update --system && gem install bundler
WORKDIR /usr/src/app
ENTRYPOINT ["./docker-entrypoint.sh"]
EXPOSE 3000
CMD ["bundle", "exec", "rails", "s", "-b", "0.0.0.0"]
CODE
# Setup docker-compose.yml
file 'docker-compose.yml', <<-CODE
services:
web:
build: .
command: bash -c "rm -f tmp/pids/server.pid && bin/rails s -p 3000 -b '0.0.0.0'"
stdin_open: true
tty: true
volumes:
- .:/usr/src/app
- bundle:/usr/local/bundle
ports:
- "3000:3000"
env_file:
- ${ENV_FILE:-.env}
environment:
- HISTFILE=/usr/src/app/log/.bash_history
depends_on:
- db
db:
image: postgres:15
ports:
- "5432:5432"
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
volumes:
- pg_data:/var/lib/postgresql/data
volumes:
pg_data:
bundle:
CODE
file "docker-entrypoint.sh", <<-CODE
#!/bin/bash
set -e
# Remove a potentially pre-existing server.pid for Rails.
rm -f /usr/src/app/tmp/pids/server.pid
echo "bundle install..."
bundle check || bundle install --jobs 4
# Then exec the container's main process (what's set as CMD in the Dockerfile).
exec "$@"
CODE
run "chmod +x docker-entrypoint.sh"
# Change database configuration to consider DATABASE_URL
database = ask("What do you want to call the database?")
database = 'database' if database.empty?
file ".env.template", <<-CODE
RAILS_ENV=development
DATABASE_URL=postgresql://postgres:postgres@db/#{database}_development
CODE
file ".env.test.template", <<-CODE
RAILS_ENV=test
DATABASE_URL=postgresql://postgres:postgres@db/#{database}_test
DATABASE_CLEANER_ALLOW_REMOTE_DATABASE_URL=true
CODE
# Setup Docker Container
if yes?("Build container? (y/n)")
run "cp .env.template .env"
run "cp .env.test.template .env.test"
run "docker compose build --no-cache"
end
# Setup database
if yes?("Run database migrations inside container (y/n)?")
run "docker compose run web rails db:create db:migrate"
run "ENV_FILE=.env.test docker compose run web rails db:create db:migrate"
end