Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to connect to a IPv6 listener with host.docker.internal #10820

Closed
3 tasks done
leobastiani opened this issue Apr 3, 2021 · 4 comments
Closed
3 tasks done

Unable to connect to a IPv6 listener with host.docker.internal #10820

leobastiani opened this issue Apr 3, 2021 · 4 comments

Comments

@leobastiani
Copy link

leobastiani commented Apr 3, 2021

  • I have tried with the latest version of Docker Desktop
  • I have tried disabling enabled experimental features
  • I have uploaded Diagnostics
  • Diagnostics ID: 463CA6FE-57D8-4251-BAF9-E2B283F65F5C/20210403031902

Actual behavior

It's not possible to connect to a java application running on host within a docker container.

Expected behavior

Be able to connect to a java application like it's possible to connect with any other application.

Information

  • Is it reproducible? Yes.
  • Windows Version: 20H2 19042.662
  • Docker Desktop Version: 3.2.2 (61853)
  • WSL2 or Hyper-V backend? WSL2
  • Are you running inside a virtualized Windows e.g. on a cloud server or a VM: No

Steps to reproduce the behavior

Here is my first post on stackoverflow when I thought it's an issue with docker. https://stackoverflow.com/questions/66894825/docker-container-is-not-able-to-connect-to-my-java-application-listening-on-host
Then I found it works on Ubuntu but it fails on Windows.

  1. docker-compose.yml:
version: "3.8"
services:
  ssh:
    image: rastasheep/ubuntu-sshd
    ports:
      - "2222:22"
  1. connect to it and install netcat
docker-compose up -d
ssh root@localhost -p 2222 # password is root
apt update && apt install -y netcat
  1. run a netcat server on the host
nc -v -l 127.0.0.1 8080
  1. try to connect to it. you will be able to connect successfully
nc -v host.docker.internal 8080
  1. start any java application (an example is written below)
java ./Server.java
  1. try to connect to it and see "Connection refused"
nc -v host.docker.internal 8080

Some screenshots

image
image
image

Server.java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

public class Server implements AutoCloseable {
  private final ServerSocket server;

  public Server(String host, int port, int backlogConnectionQueueLength)
      throws UnknownHostException, IOException {
    InetAddress inetAddress = InetAddress.getByName(host);
    System.out.println("inetAddress: " + inetAddress);
    server = new ServerSocket(port, backlogConnectionQueueLength, inetAddress);
    System.out.println(Thread.currentThread() + " Created Server");
  }

  public static void main(String[] args) {
    try (Server server = new Server("localhost", 8080, 50)) {
      server.start();
    } catch (IOException e) {
      System.out.println(e);
    }
  }

  public void start() {
    System.out.println(Thread.currentThread() + " Server Ready: " + server);
    while (true) {
      acceptAndHandleClient(server);
    }
  }

  private void acceptAndHandleClient(ServerSocket server) {
    System.out.println(Thread.currentThread() + " Waiting for Incoming connections...");
    try (Socket clientSocket = server.accept()) {
      handleNewClient(clientSocket);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  private void handleNewClient(Socket clientSocket) throws IOException {
    System.out.println(Thread.currentThread() + " Received Connection from " + clientSocket);
    BufferedReader is = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
    PrintStream os = new PrintStream(clientSocket.getOutputStream());
    // echo that data back to the client, except for QUIT.
    String line = null;
    while ((line = is.readLine()) != null) {
      System.out.println(Thread.currentThread() + " Server Got => " + line);
      if (line.equalsIgnoreCase("QUIT")) break;
      else {
        System.out.println(Thread.currentThread() + " Server echoing line back => " + line);
        os.println("Response: " + line);
        os.flush();
      }
    }
    System.out.println(Thread.currentThread() + " Server Closing Connection by Sending => Ok");
    os.println("Ok");
    os.flush();
    is.close();
    os.close();
  }

  public void close() throws IOException {
    server.close();
  }
}
@leobastiani
Copy link
Author

Tested on Docker for Mac here (sorry it is in Portuguese) https://www.facebook.com/groups/806364762728291/permalink/4136934296337971/?comment_id=4139144289450305&reply_comment_id=4142867015744699
Tested on Ubuntu on my worker mate's pc and it worked
It just fails on Windows

@leobastiani
Copy link
Author

I just realized how to solve it, When I executed lsof -i :8080 it was marked as a IPv6 type. Then I executed the following command to run the server
java -Djava.net.preferIPv4Stack=true ./Server.java
Finally lsof -i :8080 showed a IPv4 type and then I could connect to it from the container

@leobastiani leobastiani changed the title Unable to connect to a java application on host with host.docker.internal but it is able to connect to any other application Unable to connect to a IPv6 listener with host.docker.internal Apr 3, 2021
@stephen-turner
Copy link
Contributor

Glad you solved it. IPv6 support is not yet complete. This is documented at https://docs.docker.com/docker-for-windows/troubleshoot/#networking-issues. There is an issue on docker/roadmap that you can vote for if this is important to you.

@docker-robott
Copy link
Collaborator

Closed issues are locked after 30 days of inactivity.
This helps our team focus on active issues.

If you have found a problem that seems similar to this, please open a new issue.

Send feedback to Docker Community Slack channels #docker-for-mac or #docker-for-windows.
/lifecycle locked

@docker docker locked and limited conversation to collaborators May 6, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants