Skip to content

DNS resolution in a Java Docker container fails #6844

@JacekCala

Description

@JacekCala

Netty version: 4.1.12.Final
Docker image: openjdk:8-jre

Context:
Running DNS resolution in a Java docker container fails. DNS resolution should complete successfully and return the IP address of the sibling docker container. Instead it throws the following exception:

Exception in thread "main" java.util.concurrent.ExecutionException: java.net.UnknownHostException: failed to resolve 'recomp-hdb' after 3 queries
    at io.netty.util.concurrent.AbstractFuture.get(AbstractFuture.java:41)
    at TestNetty.main(TestNetty.java:15)
Caused by: java.net.UnknownHostException: failed to resolve 'recomp-hdb' after 3 queries
    at io.netty.resolver.dns.DnsNameResolverContext.finishResolve(DnsNameResolverContext.java:688)
    at io.netty.resolver.dns.DnsNameResolverContext.tryToFinishResolve(DnsNameResolverContext.java:620)
    at io.netty.resolver.dns.DnsNameResolverContext$3.operationComplete(DnsNameResolverContext.java:313)
    at io.netty.util.concurrent.DefaultPromise.notifyListener0(DefaultPromise.java:507)
    ...

DNS resolution with plain Java code works as expected and returns the IP address.

The Netty-based code that throws the exception:

import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioDatagramChannel;
import io.netty.resolver.dns.DnsNameResolverBuilder;
import io.netty.resolver.dns.DnsNameResolver;
import io.netty.resolver.dns.DnsServerAddresses;
public class TestNetty {
  private static NioEventLoopGroup bossGroup = new NioEventLoopGroup();
  
  public static void main(String[] args) throws Exception {
    DnsNameResolver res = new DnsNameResolverBuilder(bossGroup.next())
        .channelType(NioDatagramChannel.class)
        .build();

    System.out.println(res.resolve("recomp-hdb").await().get());
  }
}

The plain Java code which works fine:

import java.net.InetAddress;
public class Test {
    public static void main(String args[]) throws Exception {
            InetAddress address = InetAddress.getByName("recomp-hdb");
            System.out.println(address.getHostAddress());
    }
}

Steps to reproduce:

  1. Create a docker compose file that defines two docker containers, one of which is based on openjdk image.
  2. Build the code shown above, changing the name of the sibling container according to your docker compose.
  3. Copy in the code to the java container and run.
  4. The plain java code will show the correct response. The netty-based code will throw the exception as above.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions