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?