Advertisement
Guest User

Untitled

a guest
Mar 31st, 2017
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. # Variables by Secrets
  2.  
  3. Sample script that allows you to define as environment variables the name of the docker secret that contains the secret value.
  4. It will be in charge of analyze all the environment variables searching for the placeholder to substitute the variable value
  5. by the secret.
  6.  
  7. ## Usage
  8.  
  9. You can define the next environment variables:
  10.  
  11. ``` bash
  12. $ env | grep DB_
  13. DB_HOST=my-db-host
  14. DB_USER=my-db-user
  15. DB_PASS=my-db-pass
  16. ```
  17.  
  18. And nothing would happen. None of the variables would be modified when starting the container.
  19.  
  20. But if you define variables with the defined placeholder it will expand the value with the referred secret.
  21.  
  22. ### Example
  23.  
  24. Create Secret
  25. ``` bash
  26. echo "my-db-pass" | docker secret create secret-db-pass -
  27. ```
  28.  
  29. ``` bash
  30. $ env | grep DB_
  31. DB_HOST=my-db-host
  32. DB_USER=my-db-user
  33. DB_PASS={{DOCKER-SECRET:secret-db-pass}}
  34. ```
  35.  
  36. When starting the script will search for the placeholder `{{DOCKER-SECRET:xxxx}}` on each
  37. environment variable and will substitute the value by the content of the secret `xxxx`,
  38. in this example it means to end up with:
  39.  
  40. ``` bash
  41. DB_HOST=my-db-host
  42. DB_USER=my-db-user
  43. DB_PASS=my-db-pass
  44. ```
  45.  
  46. ### How to use it
  47.  
  48. If you want to use this feature on any image just add the env_secrets_expand.sh
  49. file in your container entrypoint script and invoke it with `source env_secrets_expand.sh`
  50.  
  51. ### How to test this
  52.  
  53. Build a sample image with the required dependency and enter into it:
  54.  
  55. ``` bash
  56. docker run --rm -v $PWD:/test -it alpine sh
  57. ```
  58.  
  59. Just emulate the creation of a secret and the example variables with the next commands:
  60.  
  61. ``` bash
  62. mkdir -p /run/secrets/
  63. echo "my-db-pass" > /run/secrets/secret-db-pass
  64. export DB_HOST=my-db-host
  65. export DB_USER=my-db-user
  66. export DB_PASS={{DOCKER-SECRET:secret-db-pass}}
  67. ```
  68.  
  69. Execute the script:
  70.  
  71. ``` bash
  72. ENV_SECRETS_DEBUG=true /test/env_secrets_expand.sh
  73. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement