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:

  • x509: certificate is valid for *, not
  • Receive UDP Multicast in Docker Container
  • How do I forward a docker-machine port to my host port on OSX?
  • How to connect to docker container from browser on host?
  • AWS Elastic Beanstalk with single container docker: Logging parameter in
  • Can I have a writable Docker volume mounted under a read-only volume?
  • <!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).

  • Unable to access web application in Docker container via container's ip and exposed port over http
  • Files in docker container disappear
  • My docker container has no internet
  • What is the optimal way to store data-files for testing using travis-ci + Docker?
  • How to organize microservices projects connection parameters
  • Getting java.lang.OutOfMemoryError thrown at me when running Spark inside Docker
  • 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.