|
| 1 | +# Quick start for Rails 6 + Webpacker + PostgreSQL on Docker |
| 2 | + |
| 3 | +> The repository is based on the article [Ruby on Whales: Dockerizing Ruby and Rails development — Martian Chronicles, Evil Martians’ team blog](https://evilmartians.com/chronicles/ruby-on-whales-docker-for-ruby-rails-development). Note that the article will be continuously updated. |
| 4 | +
|
| 5 | +The docker-compose.yml is for building a minimum and configurable development environment for Rails 6 with the following, in a very short time. |
| 6 | + |
| 7 | +- Webpacker |
| 8 | +- PostgreSQL |
| 9 | + |
| 10 | +You don't need to tweak the Dockerfile to update gems, packs, databases. |
| 11 | + |
| 12 | +Note that current config is just for building a dev env, not for production container. |
| 13 | + |
| 14 | +Assumed that bundler is included within Ruby 2.7 or later. If you want to specify other version of bundler, uncomment the following: |
| 15 | + |
| 16 | +- `BUNDLER_VERSION` in docker-compose.yml |
| 17 | +- `&& gem install bundler:$BUNDLER_VERSION` in .dockerdev/Dockerfile. |
| 18 | + |
| 19 | +## Requirement |
| 20 | + |
| 21 | +- Docker and Docker Compose |
| 22 | +- [dip](https://github.com/bibendi/dip) for managing docker-compose.yml |
| 23 | + |
| 24 | +## Step |
| 25 | + |
| 26 | +### Preparation |
| 27 | + |
| 28 | +- Clone this repository to your local environment. |
| 29 | +- You can `rm -rf .git` to recreate the repository for your own usage. |
| 30 | +- Adjust the values at the top of docker-compose.yml if needed: |
| 31 | + |
| 32 | +```yaml |
| 33 | +x-var: &APP_IMAGE_TAG |
| 34 | + "my_app:1.0.0" |
| 35 | +x-var: &RUBY_VERSION |
| 36 | + "2.7.0-slim-buster" |
| 37 | +x-var: &PG_MAJOR |
| 38 | + 12 |
| 39 | +x-var: &POSTGRES |
| 40 | + "postgres:12" |
| 41 | +x-var: &NODE_MAJOR |
| 42 | + 12 |
| 43 | +x-var: &YARN_VERSION |
| 44 | + 1.21.1 |
| 45 | +x-var: &BUNDLER_VERSION |
| 46 | + 2.1.2 |
| 47 | +``` |
| 48 | +
|
| 49 | +Assumed that bundler is included within Ruby 2.7 or later. If you want to specify other version of bundler, uncomment the following as well: |
| 50 | +
|
| 51 | +- `BUNDLER_VERSION` in docker-compose.yml |
| 52 | +- `&& gem install bundler:$BUNDLER_VERSION` in .dockerdev/Dockerfile. |
| 53 | + |
| 54 | +### Install |
| 55 | + |
| 56 | +### A. Quick Install |
| 57 | + |
| 58 | +- `dip provision` |
| 59 | + |
| 60 | +> Note: `--skip-listen` option is specified in the command to avoid the issue on macOS: |
| 61 | +> ref: [Code is not reloaded in dev with Docker on OS X · Issue \#25186 · rails/rails](https://github.com/rails/rails/issues/25186). Perhaps you can remove the option for Linux environments. |
| 62 | + |
| 63 | +### B. Custom Install |
| 64 | + |
| 65 | +- `dip compose build` to build a container. |
| 66 | +- `dip bundle install` to install gems for Rails. |
| 67 | +- `dip bundle exec rails new . --webpacker <options as you like>`. |
| 68 | + - To macOS user: add `--skip-listen` |
| 69 | +- `dip yarn install` to install yarn. |
| 70 | +- Perform the following manually to activate local access via Docker: |
| 71 | + |
| 72 | +```sh |
| 73 | +dip sh -c "sed -i -e \"3i\ config.hosts << 'localhost'\" config/environments/development.rb" |
| 74 | +dip sh -c "sed -i -e \"4i\ config.web_console.whitelisted_ips = '0.0.0.0/0'\" config/environments/development.rb" |
| 75 | +``` |
| 76 | + |
| 77 | +- Then manually create databases: |
| 78 | + |
| 79 | +```sh |
| 80 | +dip sh -c "rails db:prepare 2> /dev/null; exit 0 && rails db:prepare" |
| 81 | +dip sh -c "RAILS_ENV=test rails db:prepare" |
| 82 | +``` |
| 83 | + |
| 84 | +> Known issue: currently, running `db:prepare` twice is needed for establishing the initial database connection. |
| 85 | + |
| 86 | +-------- |
| 87 | + |
| 88 | +That's all. Now you can run `rails s` command via `dip rails s`. You don't need to add `bundle exec` any more. |
| 89 | + |
| 90 | +### Misc |
| 91 | + |
| 92 | +- You can see the available dip commands via `dip ls`. |
| 93 | +- .vscode contains a minimum set of conf and extensions. You can discard. |
| 94 | +- If you encounter any issues around caching, try checking bootsnap and spring gem. |
| 95 | + |
| 96 | +## Webpacker + Bootstrap + font-awesome |
| 97 | + |
| 98 | +You can configure Bootstrap 4 and font-awesome on Webpacker by running the following script: |
| 99 | + |
| 100 | +```sh |
| 101 | +dip yarn add bootstrap jquery popper.js @fortawesome/fontawesome-free |
| 102 | +
|
| 103 | +mkdir app/javascript/src |
| 104 | +mkdir app/javascript/images |
| 105 | +
|
| 106 | +echo "@import '~bootstrap/scss/bootstrap';" > app/javascript/src/application.sass |
| 107 | +echo "@import '~@fortawesome/fontawesome-free/scss/fontawesome';" >> app/javascript/src/application.sass |
| 108 | +
|
| 109 | +dip sh -c 'sed -i -e "s/stylesheet_link_tag/stylesheet_pack_tag/g" app/views/layouts/application.html.erb' |
| 110 | +
|
| 111 | +dip sh -c "sed -i -e \"10i\import 'bootstrap';\" app/javascript/packs/application.js" |
| 112 | +dip sh -c "sed -i -e \"11i\import '../src/application.sass';\" app/javascript/packs/application.js" |
| 113 | +dip sh -c "sed -i -e \"12i\import '@fortawesome/fontawesome-free/js/all';\" app/javascript/packs/application.js" |
| 114 | +``` |
| 115 | + |
| 116 | +Then you can remove app/assets and deactivate Sprockets if unnecessary. |
| 117 | + |
0 commit comments