Published on

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.

Our Directory Setup:

This can be retrieved from, but I will place the code in here as well.

FROM alpine:edge
RUN apk add --no-cache python2 py2-pip && pip install flask
CMD ["/bin/sh", "/"]
import os
from glob import glob

for var in glob('/run/secrets/*'):
    os.environ[k] = v
    print("export {key}={value}".format(key=k,value=v))
import os
from flask import Flask

flask_host = str(os.environ['flask_host'])
flask_port = int(os.environ['flask_port'])

app = Flask(__name__)

def index():
    return 'ok\n'

if __name__ == '__main__':, port=flask_port)

The boot script:
set -e
eval $(python /
python /

Flow Information:

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.


Create Docker Secrets:

echo 5001 | docker secret create flask_port -
echo | docker secret create flask_host -

Build and Push the Image:

docker build -t<user>/<repo>/<image>:<tag>
docker push<user>/<repo>/<image>:<tag>

Create the Service, and specify the secrets that we created earlier:

docker service create --name webapp \
  --secret source=flask_host,target=flask_host \
  --secret source=flask_port,target=flask_port \<user>/<repo>/<image>:<tag>

Exec into the container, list to see where the secrets got populated:

ls /run/secrets/
# flask_host  flask_port

Do a netstat, to see that the value from the created secret is listening:

netstat -tulpn
# Active Internet connections (only servers)
# Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
# tcp        0      0  *               LISTEN      7/python

Do a GET request on the Flask Application:

# ok

Thank You

Thanks for reading, feel free to check out my website, and subscrube to my newsletter or follow me at @ruanbekker on Twitter.

Please feel free to show support by, sharing this post, making a donation, subscribing or reach out to me if you want me to demo and write up on any specific tech topic.

Buy Me A Coffee