keys.env contains private key:
KEY="-----BEGIN PRIVATE KEY-----\nBADANBgkqhki......xjiWsX7Qjoh8XH8nAECgYEAkCNIy1eICdUUt6HgV\nnEGKpwDETJTIJdmW5MlOzsURI/RSnE2Qas/k758isGLaA4m9dZJoxuP/pCfwcvLj\nwmjVBPdTTNF6ADgor6ZVIp6os5wIIurZH7f8yXaggTEyk5r8K6qxz9t/D\n4FaPDsZ2icg0N5i2y/2Sa0w=\n-----END PRIVATE KEY-----\n"
(The value of KEY should be enclosed in quotes because it contains \n)
docker-compose.yml contains env_file parameter:
version: '2'
services:
testenv:
image: ruby:2.4-slim
env_file:
- ./keys.env
I run the container with ruby app:
docker-compose run testenv
and try to use ENV['KEY'] variable:
irb(main):001:0> ENV['KEY']
=> "\"-----BEGIN PRIVATE KEY-----\\nBADANBgkqhki......xjiWsX7Qjoh8XH8nAECgYEAkCNIy1eICdUUt6HgV\\nnEGKpwDETJTIJdmW5MlOzsURI/RSnE2Qas/k758isGLaA4m9dZJoxuP/pCfwcvLj\\nwmjVBPdTTNF6ADgor6ZVIp6os5wIIurZH7f8yXaggTEyk5r8K6qxz9t/D\\n4FaPDsZ2icg0N5i2y/2Sa0w=\\n-----END PRIVATE KEY-----\\n\""
The variable is escaped, but I need clean private_key.
I have not found the answer in the Docker documentations.
I see two working solutions, but none seems to me right.
- Store private_key in the .env file encoded and without quotes. A module in the app will decode this value.
- Unescape value in the app (like this Best way to escape and unescape strings in Ruby?)
Is there a way to solve this problem with built-in capabilities of Docker?