I'm new to docker and need to connect from inside a running centos docker container to the host running the container using a java socket. When trying to connect a SocketException occurs:
[root@743fedf46128 test]# java SocketTest 10.1.196.134 4444 127.0.0.1 0
Trying to 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
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at java.net.Socket.connect(Socket.java:528)
at SocketTest.main(SocketTest.java:54)
The Java Class TestSocket actually only tries to open a 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 to 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 it fails on the last line (the connect).
Ping as well as telneting the port from within the running container however 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 from 10.1.196.134: icmp_seq=1 ttl=64 time=0.047 ms
64 bytes from 10.1.196.134: icmp_seq=2 ttl=64 time=0.101 ms
64 bytes from 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 to 10.1.196.134.
Escape character is '^]'.
This is a test
Connection closed by foreign host.
Can anybody provide me a hint, why trying to connect fails with java ? For completness, my Dockerfile looks like this:
FROM centos:centos7
# Set the 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 the test class
ADD dockerfiles/SocketTest.class /opt/test/SocketTest.class
Thank you in advance for your replies.