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:

  • Docker compose in production?
  • Docker user namespacing map user in container to host
  • How to connect mysql in a rails appliation with docker?
  • I can't run docker daemon on ubuntu 14.04 LTS (on VPS)
  • Docker run error: could not create session key: too many links
  • Are Docker Cloud nodes managed?
  • <!DOCTYPE html>
            ws = new WebSocket("ws://localhost:8888/ws");
            ws.onmessage = function(evt) {
                console.log("Message Received: " +
            ws.onopen = function(evt) { 
                console.log("***Connection Opened***");
            ws.onclose = function(evt) { 
                console.log("***Connection Closed***");

    And here is my simple tornado websocket solution

    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:
        def on_message(self, message):
            print ('message received:  %s' % message)
            # Reverse Message and send it back
            print ('sending back message: %s' % message[::-1])
        def on_close(self):
            print ('connection closed')
            if self in wss:
        def check_origin(self, origin):
            return True
    application = tornado.web.Application([
        (r'/ws', WSHandler),
    if __name__ == "__main__":
        http_server = tornado.httpserver.HTTPServer(application)
        myIP = socket.gethostbyname(socket.gethostname())
        print ('*** Websocket Server Started at %s***' % myIP)
        main_loop = tornado.ioloop.IOLoop.instance()

    I run python3 Recieve *** Websocket Server Started at***. 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 from docker.
    I run docker container with:

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

    And then run python3 Recieve: *** Websocket Server Started at***.
    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 pass securely SSH Keys to Docker Build?
  • Load Postgres dump after docker-compose up
  • Docker impossible setup sysctl parameter in container RHEL 6.6
  • Update docker network in a swarm cluster without downtime
  • What is the entry point/command required to run an etcd container in ECS?
  • Doesn't Docker port binding obviate the need for Kubernetes' complex networking model?
  • 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.