Manu
Manu

Reputation: 396

"Connection was reset" error on flask server

I'm having an issue with dockers, I followed official docker tutorial to deploy a web app (and luckily a python/flask one) but when I tried to deploy my app when I come to the connection part it failed and reply "Connection was reset"

Code


    import json
    import threading
    import thread
    import io

    from flask import Flask, render_template, request
    from flask_socketio import SocketIO

    # Global variables
    app = Flask(__name__)

    lock = threading.Semaphore(0)
    IOCReplay.lock = lock

    async_mode = None
    socketio = SocketIO(app)
    IOCReplay.socketio = socketio


    @app.route("/")
    def root():
        return render_template('index.html')

    @app.route("/dependencies")
    def getDependencies():
        data = ''
        with open('./dependencies.json') as data_file:
            data = json.load(data_file)

        return json.dumps(data)

    if __name__ == "__main__":
        socketio.run(app, port=5000)
    docker run -d -P guitest:1
    6d95689601b8(...)

    docker ps
    CONTAINER ID        IMAGE               COMMAND              
    6d95689601b8        guitest:1           "python test.py"   

    CREATED             STATUS              PORTS                       NAMES
    4 seconds ago       Up 2 seconds        0.0.0.0:32771->5000/tcp     loving_boyd

Dockerfile is OK.

Issue

And so when I'm logging into 0.0.0.0:32771 it says "Connection was reset"

I saw from docker FAQ that to correct this problem, i have to "change the service’s configuration on [my] localhost so that the service accepts requests from all IPs"

Upvotes: 6

Views: 13532

Answers (1)

Manu
Manu

Reputation: 396

Ok I have solved all my problems! Thanks to @n2o

The problem was incorrect arguments for socketio.run(app)

Previous:

    from flask import Flask, render_template, request
    from flask_socketio import SocketIO

    # Code here #

    if __name__ == "__main__":
        socketio.run(app, port=5000)

Fixed:

    import os
    from flask import Flask, render_template, request
    from flask_socketio import SocketIO

    # code here #

    if __name__ == "__main__":
        port = int(os.environ.get('PORT', 5000))
        socketio.run(app, host='0.0.0.0', port=port)

Upvotes: 7

Related Questions