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.

  • docker stack deploy using the client api
  • Configure Dancer from environment variables?
  • How to run multiple times a Docker container with different parameters in Kubernetes?
  • Docker - proxy container port on host
  • Docker: Different ENV variables for development and production environments
  • No connection to generic driver
  • 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.

  • How to mount docker volume with jenkins docker container?
  • How to restrict cpu usage from host to docker container
  • FTP into existing Docker Containers
  • Use custom docker binary in CoreOS
  • Docker deployment options
  • how to consume rest api of apache nutch docker
  • 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.