Skip to content

Commit 37d16b1

Browse files
committed
# This is a combination of 2 commits.
# This is the 1st commit message: Initial config for Rails 6 with Webpacker & PostgreSQL on Docker # This is the commit message #2: Initial commit
1 parent 7c2b032 commit 37d16b1

12 files changed

+495
-0
lines changed

.dockerdev/.bashrc

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
alias rm='rm -i'
2+
alias su='sudo -s'
3+
4+
alias ls='ls --color=auto'
5+
6+
alias ll='ls -lh'
7+
alias l='ls -la'

.dockerdev/.psqlrc

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
-- Don't display the "helpful" message on startup.
2+
\set QUIET 1
3+
4+
-- Allow specifying the path to history file via `PSQL_HISTFILE` env variable
5+
-- (and fallback to the defaukt $HOME/.psql_history otherwise)
6+
\set HISTFILE `[[ -z $PSQL_HISTFILE ]] && echo $HOME/.psql_history || echo $PSQL_HISTFILE`
7+
8+
-- Show how long each query takes to execute
9+
\timing
10+
11+
-- Use best available output format
12+
\x auto
13+
14+
-- Verbose error reports
15+
\set VERBOSITY verbose
16+
17+
-- If a command is run more than once in a row,
18+
-- only store it once in the history
19+
\set HISTCONTROL ignoredups
20+
\set COMP_KEYWORD_CASE upper
21+
22+
-- By default, NULL displays as an empty space. Is it actually an empty
23+
-- string, or is it null? This makes that distinction visible
24+
\pset null '[NULL]'
25+
26+
\unset QUIET

.dockerdev/Aptfile

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
vim

