Bidirectional socket communication between docker container and host - docker

I want to establish a TCP socket based communication between client and server hosted on a docker and host respectively.
I am trying to run a GCC based socket agent on a ubuntu container running on Docker desktop installed on Windows 10 host. I have done port mapping (-p) to a port where a server runs on Windows 10.
docker run -it --name ubuntu1 -p 5997:5997 ubuntu /bin/bash
Now when I try to run a java socket server on windows 10 host it is showing error that port is already bind. But I have checked no other application is binding on port 5997.
I found that -p binds the host port already, so another service can not bind this. If I run the socket server on host first then starting container fails.
Error response from daemon: Ports are not available: listen tcp 0.0.0.0:5997: bind: Only one usage of each socket address (protocol/network address/port) is normally permitted.
Error: failed to start containers: ubuntu1
What is the correct way to establish a bidirectional socket communication between container and host ,where socket client runs at the container and socket server at the host ?

Related

Can't connect to my broker in a Docker container [duplicate]

This question already has answers here:
Mosquitto: Starting in local only mode
(6 answers)
Closed 1 year ago.
I'm trying to setup mqtt broker in a Docker container. I pulled the following Docker image (https://hub.docker.com/_/eclipse-mosquitto) on my machine and I can successfully launch the Docker container with the following command:
docker run -it -p 1883:1883 -p 9001:9001 --network=host eclipse-mosquitto
If I run it with that command I get the following output:
WARNING: Published ports are discarded when using host network mode
1616081533: mosquitto version 2.0.9 starting
1616081533: Config loaded from /mosquitto/config/mosquitto.conf.
1616081533: Starting in local only mode. Connections will only be possible from clients running on this machine.
1616081533: Create a configuration file which defines a listener to allow remote access.
1616081533: Opening ipv4 listen socket on port 1883.
1616081533: Opening ipv6 listen socket on port 1883.
1616081533: mosquitto version 2.0.9 running
So then I start Mqttfx and I set up a connection to 127.0.0.1 and port 1883 but the MQTT client is unable to connect to my broker. What am I doing wrong?
Okay, let's read those logs :
WARNING: Published ports are discarded when using host network mode
In host mode, the ports exposed by the container are directly accessible from your local machine IP (the container uses your host machine IP address). So you do not need the -p option when launching the container
1616081533: Starting in local only mode. Connections will only be possible from clients running on this machine.
1616081533: Create a configuration file which defines a listener to allow remote access.
It seems that you need to change some configurations for Mosquitto : creating a mosquitto.conf file and looking more precisely at options like bind_addressand listeners
You can find more about that here and here

Error "Failed to open TCP connection to localhost:35729" on Rails app on a Docker container

I am having getting an error message "Failed to open TCP connection to localhost:35729 (Cannot assign requested address - connect(2) for "localhost" port 35729)" when I start my Rails local server on my Docker container by foreman start. I tried to access the server on "localhost:5000". The rails server is configured to be hosted on port 5000 on the localhost.
As for the docker container, I started it from an image by running a docker command
docker run -it --name <my-container-name> -p 5000:5000 -p 35729:35729 -v <host_project-directory>:/home <image-name> /bin/bash
in order to bind the port 35729 on the docker container with the host's port 35729. The port 5000 is where this rails application is configured to run the local server, so I also bound it with the host.
I also confirmed the ports are bound to the local machine by starting the container and run
docker port <my-container-name>
which gave me
35729/tcp -> 0.0.0.0:35729
5000/tcp -> 0.0.0.0:5000
So I'm guessing it is not the problem with port binding.
Why am I getting this error message even though the port is open on the Docker container and is bound to the host machine? I appreciate any advice to be able to connect to the local server running on the container from the host machine.
Notes:
Rails Version: 4.2.11.1
Docker container: Debian GNU/Linux 9

How to access Database using domain name from windonws docker container

Getting error as below:
Error: com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host *(my database server ip address), port *(database port) has failed. Error: "connect timed out. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".
Database string: jdbc:sqlserver://dns:port;DatabaseName=testdb
Using Docker Windows container
DockerfileWar:
FROM openjdk:8
ADD target/dv-2.war dv-2.war
EXPOSE 8085
ENTRYPOINT ["java","-jar","dv-2.war"]
Build image from Project: docker build -f DockerfileWar -t dv-2war .
docker run -p 8085:8085 dv-1war
Getting above error while run the container.
Note: If i used the IP address at DNS than it is working. But i want to do with DNS only. Just for note database running on some other machine(Not on any container). Spring boot application running on docker windows container.
Thanks, Dhaval

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

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.
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

By default, can a docker container call host's localhost UDP?

I have a docker container, and also installed on the VM a daemon listening for UDP on port 8125. The container sends data with UDP protocol on this 8125 port.
I was trying to open the port by starting the container with the -p 8125:8125/udp, but I'm getting the following error:
Error starting userland proxy: listen udp 0.0.0.0:8125: bind: address already in use
Which makes sense because the daemon is already listening on this port.
So how can I configure Docker so that the container can send UDP payloads to the external daemon ?
Opening ports is only needed when you want to Listen for the requests not sending. By default Docker provides the necessary network namespace for your container to communicate to the host or outside world.
So, you could it in two ways:
use --net host in your docker run and send requests to localhost:8125 in this case you containerized app is effectively sharing the host's network stack. So localhost points to the daemon that's already running in your host.
talk to the container network gateway (which is usually 172.17.0.1) or your host's hostname from your container. Then your are able to send packets to your daemon in your host.

Resources