Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 496
jaysonpryde Avatar asked Aug 30 '18 03:08

jaysonpryde


People also ask

How do I connect to WebSocket client?

In order to communicate using the WebSocket protocol, you need to create a WebSocket object; this will automatically attempt to open the connection to the server. The URL to which to connect; this should be the URL to which the WebSocket server will respond.

How do I map a container port to a host port?

To make a port available to services outside of Docker, or to Docker containers which are not connected to the container's network, use the --publish or -p flag. This creates a firewall rule which maps a container port to a port on the Docker host to the outside world.

Can you remote into a Docker container?

The SSH method works fine for Docker containers, too. That said, you can SSH into a Docker container using Docker's built-in docker exec . If you do not need an interactive shell, you can also use the docker attach command to connect the host's stdin and stdout to the running container and execute remote commands.

How do I connect to Docker host?

Use --network="host" in your docker run command, then 127.0. 0.1 in your docker container will point to your docker host. Note: This mode only works on Docker for Linux, per the documentation.


1 Answers

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

like image 143
Truong Dang Avatar answered Oct 05 '22 06:10

Truong Dang