Skip to content

Commit

Permalink
Merge pull request #377 from wearefuturegov/docker-hot-reload
Browse files Browse the repository at this point in the history
Add support for hotreload in docker - if you are using docker for development you may need to rebuild using the instructions in the README
  • Loading branch information
dtt101 authored Dec 17, 2020
2 parents 759a749 + 2c79a0f commit 8abda68
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Dockerfile.dev
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ FROM ruby:2.5.8

RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
RUN echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
RUN apt-get update -qq && apt-get remove cmdtest && apt-get install -y nodejs postgresql-client yarn
RUN apt-get update -qq && apt-get install -y nodejs postgresql-client yarn
RUN gem install bundler -v 2.1.4
ENV RAILS_ENV development
ENV DATABASE_HOST postgresql
Expand Down
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ gem 'activerecord-import'

group :development, :test do
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
gem 'guard', '~>2.16.2', require: false
gem 'guard-livereload', '~>2.5.2', require: false
gem 'rack-livereload'
gem 'foreman'
end

group :development do
Expand Down
34 changes: 34 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,11 @@ GEM
cucumber-wire (0.0.1)
database_cleaner (1.8.4)
diff-lcs (1.3)
em-websocket (0.5.2)
eventmachine (>= 0.12.9)
http_parser.rb (~> 0.6.0)
erubi (1.9.0)
eventmachine (1.2.7)
factory_bot (5.1.2)
activesupport (>= 4.2.0)
factory_bot_rails (5.1.1)
Expand All @@ -115,12 +119,30 @@ GEM
faker (2.11.0)
i18n (>= 1.6, < 2)
ffi (1.12.2)
foreman (0.87.2)
formatador (0.2.5)
gherkin (5.1.0)
globalid (0.4.2)
activesupport (>= 4.2.0)
govuk_notify_rails (2.1.2)
notifications-ruby-client (~> 5.1)
rails (>= 4.1.0)
guard (2.16.2)
formatador (>= 0.2.4)
listen (>= 2.7, < 4.0)
lumberjack (>= 1.0.12, < 2.0)
nenv (~> 0.1)
notiffany (~> 0.0)
pry (>= 0.9.12)
shellany (~> 0.0)
thor (>= 0.18.1)
guard-compat (1.2.1)
guard-livereload (2.5.2)
em-websocket (~> 0.5)
guard (~> 2.8)
guard-compat (~> 1.0)
multi_json (~> 1.8)
http_parser.rb (0.6.0)
i18n (1.8.2)
concurrent-ruby (~> 1.0)
jaro_winkler (1.5.4)
Expand Down Expand Up @@ -150,6 +172,7 @@ GEM
loofah (2.4.0)
crass (~> 1.0.2)
nokogiri (>= 1.5.9)
lumberjack (1.2.8)
mail (2.7.1)
mini_mime (>= 0.1.1)
marcel (0.3.3)
Expand All @@ -165,9 +188,13 @@ GEM
msgpack (1.3.3)
multi_json (1.14.1)
multi_test (0.1.2)
nenv (0.3.0)
nio4r (2.5.2)
nokogiri (1.10.9)
mini_portile2 (~> 2.4.0)
notiffany (0.1.3)
nenv (~> 0.1)
shellany (~> 0.0)
notifications-ruby-client (5.1.2)
jwt (>= 1.5, < 3)
paper_trail (10.3.1)
Expand Down Expand Up @@ -199,6 +226,8 @@ GEM
pundit (2.1.0)
activesupport (>= 3.0.0)
rack (2.2.2)
rack-livereload (0.3.17)
rack
rack-proxy (0.6.5)
rack
rack-test (1.1.0)
Expand Down Expand Up @@ -290,6 +319,7 @@ GEM
selenium-webdriver (3.142.7)
childprocess (>= 0.5, < 4.0)
rubyzip (>= 1.2.2)
shellany (0.0.1)
shoulda-matchers (4.3.0)
activesupport (>= 4.2.0)
spring (2.1.0)
Expand Down Expand Up @@ -351,7 +381,10 @@ DEPENDENCIES
database_cleaner
factory_bot_rails
faker
foreman
govuk_notify_rails
guard (~> 2.16.2)
guard-livereload (~> 2.5.2)
jbuilder (~> 2.7)
jsonb_accessor (~> 1.0.0)
kaminari
Expand All @@ -365,6 +398,7 @@ DEPENDENCIES
pry-rails
puma (~> 4.1)
pundit (~> 2.1.0)
rack-livereload
rails (~> 6.0.2, >= 6.0.2.2)
rails-controller-testing
roo (~> 2.8)
Expand Down
54 changes: 54 additions & 0 deletions Guardfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# A sample Guardfile
# More info at https://github.com/guard/guard#readme

