Docker openjdk:8 UnknownHostException

时间:2018-01-10 10:06:57

标签: java docker openjdk

我有自定义java应用程序,使用org.​apache.​commons.​io.FileUtils.copyFile(File srcFile, File destFile)从Web下载文件。它直接运行时效果很好但是当我从docker容器运行时,我得到以下异常:

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)

我尝试在httphttps并使用EXPOSE 8080选项运行的容器上展示EXPOSE 8443-P个端口。接下来就是使用--hostname=127.0.0.1选项运行容器。然后我发现了Dockerfile的一个hack:RUN echo 'hosts: files mdns4_minimal [NOTFOUND=return] dns mdns4' >> /etc/nsswitch.conf。什么都没有帮助。你对如何解决这个问题有什么建议吗?

Docker版本17.10.0-ce, build f4ffd25, Dockerfile基础映像:openjdk:8

Docker图像/etc/hosts包含:

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

1 个答案:

答案 0 :(得分:1)

问题出在容器内使用的DNS服务器中。它要么与主机DNS不匹配,要么它不会保留所请求的DNS名称。

您可以通过在容器启动期间传递一个众所周知的DNS服务器来解决此问题。

使用

--dns=8.8.8.8

指Google DNS。

另请参阅the docker documentation了解DNS设置。