Populate Environment Variables From Docker Secrets With a Flask Demo App
In this post we will create a basic Python Flask WebApp on Docker Swarm, but we will read our Flask Host, and Flask Port from Environment Variables, which will be populated from Docker Secrets, which we will read in from a python script.
The exporter script checks all the secrets that is mounted to the container, then formats the secrets to a key/value pair, which then exports the environment variables to the current shell, which thereafter gets read by the flask application.
Exec into the container, list to see where the secrets got populated:
boot.sh
12
$ ls /run/secrets/
flask_host flask_port
Do a netstat, to see that the value from the created secret is listening:
boot.sh
1234
$ netstat -tulpn
Active Internet connections (only servers)Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 00 0.0.0.0:5001 0.0.0.0:* LISTEN 7/python