Skip to content

Instantly share code, notes, and snippets.

@codealchemy
Last active September 7, 2018 18:56
Show Gist options
  • Select an option

  • Save codealchemy/b1c49d716ac3babb73b0d8e00917b4ac to your computer and use it in GitHub Desktop.

Select an option

Save codealchemy/b1c49d716ac3babb73b0d8e00917b4ac to your computer and use it in GitHub Desktop.
Basic Rails API template with Docker
remove_file "Gemfile"
run "touch Gemfile"
add_source 'https://rubygems.org'
gem 'bootsnap', require: false
gem "health_check"
gem 'rails', '~> 5.2'
gem 'puma'
gem 'pg'
gem_group :development, :test do
gem 'pry-rails'
gem 'rspec-rails', require: false
end
create_file "Dockerfile" do <<-EOF
FROM ruby:2.5.1
RUN apt-get update -qq && \
apt-get install -y \
build-essential \
libpq-dev \
nodejs
EXPOSE 3000
RUN mkdir /app
ADD . /app
WORKDIR /app
RUN bundle install
CMD ["rails", "server", "-b", "0.0.0.0"]
EOF
end
create_file "docker-compose.yml" do <<-EOF
version: '3'
services:
db:
image: postgres
volumes:
- ./tmp/db:/var/lib/postgresql/data
api:
build: .
volumes:
- .:/app
depends_on:
- db
ports:
- "3000:3000"
EOF
end
inside 'config' do
remove_file 'database.yml'
create_file 'database.yml' do <<-EOF
default: &default
adapter: postgresql
host: db
port: 5432
pool: 5
timeout: 5000
user: postgres
password: postgres
development:
<<: *default
database: #{app_name}_development
test:
<<: *default
database: #{app_name}_test
production:
<<: *default
database: #{app_name}_production
EOF
end
end
after_bundle do
generate "rspec:install"
git :init
git add: "."
git commit: "-a -m 'Initial commit'"
end
@codealchemy
Copy link
Author

codealchemy commented Sep 7, 2018

Steps for using this template:

Create the Rails app

> rails new sample_app --skip-spring --skip-listen --database=postgresql --api -T -M -m https://gist.githubusercontent.com/codealchemy/b1c49d716ac3babb73b0d8e00917b4ac/raw/460e416e56e7a37674ca81a734910a33c7043a53/pg_api_docker_template.rb

cd into the new app directory and start up docker

> cd sample_app
> docker-compose up -d

Once that's done, go into the container and create the development database

> docker-compose run api bash
api > bundle exec rake db:create

Then head to localhost:3000 in your browser and you should be good to go!

📝 To close everything down

> docker-compose down --remove-orphans

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment