The docker container directly runs through ping to obtain the IP operation of the public network

  • 2021-09-16 08:33:29
  • OfStack

The local IP multi-service registry can be used to obtain the local public network ip through the container

You can get different server information by changing the command to ping


public static String getIfconfigIP()
  {
    BufferedReader in = null;
    String outline = "";
    // timeOut window For ms linux  For s
    Runtime r = Runtime.getRuntime(); //  To be executed ping Command , This command is windows Format command 
    String pingCommand = " curl ifconfig.me ";// windows System 
    String os = System.getProperty("os.name").toLowerCase();
    if (os.indexOf("linux") >= 0)
    {
      pingCommand = " curl ifconfig.me ";
    }
    if (os.indexOf("mac") >= 0)
    {
      pingCommand = " curl ifconfig.me ";
    }
    try
    { //  Execute the command and get the output 
      System.out.println(pingCommand);
      Process p = r.exec(pingCommand);
      if (p == null)
      {
        return "cmd failed";
      }
      in = new BufferedReader(new InputStreamReader(p.getInputStream())); //  Check the output line by line , Calculations appear similarly =23ms
      // TTL=62 Number of words 
      int connectedCount = 0;
      String line = null;
 
      while ((line = in.readLine()) != null)
      {
        outline += line;
      }
      return outline;
    }
    catch (Exception ex)
    {
      ex.printStackTrace(); //  False is returned if an exception occurs 
      return outline;
    }
    finally
    {
      try
      {
        in.close();
      }
      catch (IOException e)
      {
        e.printStackTrace();
      }
    }
 
  }

docker inside ip is false, how to run in docker program to obtain the host host ip address

Here are some tips for me


p = require('child_process')
   .spawnSync('curl', ['ifconfig.io'])
   .stdout
   .toString()
   .trim()

Supplementary knowledge: docker network problem, in docker container, ping cannot be connected to the external network ip of the host machine, but ping can be connected to the external network ip of other machines

Problem description

Machine A and machine B each have their own internal network IP and external network IP, for example: A-IP-internal, A-IP-external

B-IP-inside, B-IP-outside, A machine has one domain name www. xxx. com

Install the docker container of jenkins on the A machine, and enter the container after starting

#docker exec -it jenkins /bin/sh

Then in the container ping host A intranet IP, ping can be connected

# ping A-IP-inside (172.16 …)//Return ping

# ping A-IP-OUR (202.106 …)//ping NOT

# ping www. xxx. com//can be correctly parsed as IP of A, but ping does not work

# ping baidu. com//can be connected to ping.

In addition:

1. The host machine A, ping A-IP-outside the docker container (202.106 …) is open.

2. Install the jenkins container on the B machine. ping A-IP-outside the container (202.106 …) is open.

2. Installing an jenkins container on an B machine, inside the container ping B-IP-outside (202.106 …) is impassable.

Finding various network problems, removing containers, mirroring, and reloading docker will not work.

In summary:

In the docker container, the public network IP of the ping host itself is different from the ping

I need to resolve the intranet address of the A machine in the container of the A machine, ping, www. xxx. com.

Because ping can pass the intranet address of A machine in the container.

Newly modified docker-compose file

extra_hosts:

- " www.xxx.com:172.16.xxx.A"

After rebuilding the docker container, enter the docker container

# ping A-IP-OUR (202.106 …)//Correct ping ON, OK meets requirements.


Related articles: