Last active
September 7, 2018 18:56
-
-
Save codealchemy/b1c49d716ac3babb73b0d8e00917b4ac to your computer and use it in GitHub Desktop.
Basic Rails API template with Docker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.rbcdinto the new app directory and start up dockerOnce that's done, go into the container and create the development database
Then head to
localhost:3000in your browser and you should be good to go!📝 To close everything down
> docker-compose down --remove-orphans