Deploy Rails application as Production mode
When we deploy a Rails app to a cloud, or even running from a docker image in Production mode, there will be a number of issues could happen. Below is what I encountered:
1/ ArgumentError: Missing `secret_key_base` for 'production' environment, set this string with `bin/rails credentials:edit`
This seems to be a missing key in config/credentials.yml.enc, but actually the is is available. After some digging, I found that when copying the app to build Docker image, we ignored the file /config/master.key
If including that file, the issue fixed, but we shouldn't fix this way.
So we must put the master key as an environment variable. If running a container, the fix can be this way: -e RAILS_MASTER_KEY=<YOUR_KEY>
2/ Set production environment
Can be done by this way: -e RAILS_ENV=production ; when running by docker
3/ Missing application.css file
Ensure that the application.css file exists in the app/assets/stylesheets directory.
Open the config/initializers/assets.rb file and verify the following settings:
Rails.application.config.assets.precompile += %w( application.css )
Then add this into the startup script before starting the server:
rails assets:precompile
4/ Server memory issue.
If running in development/test modes, the ram 256MB is OK, when switching to production, we need to add more RAM, eg 512MB.
2024-09-18 16:36:06.105 ICT
Memory limit of 256 MiB exceeded with 256 MiB used. Consider increasing the memory limit, see https://cloud.google.com/run/docs/configuring/memory-limits
5/ Set number of threads for the server:
Create this ENV variable: RAILS_MAX_THREADS