0

I have a java program that runs inside a docker container. This program tries to connect to a Ipv4 socket. I get the following Invalid argument exception

Caused by: java.net.SocketException: Invalid argument: /135.250.15.104:6524
    at sun.nio.ch.Net.connect0(Native Method)
    at sun.nio.ch.Net.connect(Net.java:454)
    at sun.nio.ch.Net.connect(Net.java:446)
    at sun.nio.ch.SocketChannelImpl.connect(SocketChannelImpl.java:648)
    at io.netty.channel.socket.nio.NioSocketChannel.doConnect(NioSocketChannel.java:209)
    at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.connect(AbstractNioChannel.java:199)
    at io.netty.channel.DefaultChannelPipeline$HeadContext.connect(DefaultChannelPipeline.java:1089)
    at io.netty.channel.AbstractChannelHandlerContext.invokeConnect(AbstractChannelHandlerContext.java:543)
    at io.netty.channel.AbstractChannelHandlerContext.connect(AbstractChannelHandlerContext.java:528)
    at io.netty.handler.ssl.SslHandler.connect(SslHandler.java:415)
    at io.netty.channel.AbstractChannelHandlerContext.invokeConnect(AbstractChannelHandlerContext.java:543)
    at io.netty.channel.AbstractChannelHandlerContext.connect(AbstractChannelHandlerContext.java:528)
    at io.netty.channel.ChannelOutboundHandlerAdapter.connect(ChannelOutboundHandlerAdapter.java:47)
    at io.netty.channel.AbstractChannelHandlerContext.invokeConnect(AbstractChannelHandlerContext.java:543)
    at io.netty.channel.AbstractChannelHandlerContext.connect(AbstractChannelHandlerContext.java:528)
    at io.netty.channel.DefaultChannelPipeline.connect(DefaultChannelPipeline.java:914)
    at io.netty.channel.AbstractChannel.connect(AbstractChannel.java:208)
    at io.netty.bootstrap.Bootstrap$2.run(Bootstrap.java:167)
    at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:357)
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:357)
    at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:111)
    at io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:137)

Now I have tried setting -Djava.net.preferIPv4Stack=true JVM option, but no luck.

I am using open-jdk 8 and from java:openjdk-8u72-jre is the entry in my DockerFile . Note that the same IP address is ping-able from the docker container.

1 Answer 1

1

The problem was solved when I used --net=host see docker reference in my docker run command and network_mode : "host" in my docker-compose file see compose reference.

Also see a related stackoverflow post. The problem seems to get exposed only when the socket connection uses a per-defined local socket address instead of letting the NIO choose a free local address.

But the exception stack trace says "Invalid argument" for the remote address which kind of is weird and that is why it took time for me to figure out the problem.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.