Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start docker with flask application using ssl [duplicate]

I've got a flask application with SSL authorization. Here is my run.py:

#!flask/bin/python
from app import app
import ssl
ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
ctx.load_cert_chain('sertnew/se-emulator.crt', 'sertnew/se-emulator.key')
app.run(debug=True, host='127.0.0.1', port=5000, ssl_context=ctx)

On my machine, I run it simply with python run.py Then I open https://localhost:5000 in chrome and it works (there is a message of non-secure connection, but it's ok for me)

Now I'm trying to make it work in Docker container. I've got a Dockerfile like this:

FROM python:3.5-slim
RUN apt-get update && apt-get install -y python3-pip
COPY . /storage-emulator
WORKDIR /storage-emulator
RUN pip3 install -r requirements.txt
EXPOSE 5000
ENTRYPOINT ["python"]
CMD ["run.py"]

and try to run it in different ways. I can see "Running on https://127.0.0.1:5000/ (Press CTRL+C to quit)" message, but can't open the page in the browser. What am I doing wrong?

like image 885
Julia Aleksandrova Avatar asked Feb 13 '18 11:02

Julia Aleksandrova


1 Answers

This is a rather easy fix, you have to change this line:

app.run(debug=True, host='127.0.0.1', port=5000, ssl_context=ctx)

to

app.run(debug=True, host='0.0.0.0', port=5000, ssl_context=ctx)

You have to think from the containers' perspective: The container has its own "localhost", which is different from the localhost of the host machine, all of that means that flask has never received the request.

Therefore you can simply bind to all IPs within the container, which is done by binding to "0.0.0.0".

like image 138
isset Avatar answered Oct 25 '22 13:10

isset