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