简体   繁体   English

Docker openjdk:8 UnknownHostException

[英]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) .我有使用org.​apache.​commons.​io.FileUtils.copyFile(File srcFile, File destFile)从网络下载文件的自定义 Java 应用程序。 It works perfectly when it runs directly but when I run it from a docker container I get following exception:当它直接运行时它工作得很好,但是当我从 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)

I tried exposing http and https ports on the container with EXPOSE 8080 , EXPOSE 8443 and running with -P option.我尝试使用EXPOSE 8080EXPOSE 8443在容器上公开httphttps端口并使用-P选项运行。 Next thing was running the container with --hostname=127.0.0.1 option.接下来是使用--hostname=127.0.0.1选项运行容器。 Then I found a hack for Dockerfile: RUN echo 'hosts: files mdns4_minimal [NOTFOUND=return] dns mdns4' >> /etc/nsswitch.conf .然后我发现了 Dockerfile 的一个 hack: 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 版本17.10.0-ce, build f4ffd25 ,Dockerfile 基础镜像: openjdk:8

Docker image /etc/hosts contains: 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

The problem is in the DNS server used inside the container.问题在于容器内使用的 DNS 服务器。 It either doesn't match with the hosts DNS or it doesn't hold the requested DNS name.它要么与主机 DNS 不匹配,要么不保存请求的 DNS 名称。

You can solve the issue by passing a well known DNS server during container startup.您可以通过在容器启动期间传递众所周知的 DNS 服务器来解决该问题。

Use使用

--dns=8.8.8.8

to refer to Google DNS.参考谷歌 DNS。

See also the docker documentation for DNS setup.另请参阅docker 文档以了解 DNS 设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM