I'm mirroring [this answer][1] here:
Thanks to @mdkrog, because that's part of the issue!
There are 2 things that need to be done:
1) Map the current directory to the place where Docker is currently hosting the files.
2) Change the config.file_watcher to ActiveSupport::FileUpdateChecker
#Step 1:
In your Dockerfile, check where are you copying/adding the files.
See my Dockerfile:
# https://docs.docker.com/compose/rails/#define-the-project
FROM ruby:2.5.0
# The qq is for silent output in the console
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs vim
# Sets the path where the app is going to be installed
ENV RAILS_ROOT /var/www/
# Creates the directory and all the parents (if they don't exist)
RUN mkdir -p $RAILS_ROOT
# This is given by the Ruby Image.
# This will be the de-facto directory that
# all the contents are going to be stored.
WORKDIR $RAILS_ROOT
# We are copying the Gemfile first, so we can install
# all the dependencies without any issues
# Rails will be installed once you load it from the Gemfile
# This will also ensure that gems are cached and onlu updated when
# they change.
COPY Gemfile ./
COPY Gemfile.lock ./
# Installs the Gem File.
RUN bundle install
# We copy all the files from the current directory to our
# /app directory
# Pay close attention to the dot (.)
# The first one will select ALL The files of the current directory,
# The second dot will copy it to the WORKDIR!
COPY . .
The `/var/www` directory is key here. That's the inner folder structure of the image, and where you need to map the current directory to.
Then, in your docker-compose, define an index called volumes, and place that route there (Works for V2 as well!):
version: '3'
services:
rails:
# Relative path to Dockerfile.
# Docker will read the Dockerfile automatically
build: .
# This is the one that makes the magic
volumes:
- "./:/var/www"
[![Directory of the current project][2]][2]
The image above is for reference. Check that the docker-compose and Dockefile are in the same directory. They **not** necessarily need to be like this, you just have to be sure that the directories are specified correctly.
docker-compose works **relative to the file**. The `./`means that it will take the current docker-compose directory (In this case the entire ruby app) as the place where it's going to host the image's content.
The `:` just a way to divide between the where it's going to be vs where the image has it.
The next part: `/var/www/` is the same path you specified in the Dockerfile.
# Step 2:
Open development.rb (Can be found in <root>/config/environments)
and look for `config.file_watcher`, replace:
config.file_watcher = ActiveSupport::EventedFileUpdateChecker
for:
config.file_watcher = ActiveSupport::FileUpdateChecker
This would do a polling mechanism instead.
That's it!
Remember, that anything that is not routes.rb, and it's not inside the app folder, it's highly probable that the Rails app is going to need to be reloaded manually.
[1]: https://stackoverflow.com/questions/37699573/rails-app-in-docker-container-doesnt-reload-in-development/49678269#49678269
[2]: https://i.stack.imgur.com/gbTjt.png
I was struggling with this as well, there are 2 things that you need to do:
1) Map the current directory to the place where Docker is currently hosting the files.
2) Change the config.file_watcher to ActiveSupport::FileUpdateChecker
#Step 1:
In your Dockerfile, check where are you copying/adding the files.
See my Dockerfile:
# https://docs.docker.com/compose/rails/#define-the-project
FROM ruby:2.5.0
# The qq is for silent output in the console
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs vim
# Sets the path where the app is going to be installed
ENV RAILS_ROOT /var/www/
# Creates the directory and all the parents (if they don't exist)
RUN mkdir -p $RAILS_ROOT
# This is given by the Ruby Image.
# This will be the de-facto directory that
# all the contents are going to be stored.
WORKDIR $RAILS_ROOT
# We are copying the Gemfile first, so we can install
# all the dependencies without any issues
# Rails will be installed once you load it from the Gemfile
# This will also ensure that gems are cached and onlu updated when
# they change.
COPY Gemfile ./
COPY Gemfile.lock ./
# Installs the Gem File.
RUN bundle install
# We copy all the files from the current directory to our
# /app directory
# Pay close attention to the dot (.)
# The first one will select ALL The files of the current directory,
# The second dot will copy it to the WORKDIR!
COPY . .
The `/var/www` directory is key here. That's the inner folder structure of the image, and where you need to map the current directory to.
Then, in your docker-compose, define an index called volumes, and place that route there (Works for V2 as well!):
version: '3'
services:
rails:
# Relative path to Dockerfile.
# Docker will read the Dockerfile automatically
build: .
# This is the one that makes the magic
volumes:
- "./:/var/www"
[![Directory of the current project][1]][1]
The image above is for reference. Check that the docker-compose and Dockefile are in the same directory. They **not** necessarily need to be like this, you just have to be sure that the directories are specified correctly.
docker-compose works **relative to the file**. The `./`means that it will take the current docker-compose directory (In this case the entire ruby app) as the place where it's going to host the image's content.
The `:` just a way to divide between the where it's going to be vs where the image has it.
The next part: `/var/www/` is the same path you specified in the Dockerfile.
# Step 2:
Open development.rb (Can be found in <root>/config/environments)
and look for `config.file_watcher`, replace:
config.file_watcher = ActiveSupport::EventedFileUpdateChecker
for:
config.file_watcher = ActiveSupport::FileUpdateChecker
This would do a polling mechanism instead.
That's it!
Remember, that anything that is not routes.rb, and it's not inside the app folder, it's highly probable that the Rails app is going to need to be reloaded manually.
[1]: https://i.stack.imgur.com/gbTjt.png