getting java.net.SocketException: Invalid argument:inside docker container

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.

  • How to run Redis on Docker using docker-compose.yml?
  • Kubernetes NFS volume mount fail with exit status 32
  • Docker fedora hbase JAVA_HOME issue
  • How to manage secrets in a Microservice / Container / Cloud environment?
  • Docker mono dns issue
  • Build with docker into host directory
  • 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.

  • Docker Rails App and postgres
  • Cannot publish: Visual Studio 2015 Tools for Docker
  • Remove docker image depending on an already removed image
  • Dockerfile entrypoint not lunching on interactive mode
  • Docker swarm-manager displays old container information
  • Stopping prometheus docker container gracefully
  • One Solution collect form web for “getting java.net.SocketException: Invalid argument:inside docker container”

    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.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.