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:

  • How to edit files in a Confluence docker container? There is no vim and apt-get in it
  • how does Flocker implement image migration while doing container migration between hosts
  • how a host can provide service to containers?
  • Docker - Jenkins Image
  • docker pull through cache for private registry not working
  • node can't find modules that exists in parent node_modules folder
  • <!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).

  • How to fetch Ips of a service in docker swarm cluster ?
  • Catching 404 and redirect to wwwroot
  • Tomcat + MySQL docker container outputting utf8 text with wrong encoding
  • Docker exec printf gives No such file or directory error
  • Rocket.Chat as Docker-Container using sub folder
  • call a docker container from another container
  • One Solution collect form web for “Tornado websocket in docker (net::ERR_CONNECTION_REFUSED)”

    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

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.