CGH Tompkins
CGH Tompkins

Reputation: 565

Docker openjdk:8 UnknownHostException

I have custom java application that downloads files from web using org.​apache.​commons.​io.FileUtils.copyFile(File srcFile, File destFile). It works perfectly when it runs directly but when I run it from a docker container I get following exception:

Exception in thread "main" java.net.UnknownHostException: <MY HOST>
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at sun.net.NetworkClient.doConnect(NetworkClient.java:175)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:463)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:558)
at sun.net.www.http.HttpClient.<init>(HttpClient.java:242)
at sun.net.www.http.HttpClient.New(HttpClient.java:339)
at sun.net.www.http.HttpClient.New(HttpClient.java:357)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1220)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1156)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1050)
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:984)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1564)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1492)
at org.apache.commons.io.FileUtils.copyURLToFile(FileUtils.java:1506)

I tried exposing http and https ports on the container with EXPOSE 8080, EXPOSE 8443 and running with -P option. Next thing was running the container with --hostname=127.0.0.1 option. Then I found a hack for Dockerfile: RUN echo 'hosts: files mdns4_minimal [NOTFOUND=return] dns mdns4' >> /etc/nsswitch.conf. Nothing helped. Do you have any suggestions how to solve this?

Docker version 17.10.0-ce, build f4ffd25, Dockerfile base image: openjdk:8

Docker image /etc/hosts contains:

127.0.0.1       localhost
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2      afa4800849e0

Upvotes: 3

Views: 6193

Answers (1)

Sasha Shpota
Sasha Shpota

Reputation: 10300

The problem is in the DNS server used inside the container. It either doesn't match with the hosts DNS or it doesn't hold the requested DNS name.

You can solve the issue by passing a well known DNS server during container startup.

Use

--dns=8.8.8.8

to refer to Google DNS.

See also the docker documentation for DNS setup.

Upvotes: 4

Related Questions