nickjj / docker-rails-example

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

Put gems in host 'vendor/bundle' #66

Open travisdock opened 9 months ago

travisdock commented 9 months ago

Hi Nick,

I asked you in a previous PR if you had considered putting gems on the host so that you don't have to rebuild your containers and bundle install all gems every time you add a new one. I made a quick example of how this might work. I am still learning about your setup so I am not sure this is the best solution but it works.

In order to test it you can add the BUNDLE_PATH variable from .env.example and remove your previously built images. Then run docker compose up --build and you should see the gems install as the web container starts up. They are installed into the vendor/bundle folder locally. Visit localhost:8000 to make sure everything is working and then go back to your project and add any gem to your Gemfile. You can restart the containers or you can exec into the web container and bundle install. The new gem will be installed locally, no need to rebuild anything. But if you do need to rebuild a container the gems will still be there ready to use without running bundle again.

Note: One side effect of this method that I don't quite understand is that I am now forced to use bundle exec anywhere I wish to use one of the installed gems. I know this is because I am no longer installing gems in the default path but for the life of me I cannot get it to recognize my new path. Adding to GEM_HOME and GEM_PATHS didn't seem to help but perhaps I am misunderstanding how those work.

Also, you mentioned in the PR where I brought up this idea:

a volume mount works at the cost of mounting a lot of files back to your host.

Could you explain what that cost is? My thought was that it would be a gain to store more files on the host rather than have large images that take more time to upload and download.

This is more of an idea PR to get your thoughts. If you are considering merging it and you want me to polish it up or make any changes let me know! Feel free to close it as well if it doesn't suit your fancy.