Docker, Rails and passing configuration via environment variables.

Docker has a really nice option to pass configuration variables to the inside of the container. Sadly it’s only available to the user that the container runs with.

Problems are starting when you want to su - user and do something which requires use of passed environment variables which are not there after executing su - command.

One solution to this is to use a entrypoint wrapper script which before starting anything else will execute

env > /etc/.docker_env

At this point we have all variables saved in a file and we’re able to source them when starting new shell. This is only a part of the complete solution as we would need to have all those variables available also inside our Rails app.

Digging a bit in Rails initialization process led me to config/boot.rb which is used to set ENV['BUNDLE_GEMFILE'] a variable defining location path of Gemfile. We can also use this file to import all environment variables from .docker_env file.


require 'dotenv'

does the trick and next time when we need to run any rails task we don’t have to worry about sourcing proper environment.

Docker, Rails and passing configuration via environment variables.

Rails, database connection pool and sidekiq concurrency.

This should be quite easy for everyone, but not everyone understands how connection pooling and concurrency cooperate and blame the part of the system which shouldn’t be blamed in the first place.

So here it goes.

Connection pool in Rails config/database.yml must have a larger value than the Concurrency level set in config/sidekiq.yml

Why? Because when you run rails app and sidekiq together on one node sidekiq is using connection pool settings from Rails app. You need to have some part of the pool for Rails app and some part of the pool for sidekiq tasks.

Simple as that. If you see timeouts in sidekiq saying that it couldn’t obtain database connection in time specified by timeout value (default is 5 secs) then most probably it means that you have misconfigured your application.

Rails, database connection pool and sidekiq concurrency.