How to enabe the web socket connection in docker

Hello i am building the react project in docker i get this error

WebSocket connection to 'ws://localhost:33647/' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED

can anyone help me out

You need to provide more information. What containers are you using? are you trying connect to host port?

FROM node:8.9.0

WORKDIR /reactapp

EXPOSE 1234

ADD package.json /reactapp
ADD yarn.lock /reactapp

RUN yarn install

ADD . /reactapp

Here is my docker file please have a look

You have put that you are exposing 1234, but you are trying to connect via port 33647.

You need to expose the ports needed for the websocket

Expose is a documentation thing used to automate mapping of port with -P (uppercase)

you do NOT need to expose to use a port or map it…

and while I agree the container SAYS it is using port 1234, that really looks like a randomly selected value
and NOT what the app IN the container is actually using… THAT value is THE most important. when mapping ports to the docker host

also, never use ‘localhost’ because it is different inside every system… each containers ‘localhost’ is INSIDE their respective containers. and the docker host has one too… and none of these are the same.

while exposing that port it says such port doesn’t exist

can you please illustrate through example as i am not getting it

an application uses a port. 80 for http for example, or 8080… 443 for https, 22 for ssh, etc…

docker doesn’t know. as there is no way for docker to discover what ports are used by an application.
SO, how would you auto map any/all the ports an app in container might be using, onto the docker host?

well, you would need to TELL docker that an app in a container, INTENDS to use a port
welcome to Expose… it does NOT CAUSE the port to be used by the application…

I can write EXPOSE 80
but if I never start a web server, the port is not ACTUALLY being used.

-P (capital), will read the image info, and assign ports to all ‘exposed’ ports. no idea if any of those will actually be used by an application IN the container…

so, you said in your dockerfile
EXPOSE 1234

(to me that seems a little weird, never known an app that uses that port)…

anyhow…
then u did this

ws://localhost:33647/

33647 does not map to 1234 anywhere… you didn’t show the docker run command

also, localhost is a trouble maker… localhost in the docker host is different that localhost in a container

so how does the docker host (localhost) get from port 33647 to the container 1234, AND… what app is actually using port 1234.


seems to indicate that the default port is 8081