Connection was reset on a flask web app

==========

Hello everybody!

I’m actually apprentice in the IoT world, I’m learning how to integrate application (python and js at the moment) on several services such as cloudfoundry or dockers!

  • fio: blocksize too large for data set
  • Gracefully stopping celery in docker container
  • How to Host Ghost Blog on Home Web Server
  • How do I undo the command $ eval “$(docker-machine env blog)”
  • Writing to docker volume from Dockerfile does not work
  • Dockerfile: Permission denied during build when running ssh-agent on /tmp
  • I’m having an issue with dockers, I followed official docker tutorial to deploy a web app (and luckily a python/flask one :D) 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)
    

    on bash

    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: OK

    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”

    It’s my first question on Stack Overflow, so if I have made a mistake or i’m a bit confusing or my english is trash I apologize just tell me how to improve myself!

    Thank you for your time and I wish you a good year 2017! 🙂


    Evolution of the topic

    Ok it looks like it’s from my code there is a problem (because training/webapp from tutorial was working I had doubt about possible localhost conflicts) but i’m not even 40% sure about what I’m saying haha

    it looks like flask-socketio is a bit touchy on the parameters so I replaced by:
    import os
    from flask import Flask, render_template, request
    from flask_socketio import SocketIO

    @app.route('/')
    def hello():
        return render_template('index.html')
    
    if __name__ == '__main__':
        # Bind to PORT if defined, otherwise default to 5000.
        port = int(os.environ.get('PORT', 5000))
        socketio.run(app, host='0.0.0.0', port=port)
    

    and I also removed all the import (threading, json, other .py calls etc)
    and now, at least, it shows a non-fonctional GUI

    I think I’ll let the topic on because I think the problem will show again as I will import exotic things such as threading or DB connection @_@

  • can't connect to mysql docker when mount a directory
  • Install mysql in dockerfile?
  • Installing composer in a docker container
  • Can't access postgresql in a docker container from a VM
  • Is it possible to modify docker run option without removing and recreating the container?
  • Why is Jenkins on the docker host responding to HTTP requests from inside containers?
  • One Solution collect form web for “Connection was reset on a flask web app”

    Ok I have solved all my problems! 😀
    My manager will be so happy haha thank you @n2o

    So yes the problem was incorrect arguments for socketio.run(app)

    I repost the correction

    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)
    
    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.