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.
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With