My flask app is running inside docker. I am trying to connect using a client on host machine but the client hangs after making an initial connection.
When I run the client inside the same container the clients works just fine. And I see this in flask logs.
(1647) accepted ('127.0.0.1', 44474)
(1647) accepted ('127.0.0.1', 44478)
I think this happens because host ports are not mapped to the container port. Is there a way I can specify a port range for socketio and map them on the docker container.
Socket.IO is a web based protocol, it uses whatever port you put the Flask application on. What port are you currently connecting to when you do it from inside the container?
The flask app is on default port 5000. And the in my client is below.
if __name__ == '__main__':
with SocketIO('localhost', 5000, LoggingNamespace) as socket:
And you are exposing port 5000 in your container and mapping it to a port on the host?
Yes this is what the docker run looks like
docker run -it --name <container-name> -p 5000:5000 <image-name> bash
I have tried to use the container IP in the client as well but it doesn't seem to work
I'm not sure what's wrong then. Socket.IO is implemented on top of HTTP and WebSocket requests, so there's really nothing else you need to expose in terms of ports.
What happens if you run a regular Flask app inside a container? Does that work well from the outside?
My server was binding to localhost instead it should bind to 0.0.0.0
$ export FLASK_APP=myapp.py
$ flask run -h 0.0.0.0
solved the issue. Thanks a lot for pointing me towards the solution.
Most helpful comment
My server was binding to localhost instead it should bind to 0.0.0.0
solved the issue. Thanks a lot for pointing me towards the solution.