.dockerdev/Dockerfile

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
ARG RUBY_VERSION
2+
FROM ruby:$RUBY_VERSION
3+
4+
ARG PG_MAJOR
5+
ARG NODE_MAJOR
6+
ARG BUNDLER_VERSION
7+
ARG YARN_VERSION
8+
9+
# Common dependencies
10+
RUN apt-get update -qq \
11+
&& DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends \
12+
build-essential \
13+
gnupg2 \
14+
curl \
15+
less \
16+
git \
17+
&& apt-get clean \
18+
&& rm -rf /var/cache/apt/archives/* \
19+
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* \
20+
&& truncate -s 0 /var/log/*log
21+
22+
# Add PostgreSQL to sources list
23+
RUN curl -sSL https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - \
24+
&& echo 'deb http://apt.postgresql.org/pub/repos/apt/ buster-pgdg main' $PG_MAJOR > /etc/apt/sources.list.d/pgdg.list
25+
26+
# Add NodeJS to sources list
27+
RUN curl -sL https://deb.nodesource.com/setup_$NODE_MAJOR.x | bash -
28+
29+
# Add Yarn to the sources list
30+
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - \
31+
&& echo 'deb http://dl.yarnpkg.com/debian/ stable main' > /etc/apt/sources.list.d/yarn.list
32+
33+
# Install dependencies
34+
COPY .dockerdev/Aptfile /tmp/Aptfile
35+
RUN apt-get update -qq && DEBIAN_FRONTEND=noninteractive apt-get -yq dist-upgrade && \
36+
DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends \
37+
libpq-dev \
38+
postgresql-client-$PG_MAJOR \
39+
nodejs \
40+
yarn=$YARN_VERSION-1 \
41+
$(cat /tmp/Aptfile | xargs) && \
42+
apt-get clean && \
43+
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* && \
44+
truncate -s 0 /var/log/*log
45+
46+
# Configure bundler
47+
ENV LANG=C.UTF-8 \
48+
BUNDLE_JOBS=4 \
49+
BUNDLE_RETRY=3
50+
51+
# Uncomment this line if you store Bundler settings in the project's root
52+
# ENV BUNDLE_APP_CONFIG=.bundle
53+
54+
# Uncomment this line if you want to run binstubs without prefixing with `bin/` or `bundle exec`
55+
ENV PATH /app/bin:$PATH
56+
57+
# Upgrade RubyGems
58+
RUN gem update --system
59+
# Uncomment this line if you would like to specify other version of bundler
60+
# && gem install bundler:$BUNDLER_VERSION
61+
62+
# Create a directory for the app code
63+
RUN mkdir -p /app
64+
65+
WORKDIR /app

.gitignore

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# See https://help.github.com/articles/ignoring-files for more about ignoring files.
2+
#
3+
# If you find yourself ignoring temporary files generated by your text editor
4+
# or operating system, you probably want to add a global ignore instead:
5+
# git config --global core.excludesfile '~/.gitignore_global'
6+
7+
# Ignore bundler config.
8+
/.bundle
9+
10+
# Ignore all logfiles and tempfiles.
11+
/log/*
12+
/tmp/*
13+
!/log/.keep
14+
!/tmp/.keep
15+
16+
17+
/public/assets
18+
.byebug_history
19+
20+
# Ignore master key for decrypting credentials and more.
21+
/config/master.key
22+
23+
/public/packs
24+
/public/packs-test
25+
/node_modules
26+
/yarn-error.log
27+
yarn-debug.log*
28+
.yarn-integrity
29+
/config/secrets.yml
30+
31+
# Ignore other unneeded files.
32+
*.swp
33+
*~
34+
.project
35+
.DS_Store
36+
.idea
37+
.secret
38+
.sass-cache
39+
.env

.vscode/extensions.json

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"recommendations": [
3+
"rebornix.Ruby",
4+
"kaiwood.endwise",
5+
"vortizhe.simple-ruby-erb",
6+
"bung87.rails",
7+
"ninoseki.vscode-gem-lens",
8+
"sporto.rails-go-to-spec"
9+
]
10+
}

.vscode/settings.json

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"ruby.useLanguageServer": true,
3+
"ruby.useBundler": false,
4+
"ruby.intellisense": "rubyLocate",
5+
"ruby.format": "rufo",
6+
"files.associations": {
7+
"*.html.erb": "html"
8+
},
9+
"[html]": {
10+
"editor.defaultFormatter": "vscode.html-language-features"
11+
},
12+
"editor.formatOnSave": true,
13+
14+
"ruby.lint": {
15+
"rubocop": true
16+
},
17+
}

Gemfile

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
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"

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 Shozo Hatta
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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+

dip.yml

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
version: "4.2"
2+
3+
environment:
4+
RAILS_ENV: development
5+
6+
compose:
7+
files:
8+
- docker-compose.yml
9+
10+
interaction:
11+
sh:
12+
description: Open a Bash shell within a Rails container (with dependencies up)
13+
service: runner
14+
command: /bin/bash
15+
16+
bash:
17+
description: Run an arbitrary script within a container (or open a shell without deps)
18+
service: runner
19+
command: /bin/bash
20+
compose_run_options: [no-deps]
21+
22+
bundle:
23+
description: Run bundler command
24+
service: backend
25+
command: bundle
26+
compose_run_options: [no-deps]
27+
28+
rake:
29+
description: Run rake command
30+
service: backend
31+
command: bundle exec rake
32+
33+
rails:
34+
description: Run rails command
35+
service: backend
36+
command: bundle exec rails
37+
subcommands:
38+
s:
39+
description: Start rails server
40+
service: rails
41+
compose_run_options: [service-ports]
42+
43+
yarn:
44+
description: Run yarn command
45+
service: backend
46+
command: yarn
47+
compose_run_options: [no-deps]
48+
49+
minitest:
50+
description: Run minitest
51+
service: backend
52+
environment:
53+
RAILS_ENV: test
54+
command: bundle exec rails test
55+
56+
psql:
57+
description: Run psql console
58+
service: postgres
59+
command: psql -h postgres -U postgres -d postgres
60+
61+
pack:
62+
description: Run webpack
63+
service: backend
64+
command: webpack
65+
66+
# Destructive!: just for a quick `rails new`
67+
provision:
68+
- dip compose down --volumes
69+
- dip compose up -d postgres
70+
- dip bundle install
71+
- dip rails new . -d postgresql --webpacker --skip-listen --skip-git
72+
- dip yarn install
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+
- dip sh -c "rails db:prepare 2> /dev/null; exit 0 && rails db:prepare && RAILS_ENV=test rails db:prepare"

0 commit comments

Comments
 (0)