title: Outbound WebSocket connection from Docker container does not work
body: <p>I have a simple TypeScript program running on Docker, and this program would like to open a Websocket connection to an outside Websocket server. Something like this</p>
  <pre><code>-----------------------                   ------------------------------
  | My Local Docker     |                   | my.ws-server.somewhere.com |                   
  |                     |                   |                            |
  |    WS client        |---&gt; Internet-----&gt;| WS server                  |
  -----------------------                   ------------------------------
  </code></pre>
  <p>When I run (<code>ts-node test.js</code>) the code with the WS client on my dev environment outside Docker, it works fine and can connect without any issues:</p>
  <pre><code>let ws = new WebSocket(&quot;ws://my.ws-server.somewhere.com&quot;, [], { })
  ws.onopen = () =&gt; {
    console.log('Connection opened ok! ')
  }
  </code></pre>
  <p>However, when I put this some thing inside Docker and run it there, the connection is refused. The program starts the connection attempt as it should, but there is just a timeout and the connection is never opened. I guess this has something to do with Docker network? If I want to open an outbound connection from Docker to the outside world, do I need to define some routes or something?</p>
  <p>My Docker setup</p>
  <p>Dockerfile</p>
  <pre><code>FROM node:12.18.3
  
  RUN mkdir /src
  
  WORKDIR /src
  ADD ./package.json /src/package.json
  RUN npm install
  
  EXPOSE 3000
  </code></pre>
  <p>docker-compose.yml</p>
  <pre><code>version: &quot;3.4&quot;
  services:
    my-service:
        container_name: my-service
        restart: always
        build:
            context: .
            dockerfile: my-service.dockerfile
        volumes:
            - &quot;.:/src&quot;
  </code></pre>
  <p>(I noticed that I can make this work by adding <code>network_mode: &quot;host&quot;</code> to docker compose, but that's not what I would really want to do so that I could keep the normal inbound port mappings)</p>