## Uncomment and set this to only include directories you want to watch
# directories %w(app lib config test spec features) \
# .select{|d| Dir.exist?(d) ? d : UI.warning("Directory #{d} does not exist")}

## Note: if you are using the `directories` clause above and you are not
## watching the project directory ('.'), then you will want to move
## the Guardfile to a watched dir and symlink it back, e.g.
#
# $ mkdir config
# $ mv Guardfile config/
# $ ln -s config/Guardfile .
#
# and, you'll have to watch "config/Guardfile" instead of "Guardfile"

guard 'livereload' do
extensions = {
css: :css,
scss: :css,
sass: :css,
js: :js,
coffee: :js,
html: :html,
png: :png,
gif: :gif,
jpg: :jpg,
jpeg: :jpeg
}

rails_view_exts = %w[erb haml slim]

# file types LiveReload may optimize refresh for
compiled_exts = extensions.values.uniq
watch(%r{public/.+\.(#{compiled_exts * '|'})})

extensions.each do |ext, type|
watch(%r{
(?:app|vendor)
(?:/assets/\w+/(?<path>[^.]+) # path+base without extension
(?<ext>\.#{ext})) # matching extension (must be first encountered)
(?:\.\w+|$) # other extensions
}x) do |m|
path = m[1]
"/assets/#{path}.#{type}"
end
end

# file needing a full reload of the page anyway
watch(%r{app/views/.+\.(#{rails_view_exts * '|'})$})
watch(%r{app/helpers/.+\.rb})
watch(%r{config/locales/.+\.yml})
end
2 changes: 2 additions & 0 deletions Procfile.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
web: bundle exec rails s -b '0.0.0.0'
guard: bundle exec guard -i
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,30 @@ It's a rails app backed by a postgresql database.

## Running with Docker Compose

Spin up a postgres database and rails app
### Build

Follow these steps to build the database and app

```
docker-compose build
docker-compose run app rails db:setup
docker-compose up
```

Run tests
The site will now be available on https://localhost:3000

### Run tests

```
docker-compose run app rake
```

### Run app

```
docker-compose up
```

## Running it locally

```
Expand Down
14 changes: 10 additions & 4 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb.

# docker specific development setup
if ENV.fetch('DOCKERIZED', false) == 'true'
# setup hot reload
config.middleware.insert_after(ActionDispatch::Static, Rack::LiveReload)
# whitelist ip to enable web console
host_ip = `/sbin/ip route|awk '/default/ { print $3 }'`.strip
config.web_console.whitelisted_ips = host_ip
end

# In the development environment your application's code is reloaded on
# every request. This slows down response time but is perfect for development
# since you don't have to restart the web server when you make code changes.
Expand Down Expand Up @@ -71,8 +80,5 @@
:port => 1025,
:authentication => :plain,
:enable_starttls_auto => false
}

# Allow console to connect from docker
config.web_console.whitelisted_ips = ['172.29.0.0/16', '127.0.0.1']
}
end
3 changes: 3 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ services:
build:
context: .
dockerfile: Dockerfile.dev
command: bash -c "rm -f tmp/pids/server.pid && foreman start -f Procfile.dev -p 3000"
volumes:
- .:/app
ports:
- "3000:3000"
- "35729:35729"
environment:
- HOSTNAME=localhost:3000
- DOCKERIZED=true
depends_on:
- postgresql

0 comments on commit 8abda68

Please sign in to comment.