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


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( has following:

from flask import Flask, jsonify
import ssl

application = Flask(__name__)

context = ssl.SSLContext()

def default():
    return 'hello world \n'
if __name__ == '__main__':'', 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
ENTRYPOINT ["python", ""]




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

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