Search code examples
dockerwebsockettornado

Tornado websocket in docker (net::ERR_CONNECTION_REFUSED)


I know how to establish websocket handshake between server and client from my local machine. But I can't realise, where should I dig in order to perform this from my docker container. I always get net::ERR_CONNECTION_REFUSED

Here is my client side client.html:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script>

        ws = new WebSocket("ws://localhost:8888/ws");

        ws.onmessage = function(evt) {
            console.log("Message Received: " + evt.data)
        }
        ws.onopen = function(evt) { 
            console.log("***Connection Opened***");
        }
        ws.onclose = function(evt) { 
            console.log("***Connection Closed***");
        }

    </script>
</head>

</html>

And here is my simple tornado websocket solution server.py:

import tornado.httpserver
import tornado.websocket
import tornado.ioloop
import tornado.web
import socket


class WSHandler(tornado.websocket.WebSocketHandler):
    def open(self):
        print ('new connection')
        if self not in wss:
            wss.append(self)

    def on_message(self, message):
        print ('message received:  %s' % message)
        # Reverse Message and send it back
        print ('sending back message: %s' % message[::-1])
        self.write_message(message[::-1])

    def on_close(self):
        print ('connection closed')
        if self in wss:
            wss.remove(self)

    def check_origin(self, origin):
        return True


application = tornado.web.Application([
    (r'/ws', WSHandler),
])


if __name__ == "__main__":
    http_server = tornado.httpserver.HTTPServer(application)
    http_server.listen(8888)
    myIP = socket.gethostbyname(socket.gethostname())
    print ('*** Websocket Server Started at %s***' % myIP)
    main_loop = tornado.ioloop.IOLoop.instance()
    main_loop.start()

I run python3 server.py. Recieve *** Websocket Server Started at 5.255.232.98***. Then, when I open client.html in browser (just a file), and print in console ws.send('hooray'), I recieve the reversed variant Message Received: yarooh. Everything is OK.


Then I try to launch my server.py from docker. I run docker container with:

docker run -ti -d -p 80:80 -v <path to server.py folder>:/root --name <my_name> <my_container>

And then run python3 server.py. Recieve: *** Websocket Server Started at 172.17.0.2***. So then, when I open client.html again, I recieve the following error:

client.html:7 WebSocket connection to 'ws://localhost:8888/ws' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED

I think the problem should be in my docker run -p 80:80 option, but I don't know how to fix it. I need docker run -p 80:80 because I have django server in it also (I am not running nginx and Django during the experiment).


Solution

  • Found the answer quite 15 minutes after posting, but hours of surfing before:)

    Should add only -p 8888:8888 to my docker run expression. The result is: docker run -ti -d -p 80:80 -p 8888:8888 -v :/root --name