nickjj / docker-rails-example

A production ready example Rails app that's using Docker and Docker Compose.
MIT License
975 stars 197 forks source link

Issue calling Node Binaries / Modules #61

Closed johannesschobel closed 1 year ago

johannesschobel commented 1 year ago

Dear @nickjj ,

first of all, i would like to thank you for putting together this awesome repository and project. This is a really cool starter for new rails projects.

I have started to work on a rails application and have installed devise for my user management. In order to send mails, i would like to use mjml (https://mjml.io/) and mjml-rails (https://github.com/sighmon/mjml-rails) respectively. However, mjml-rails requires you to also install the npm package mjml (i.e., yarn install mjml)

As far as i have understood, mjml-rails calls the mjml binary from the mjml node package in order to transform the <mjml-*> tags to proper valid and responsive html tags .

In the docker-compose setup of this project, however, all js-bundling stuff works within the NAME-js container. The actual web container does not have a node-modules folder. Hence, the mjml binary is missing.

How can i get this to work? This is not only the problem with mjml, but rather with all node packages that are called via the console, right? I know, that the basic idea is to have all the javascript-stuff built within a dedicated container and then "move the final product" to the application container. But in this case (i.e., requiring a dedicated node binary that is invoked via the console) this does not work. Or am i missing something?

Thank you very much for your time and effort in helping me with this issue. All the best, Johannes

nickjj commented 1 year ago

Hi,

Technically the JS environment has access to the Rails environment, if you need to run 1 off JS commands through Rails you can do it through that container. For example that's how the test suite is invoked since it depends on assets.

If your main Rails runtime needs full access to Node outside of the Rails console (such as when folks are hitting various URLs or Sidekiq) then you'll probably want to remove the app stage from the Dockerfile and use the assets stage for the web, worker and cable Docker Compose services.

Then the entire Node environment will be available to everything, at the cost of your Docker image being a lot larger but if you need the environment then there's not much you can do here.

I'd suggest:

johannesschobel commented 1 year ago

Dear @nickjj ,

thank you very much for helping me out. I will try your suggestion and give feedback! All the best, Johannes

johannesschobel commented 1 year ago

Dear @nickjj and everyone that may also stumble upon this issue ;) I just got it to work (thanks a lot @nickjj !). This is, what i did:

1) in your .yarnrc file, remove the line with --modules-folder 2) in your app/config/initializers/assets.rb comment out the line with Rails.application.config.assets.path 3) in your docker-compose.yml file, delete the x-assets part and the js and css services 4) compare the app and assets build phases in your Dockerfile and copy a few parts from the app to the assets part. Then remove the app part and rename the assets to app (as @nickjj described above). Basically you want to copy the cmd part from the app to the assets in order to start the actual rails server 5) in the bin/docker-entrypoint-web file, comment out the cp -r /public /app part, as your public files are now directly within the app directory of the docker container.

Happy coding ;)

All the best, Johannes

nickjj commented 1 year ago

No problem, happy to hear it helped. Thanks for listing the steps.