Docker – Connect a websocket client in a docker container to a websocket server in host

dockerwebsocket

I have a websocket server running in my host, listening to port 8080.
In a docker container, I deployed a websocket client listening to the said server using this snippet:

connect_url="ws://0.0.0.0:80/"

and, exposing/mapping port 80 of the container to port 8080 of the host.

Dockerfile:

EXPOSE 80

When I ran the container:

docker run -p 8080:80 <name>

But I'm getting this error:

docker: Error response from daemon: driver failed programming external connectivity on endpoint : Error starting userland proxy: Bind for 0.0.0.0:8080 failed: port is already allocated.

Now I think this error is because the server in the host is already using port 8080, that's why it can't be mapped.

With these details given, I just wanted to know how can my websocket client inside the docker container connect to the websocket server in the host.

Best Answer

I think problem is port 80 inside your container already in use, not 8080 on your host machine. Try to use another port for connect socket inside your docker container instead 80 (for example 777 port). Then run docker run -p 8080:777 <name>

By the way, check your host machine port already in user or not: sudo lsof -i tcp:8080 If not thing show up, that mean port 8080 not yet used. Incase already in use. Kill that process on port 8080: sudo kill -9 your_PID_ID Then try again