skip to Main Content

I’m currently coding a client-server java application. I use the java socket api for sending data from the client to the server and vice-versa.
The server is running on windows on port 9001.
The client is in a docker container on the same machine.
What port do I have to bind in docker so that the client in the docker container can connect to the server?

I’ve tried binding the server port, which obviously didn’t work because it’s already taken by the server.

2

Answers


  1. Connect to the server’s IP:9001. What won’t work is if you try to connect to localhost, as from inside the docker container it would look like connecting to the same container.

    Of course your server process needs to listen to IP:9001, and listening to localhost:9001 is not sufficient.

    Regarding binding: The server opens a port for listening, and that is called binding. The client opens a port for sending (but this sending port address is taken from the ephemeral port range and thus irrelevant) and uses that port to connect to the server port.

    See also:

    Login or Signup to reply.
  2. The client needs to connect to the host on port 9001.

    The common way to do that is to add the host-gateway as an extra host by adding --add-host=host.docker.internal:host-gateway as a parameter to your docker run command.

    Then the client can connect to your server program using host.docker.internal as the hostname (and 9001 as the port name).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search