java.net.InetAddress java class 无法解析 Alpine Docker 容器上的 IP
java.net.InetAddress java class doesn't resolve IP on Alpine Docker container
当我运行这个容器下面的代码Docker运行ningJavaJDK8 高山Linux
import java.io.*;
import java.util.*;
import java.net.*;
public class SomaDBTest {
public static void main(String... args) throws Throwable {
InetAddress ip = InetAddress.getByName("mysql");
System.out.println("Begin - mysql IP Addr = " + ip.getHostAddress());
. . .
}
}
我收到错误:
Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF8
Exception in thread "main" java.net.UnknownHostException: mysql: unknown error
at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
at java.net.InetAddress.lookupAllHostAddr(InetAddress.java:928)
at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1323)
at java.net.InetAddress.getAllByName0(InetAddress.java:1276)
at java.net.InetAddress.getAllByName(InetAddress.java:1192)
at java.net.InetAddress.getAllByName(InetAddress.java:1126)
at java.net.InetAddress.getByName(InetAddress.java:1076)
at SomaDBTest.main(SomaDBTest.java:52)
有什么想法吗?
顺便说一下,我可以 运行 ping mysql
和 nslookup
命令成功。
# ping mysql
PING mysql (172.17.0.2): 56 data bytes
64 bytes from 172.17.0.2: seq=0 ttl=64 time=0.185 ms
64 bytes from 172.17.0.2: seq=1 ttl=64 time=0.283 ms
64 bytes from 172.17.0.2: seq=2 ttl=64 time=0.424 ms
# nslookup mysql
Server: (null)
Address 1: ::1 localhost
Address 2: 127.0.0.1 localhost
Name: mysql
Address 1: 172.17.0.2 mysql
我的Docker文件很简单:
FROM frolvlad/alpine-oraclejdk8
ADD bin /
WORKDIR /
文件 SomaDBTest.java
和 SomaDBTest.class
在 bin
目录中。
到运行你做的容器:
docker build -t testInetAddress .
docker run -i -t testInetAddress java SomaDBTest
你可以在你的 Dockerfile
上输入下面的命令来解决这个问题
RUN echo 'hosts: files mdns4_minimal [NOTFOUND=return] dns mdns4' >> /etc/nsswitch.conf
当我运行这个容器下面的代码Docker运行ningJavaJDK8 高山Linux
import java.io.*;
import java.util.*;
import java.net.*;
public class SomaDBTest {
public static void main(String... args) throws Throwable {
InetAddress ip = InetAddress.getByName("mysql");
System.out.println("Begin - mysql IP Addr = " + ip.getHostAddress());
. . .
}
}
我收到错误:
Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF8
Exception in thread "main" java.net.UnknownHostException: mysql: unknown error
at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
at java.net.InetAddress.lookupAllHostAddr(InetAddress.java:928)
at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1323)
at java.net.InetAddress.getAllByName0(InetAddress.java:1276)
at java.net.InetAddress.getAllByName(InetAddress.java:1192)
at java.net.InetAddress.getAllByName(InetAddress.java:1126)
at java.net.InetAddress.getByName(InetAddress.java:1076)
at SomaDBTest.main(SomaDBTest.java:52)
有什么想法吗?
顺便说一下,我可以 运行 ping mysql
和 nslookup
命令成功。
# ping mysql
PING mysql (172.17.0.2): 56 data bytes
64 bytes from 172.17.0.2: seq=0 ttl=64 time=0.185 ms
64 bytes from 172.17.0.2: seq=1 ttl=64 time=0.283 ms
64 bytes from 172.17.0.2: seq=2 ttl=64 time=0.424 ms
# nslookup mysql
Server: (null)
Address 1: ::1 localhost
Address 2: 127.0.0.1 localhost
Name: mysql
Address 1: 172.17.0.2 mysql
我的Docker文件很简单:
FROM frolvlad/alpine-oraclejdk8
ADD bin /
WORKDIR /
文件 SomaDBTest.java
和 SomaDBTest.class
在 bin
目录中。
到运行你做的容器:
docker build -t testInetAddress .
docker run -i -t testInetAddress java SomaDBTest
你可以在你的 Dockerfile
上输入下面的命令来解决这个问题
RUN echo 'hosts: files mdns4_minimal [NOTFOUND=return] dns mdns4' >> /etc/nsswitch.conf