Deployment of flask application using Docker failing with " context.load_cert_chain('cert.pem','key.pem') FileNotFoundError: [Errno 2]"

Issue

I have a flask program deploying using docker. I want it to be https protocol enabled.
SSL self signed certificate command used:
openssl req -x509 -newkey rsa:4096 -nodes -out cert.pem -keyout key.pem -days 365

The flask app(app.py) has following:

from flask import Flask, jsonify
import ssl

application = Flask(__name__)
application.config.from_object(__name__)

context = ssl.SSLContext()
context.load_cert_chain('cert.pem','key.pem')

@application.route('/')
def default():
    return 'hello world \n'
if __name__ == '__main__':
    application.run(host='0.0.0.0', debug=False, ssl_context = context) 

I have copied both the pem files in the same directory. I already tried with placing the pem files in /tmp folder. Still having same issue.

The Docker file I am using to build image of the app:

FROM *orgs private image*
RUN /path/bin/pip install flask
ADD app.py /app.py
ENTRYPOINT ["python", "app.py"]

Solution

You

ADD app.py /app.py

but don’t

ADD cert.pem /
ADD key.pem /

so they’re not available within the container, thus the FileNotFound.

There are other improvements that can be made (like using requirements.txt file, using subfolders, etc), I recommend reading this https://docs.docker.com/language/python/build-images/

Answered By – msanford

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published