├── .build-deps ├── Gemfile.lock ├── Gemfile ├── .env.dev ├── bin └── start_dev_server ├── .build-deps.sample ├── Dockerfile ├── docker-compose.yml ├── makefile └── README.md /.build-deps: -------------------------------------------------------------------------------- 1 | build-base 2 | -------------------------------------------------------------------------------- /Gemfile.lock: -------------------------------------------------------------------------------- 1 | GEM 2 | remote: https://rubygems.org/ 3 | specs: 4 | 5 | PLATFORMS 6 | ruby 7 | 8 | DEPENDENCIES 9 | 10 | BUNDLED WITH 11 | 1.17.2 12 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | source 'https://rubygems.org' 4 | 5 | git_source(:github) {|repo_name| 'https://github.com/#{repo_name}' } 6 | 7 | # gem 'rails' 8 | -------------------------------------------------------------------------------- /.env.dev: -------------------------------------------------------------------------------- 1 | DATABASE_PASSWORD=db_password 2 | DATABASE_HOST=db 3 | # Leave the relevant line only 4 | 5 | # For mariadb example provided 6 | # DATABASE_USERNAME=root 7 | 8 | # For postgres example provided 9 | # DATABASE_USERNAME=postgres 10 | -------------------------------------------------------------------------------- /bin/start_dev_server: -------------------------------------------------------------------------------- 1 | #!/bin/ash 2 | 3 | # In this file you need to add the commands needed to start your dev server 4 | 5 | # For Rails projects, you can use the following: 6 | rm -f tmp/pids/server.pid 7 | bundle exec rails server --binding 0.0.0.0 8 | -------------------------------------------------------------------------------- /.build-deps.sample: -------------------------------------------------------------------------------- 1 | # For most projects you need: 2 | build-base 3 | 4 | # For ruby projects with mysql2 gem add: 5 | mysql-dev 6 | 7 | # For ruby projects with pg gem add: 8 | postgresql-dev 9 | 10 | # Make sure to add any needed native library to make your bundle 11 | # installation works 12 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | # Make sure to lock your ruby version to a specific one 2 | FROM ruby:alpine AS dev 3 | COPY .build-deps / 4 | RUN cat .build-deps | xargs apk add 5 | WORKDIR /APP_NAME 6 | ENV BUNDLE_PATH=/bundle \ 7 | BUNDLE_BIN=/bundle/bin \ 8 | GEM_HOME=/bundle 9 | ENV PATH="${BUNDLE_BIN}:${PATH}" 10 | 11 | FROM dev AS ci 12 | COPY Gemfile Gemfile.lock ./ 13 | RUN bundle install --jobs 20 --retry 5 14 | COPY . ./ 15 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: "3.7" 2 | services: 3 | web: 4 | build: 5 | context: . 6 | target: dev 7 | env_file: 8 | - .env.dev 9 | stdin_open: true 10 | tty: true 11 | command: ./bin/start_dev_server 12 | volumes: 13 | - ".:/APP_NAME" 14 | - bundle:/bundle 15 | ports: 16 | - "3000:3000" 17 | # Uncomment this in case of db definition 18 | # Add any other dependencies 19 | # depends_on: 20 | # - db 21 | 22 | # For mariadb databases 23 | # Make sure to select the correct database version 24 | # db: 25 | # image: "mariadb:10.3" 26 | # environment: 27 | # MYSQL_ROOT_PASSWORD: db_password 28 | 29 | # For Postgres database 30 | # Make sure to select the correct database version 31 | # db: 32 | # image: "healthcheck/postgres:alpine" 33 | # environment: 34 | # POSTGRES_PASSWORD: db_password 35 | volumes: 36 | bundle: 37 | -------------------------------------------------------------------------------- /makefile: -------------------------------------------------------------------------------- 1 | dc=docker-compose -f docker-compose.yml $(1) 2 | dc-run=$(call dc, run --rm web $(1)) 3 | 4 | usage: 5 | @echo "Available targets:" 6 | @echo " * setup - Initiates everything (building images, installing gems, creating db and migrating" 7 | @echo " * build - Build image" 8 | @echo " * bundle - Install missing gems" 9 | @echo " * db-migrate - Runs the migrations for dev database" 10 | @echo " * db-test-migrate - Runs the migrations for test database" 11 | @echo " * dev - Fires a shell inside your container" 12 | @echo " * up - Runs the development server" 13 | @echo " * tear-down - Removes all the containers and tears down the setup" 14 | @echo " * stop - Stops the server" 15 | @echo " * rspec - Runs rspec" 16 | 17 | # Without db 18 | setup: build bundle 19 | 20 | # With db 21 | setup: build bundle db-create db-migrate db-test-migrate 22 | 23 | build: 24 | $(call dc, build) 25 | bundle: 26 | $(call dc-run, bundle install) 27 | dev: 28 | $(call dc-run, ash) 29 | up: 30 | $(call dc, up) 31 | tear-down: 32 | $(call dc, down) 33 | stop: 34 | $(call dc, stop) 35 | 36 | # For rails projects uncomment this 37 | # console: 38 | # $(call dc-run, bundle exec rails console) 39 | 40 | # For projects with databases and 41 | # db-create: 42 | # $(call dc-run, bundle exec rake db:create) 43 | # db-migrate: 44 | # $(call dc-run, bundle exec rake db:migrate) 45 | # db-test-migrate: 46 | # $(call dc-run, bundle exec rake db:migrate RAILS_ENV=test) 47 | # rspec: 48 | # $(call dc-run, bundle exec rspec) 49 | 50 | 51 | # .PHONY: test 52 | # test: 53 | # $(call dc-run, bundle exec rspec) 54 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Docker Ruby Boiler Plate 2 | 3 | ## Introduction 4 | 5 | The intention of this project is to provide an easy to use boiler plates with different ruby projects (Specifically web applications). 6 | 7 | ## Setup 8 | In order to setup this boilerplate: 9 | 10 | * Clone the repository. 11 | * Remove the local git repo with `rm -r .git` and initiate your own git repo for your project. 12 | * Replace the word `APP_NAME` with your application name in both `dockerfile` and `docker-compose.yml` 13 | * Alter `docker-compose.yml` to add your dependencies, there are examples there for mariadb and postgresql. 14 | * Add your gem dependencies to the gemfile. 15 | * Run `make setup` and add the needed build dependencies to your `.build-deps` file, you can check the `.build-deps.sample` file for examples. 16 | * Keep running `make setup` until your bundle installation works without errors. 17 | * Make sure your `start_dev_server` has the needed commands to start your development server. 18 | * Alter your `.env.dev` to supply the correct credentials for your database and use the correct hostname And make sure your application database connections set to use environmental variables 19 | * Alter `makefile` to enable the needed targets. 20 | 21 | 22 | ### Example setup of Rails application called test_app with mariadb from scratch: 23 | * Rename the folder to `test_app` after cloning 24 | * Replace the word `APP_NAME` in `dockerfile` and `docker-compose.yml` to `test_app` 25 | * Go to docker-compose.yml and uncomment mariadb services so it looks like this: 26 | ``` 27 | version: "3.7" 28 | services: 29 | web: 30 | build: 31 | context: . 32 | target: dev 33 | env_file: 34 | - .env.dev 35 | stdin_open: true 36 | tty: true 37 | command: ./bin/start_dev_server 38 | volumes: 39 | - ".:/test_app" 40 | - bundle:/bundle 41 | ports: 42 | - "3000:3000" 43 | depends_on: 44 | - db 45 | 46 | db: 47 | image: "mariadb:10.3" 48 | environment: 49 | MYSQL_ROOT_PASSWORD: db_password 50 | volumes: 51 | bundle: 52 | ``` 53 | 54 | * Add `rails` and `mysql2` to your gemfile. 55 | * Running `make setup` the bundle installation step will fail. 56 | * Edit your `.build-deps` file to have the needed build dependencies like this: 57 | ``` 58 | build-base 59 | mysql-dev 60 | nodejs 61 | tzdata 62 | ``` 63 | * Run make setup again and this time your bundle will install. 64 | * Run `make dev` to jump into the container shell. 65 | * Run `rails new . -d mysql` in the container shell to generate the rails project. Accept the overwrite in case of conflicts like gemfile or readme. 66 | * Alter your `.env.dev` to look as following: 67 | ``` 68 | DATABASE_PASSWORD=db_password 69 | DATABASE_HOST=db 70 | DATABASE_USERNAME=root 71 | 72 | ``` 73 | * Alter your database.yml to use env vars you want your defaults to look something like this: 74 | ``` 75 | default: &default 76 | adapter: mysql2 77 | encoding: utf8 78 | pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> 79 | username: <%= ENV.fetch("DATABASE_USERNAME") { root } %> 80 | password: <%= ENV.fetch("DATABASE_PASSWORD") { '' } %> 81 | host: <%= ENV.fetch("DATABASE_HOST") { 'localhost' } %> 82 | ``` 83 | * Change your makefile to look like this: 84 | ``` 85 | dc=docker-compose -f docker-compose.yml $(1) 86 | dc-run=$(call dc, run --rm web $(1)) 87 | 88 | usage: 89 | @echo "Available targets:" 90 | @echo " * setup - Initiates everything (building images, installing gems, creating db and migrating" 91 | @echo " * build - Build image" 92 | @echo " * bundle - Install missing gems" 93 | @echo " * db-migrate - Runs the migrations for dev database" 94 | @echo " * db-test-migrate - Runs the migrations for test database" 95 | @echo " * dev - Fires a shell inside your container" 96 | @echo " * run - Runs the development server" 97 | @echo " * tear-down - Removes all the containers and tears down the setup" 98 | @echo " * stop - Stops the server" 99 | @echo " * test - Runs rspec" 100 | 101 | # With db 102 | setup: build bundle db-create db-migrate db-test-migrate 103 | 104 | build: 105 | $(call dc, build) 106 | bundle: 107 | $(call dc-run, bundle install) 108 | dev: 109 | $(call dc-run, ash) 110 | run: 111 | $(call dc, up) 112 | tear-down: 113 | $(call dc, down) 114 | stop: 115 | $(call dc, stop) 116 | console: 117 | $(call dc-run, bundle exec rails console) 118 | db-create: 119 | $(call dc-run, bundle exec rake db:create) 120 | db-migrate: 121 | $(call dc-run, bundle exec rake db:migrate) 122 | db-test-migrate: 123 | $(call dc-run, bundle exec rake db:migrate RAILS_ENV=test) 124 | 125 | 126 | # .PHONY: test 127 | # test: 128 | # $(call dc-run, bundle exec rspec) 129 | 130 | ``` 131 | * Rerun `make setup` to setup the databases. 132 | * Run `make up` and you should find your app running on http://localhost:3000 133 | ## Usage 134 | 135 | * After finishing the setup, running the command `make setup` should build your needed images, install necessary dependencies and setup your databases if needed. 136 | * Running `make dev` will fire up a shell inside your container and then you can treat it like your usual non dockerized development environment. 137 | 138 | --------------------------------------------------------------------------------