0

I've set up a very simple socket communication using the following Java classes:

Server.java:

public class Server
{
    public static void main(String[] args) throws Exception
    {
        try (ServerSocket listener = new ServerSocket(59090))
        {
            while (true)
            {
                try (Socket socket = listener.accept())
                {
                    Scanner in = new Scanner(socket.getInputStream());

                    while (in.hasNextLine()) 
                    {
                        System.out.println("UPPER CASE: " + in.nextLine().toUpperCase());
                    }
                }
            }
        }
    }
}

Client.java:

public class Client
{
    public static void main(String[] args) throws Exception
    {
        try (Socket socket = new Socket("172.17.0.2", 59090)) 
        {
            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

            while (true) 
            {
                Thread.sleep(1000);
                out.println("hello world");
            }
        }
    }
}

I want to run them as separate Docker containers, so I've created corresponding Dockerfiles and docker-compose.yml:

Dockerfile (Server):

FROM openjdk:13-ea-16-jdk-alpine3.9
RUN mkdir -p /app/server
WORKDIR /app/server
COPY ./Server.java ./
RUN javac Server.java
ENTRYPOINT ["java", "Server"]

Dockerfile (Client):

FROM openjdk:13-ea-16-jdk-alpine3.9
RUN mkdir -p /app/client
WORKDIR /app/client
COPY ./Client.java ./
RUN javac Client.java
ENTRYPOINT ["java", "Client"]

docker-compose.yml:

version: "3.7"
services:
  client:
    image: utku/socket-client:0.1.0
    build: ./client
  server:
    image: utku/socket-server:0.1.0
    build: ./server

After building the images using docker-compose build and running firstly the server then the client using docker run ..., I saw that Server's IP becomes 172.17.0.2 and Client's IP becomes 172.17.0.3. That's why I specify the IP address in the corresponding line in Client.java:

// Client.java
try (Socket socket = new Socket("172.17.0.2", 59090))

Docker documentation states that I should be able to access server from client using its service name as fofllows, which is server inside docker-compose.yml.

// Client.java
try (Socket socket = new Socket("server", 59090)) 

However, when I try to replace this IP address with the service name of server, I get the following error:

Exception in thread "main" java.net.UnknownHostException: server
    at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:224)
    at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:350)
    at java.base/java.net.Socket.connect(Socket.java:620)
    at java.base/java.net.Socket.connect(Socket.java:568)
    at java.base/java.net.Socket.<init>(Socket.java:459)
    at java.base/java.net.Socket.<init>(Socket.java:236)
    at Client.main(Client.java:12)

Could you point me out what I'm missing here?

Utku Ufuk
  • 318
  • 1
  • 10
  • 24
  • if i run docker network inspect bridge it shows me the containers ip mask 172.17.0.0/16. Which means somehow, it doesn't use the (default) network bridge of the docker-compose. Please check your network configuration. Let me know if there is an issue with the network. – Manuel May 04 '19 at 20:35

1 Answers1

0

You need to expose the ports to the network. Your docker-compose file should look like this:

version: "3.7"
services:
  client:
    image: utku/socket-client:0.1.0
    build: ./client
    expose: 
      - "59090"
  server:
    image: utku/socket-server:0.1.0
    build: ./server
    expose: 
      - "59090"
Marcus Held
  • 635
  • 4
  • 15
  • 1
    I can connect to server using the IP "172.17.0.2" without exposing the ports. I shouldn't need to expose the ports just to use service name instead of IP. – Utku Ufuk May 04 '19 at 19:43
  • That's strange. Are you sure that you didn't mix up the ips? Because you need to expose the port in order to access them either by name or ip in another service. So the only explanation that makes sense to me is that you connected to localhost by accident. – Marcus Held May 04 '19 at 19:52
  • Please check the alias of the container. Maybe their names is not just client and server. – Manuel May 04 '19 at 20:04