java.net.InetAddress java class doesn't resolve IP on Alpine Docker container

When I run this code below inside container Docker running Java JDK 8 on Alpine 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());
    . . . 
  }
}

I get the error:

  • No suitable driver found (jdbc:pgsql)
  • Docker build extra folders
  • Unable to connect to Docker container from host
  • How to know server's IP address where jenkins deploys and builds code
  • Why https is forced?
  • Selecting different code branches when using a shared base image in Docker
  •   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$2.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)
    

    Any Ideas ?

    By the way, I can run the ping mysql and nslookup command successfully.

    # 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
    

    My Dockerfile is very simple:

    FROM frolvlad/alpine-oraclejdk8
    ADD bin /
    WORKDIR /
    

    The files SomaDBTest.java and SomaDBTest.class is in bin directory.

    To run the container you do :

    docker build -t testInetAddress .
    docker run -i -t testInetAddress java SomaDBTest
    

  • “Peer's certificate issuer has been marked as not trusted by the user” in Openshift3
  • OpenWisk Docker Support install fails
  • Docker Experimental FeaturesToolbox
  • docker-compose : Unsupported config option for services service: 'web'
  • When does a docker container stop?
  • Docker image is not visible when I run docker ps
  • One Solution collect form web for “java.net.InetAddress java class doesn't resolve IP on Alpine Docker container”

    You can put the command bellow on your Dockerfile to solve this problem

    RUN echo 'hosts: files mdns4_minimal [NOTFOUND=return] dns mdns4' >> /etc/nsswitch.conf 
    
    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.