networking - java.net.SocketException when accessing docker host -
i'm new docker , need connect inside running centos docker container host running container using java socket. when trying connect socketexception occurs:
[root@743fedf46128 test]# java sockettest 10.1.196.134 4444 127.0.0.1 0 trying connect using ip address using remote ip 10.1.196.134 using remote port 4444 using local ip 127.0.0.1 using local port 0 /10.1.196.134, 4444, /127.0.0.1, 0 java.net.socketexception: invalid argument or cannot assign requested address @ java.net.plainsocketimpl.socketconnect(native method) @ java.net.abstractplainsocketimpl.doconnect(abstractplainsocketimpl.java:339) @ java.net.abstractplainsocketimpl.connecttoaddress(abstractplainsocketimpl.java:200) @ java.net.abstractplainsocketimpl.connect(abstractplainsocketimpl.java:182) @ java.net.sockssocketimpl.connect(sockssocketimpl.java:392) @ java.net.socket.connect(socket.java:579) @ java.net.socket.connect(socket.java:528) @ sockettest.main(sockettest.java:54)
the java class testsocket tries open socket:
public static void main(string[] args) { string remoteip = args[0]; string remoteport = args[1]; string localip = args[2]; string localport = args[3]; system.out.println("trying connect using ip address"); system.out.println("using remote ip " + remoteip); system.out.println("using remote port " + remoteport); system.out.println("using local ip " + localip); system.out.println("using local port " + localport); socket worksocket = null; try { inetaddress remoteaddress = inetaddress.getbyname(remoteip); inetaddress localaddress = inetaddress.getbyname(localip); int rport = integer.parseint(remoteport); int lport = integer.parseint(localport); system.out.println(remoteaddress + ", " + rport + ", " + localaddress + ", " + lport); worksocket = new socket(); worksocket.setreuseaddress(true); worksocket.bind(new inetsocketaddress(localaddress, lport)); worksocket.setsotimeout(1000); worksocket.connect(new inetsocketaddress(remoteaddress, rport)); }
and fails on last line (the connect).
ping telneting port within running container work:
[admin@pluto ~]$ docker exec -it --user root pensive_almeida bash [root@743fedf46128 test]# ping -c 3 10.1.196.134 ping 10.1.196.134 (10.1.196.134) 56(84) bytes of data. 64 bytes 10.1.196.134: icmp_seq=1 ttl=64 time=0.047 ms 64 bytes 10.1.196.134: icmp_seq=2 ttl=64 time=0.101 ms 64 bytes 10.1.196.134: icmp_seq=3 ttl=64 time=0.099 ms --- 10.1.196.134 ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 1999ms rtt min/avg/max/mdev = 0.047/0.082/0.101/0.026 ms [root@743fedf46128 test]# telnet 10.1.196.134 4444 trying 10.1.196.134... connected 10.1.196.134. escape character '^]'. test connection closed foreign host.
can provide me hint, why trying connect fails java ? completness, dockerfile looks this:
from centos:centos7 # set proxies env http_proxy 'http://175.23.236.88:3128/' env https_proxy 'http://175.23.236.88:3128/' # install telnet run yum -y install telnet run yum -y install wget # prepare environment env java_home /opt/java env path $path:$java_home/bin # install oracle java7 run wget --no-check-certificate --no-cookies --header "cookie:oraclelicense=accept-securebackup-cookie" \ http://download.oracle.com/otn-pub/java/jdk/7u80-b15/jdk-7u80-linux-x64.tar.gz && \ tar -xvf jdk-7u80-linux-x64.tar.gz && \ rm jdk*.tar.gz && \ mv jdk* ${java_home} workdir /opt/test # add test class add dockerfiles/sockettest.class /opt/test/sockettest.class
thank in advance replies.
you add --net=host docker run command, in way host has full access container. can expose port within dockerfile or on outside in docker run command, prefer former.
here's example of mine:
pull base image
from ubuntu:14.04
run apt-get update run apt-get install software-properties-common -y run add-apt-repository ppa:webupd8team/java -y run apt-get update run echo debconf shared/accepted-oracle-license-v1-1 select true | debconf-set-selections run apt-get install oracle-java8-installer -y run apt-get install oracle-java8-set-default -y run mkdir /temporary add client.jar /temporary
workdir /temporary
expose 15000
entrypoint /bin/bash
Comments
Post a Comment