Skip to content

Instantly share code, notes, and snippets.

@scottserok
Created April 8, 2018 21:35
Show Gist options
  • Save scottserok/6c36a7459fe00175821c543b399c7b20 to your computer and use it in GitHub Desktop.
Save scottserok/6c36a7459fe00175821c543b399c7b20 to your computer and use it in GitHub Desktop.
Load docker secrets into Rails
# config/initializers/001_secrets.rb
# Loads docker secrets into ENV
# Example docker-compose.yml:
#
# version: "3.4"
# services:
# web:
# image: myapp
# build: .
# ports:
# - "3000:3000"
# secrets:
# - secret_api_key
#
# secrets:
# secret_api_key:
# external: true
# Now create your docker secret while on a machine in the swarm:
# $ echo "389rAa39wef9+q32jfqp0w9jrq0Zhtpq23hpa9=" | docker secret create secret_api_key -
Dir['/run/secrets/*'].each do |secret_file|
next if secret_file =~ /\/$/
name = secret_file.split('/')[-1]
ENV[name.upcase] = File.read(secret_file).strip if name
end
# Now we have access to the docker secret:
# ENV['SECRET_API_KEY']
# => "389rAa39wef9+q32jfqp0w9jrq0Zhtpq23hpa9="
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment