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

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

Get the local public IP address through the container. You can use the local IP address to register multiple services.

Changing the command to ping can get different server information.

public static String getIfconfigIP()
  {
    BufferedReader in = null;
    String outline = "";
    // timeOut window is in ms and in linux is in s
    Runtime r = Runtime.getRuntime(); // The ping command to be executed, this command is a 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, and calculate similar occurrences = 23ms
      // Number of times TTL=62 is shown int connectedCount = 0;
      String line = null;
 
      while ((line = in.readLine()) != null)
      {
        outline += line;
      }
      return outline;
    }
    catch (Exception ex)
    {
      ex.printStackTrace(); // If an exception occurs, return false return outline;
    }
    finally
    {
      try
      {
        in.close();
      }
      catch (IOException e)
      {
        e.printStackTrace();
      }
    }
 
  }

The IP in docker is fake. How to get the IP address of the host machine in the program running in docker?

Here are some tips for me

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

Additional knowledge: Docker network problem. The external IP address of the host machine cannot be pinged in the Docker container, but the external IP address of other machines can be pinged.

Problem Description

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

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

Install the Jenkins Docker container on machine A, start it, and enter the container

#docker exec -it jenkins /bin/sh

Then ping the intranet IP of host A in the container, and it works.

#ping A-IP-nei (172.16...) //Returns that ping is successful

#ping A-IP-outside (202.106...) // Ping failed

#ping www.xxx.com // can be correctly resolved to the external IP address of machine A, but ping fails

#ping baidu.com //It can be pinged successfully.

in addition:

1. From the host machine A outside the docker container, ping A-IP-outside (202.106...) is successful.

2. Install the Jenkins container on machine B, and ping A-IP-external (202.106...) from within the container.

2. Install the Jenkins container on machine B. Pinging B-IP-external (202.106…) from within the container is unsuccessful.

Looking for various network problems, deleting containers, images, and reinstalling docker all do not work.

In summary:

How to ping the host's public IP in the docker container?

I need to ping www.xxx.com in the container of machine A, which will be resolved to the intranet address of machine A.

Because the intranet address of machine A can be pinged from within the container.

New 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-outside(202.106…) //The ping is successful, OK meets the requirement.

The above article about how to get the public IP address of a docker container by running it directly through ping is all I have to share with you. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM.

You may also be interested in:
  • Solve the problem that changes to the Docker MySQL container database do not take effect
  • Docker file storage path, get container startup command operation
  • Two ways to exit bash in docker container under Linux
  • Restart all stopped Docker containers with one command
  • Detailed explanation of docker version es, milvus, minio startup commands
  • Docker starts the elasticsearch image and solves the error after mounting the directory
  • Solve the problem of docker log mounting

<<:  Web page comments cause text overflow in IE

>>:  MySQL 8.0.22 decompression version installation tutorial (for beginners only)

Recommend

How to install Jenkins using Docker

Table of contents 1. Pull the image 2. Create a l...

JavaScript realizes the drag effect of modal box

Here is a case of modal box dragging. The functio...

HTML Tutorial: DOCTYPE Abbreviation

When writing HTML code, the first line should be ...

MySQL series 15 MySQL common configuration and performance stress test

1. Common MySQL configuration All the following c...

Detailed explanation of the use of Element el-button button component

1. Background Buttons are very commonly used, and...

MySQL Series Database Design Three Paradigm Tutorial Examples

Table of contents 1. Knowledge description of the...

How to quickly build an LNMP environment with Docker (latest)

Preface Tip: Here you can add the approximate con...

How to explain TypeScript generics in a simple way

Table of contents Overview What are Generics Buil...

Markup language - simplified tags

Click here to return to the 123WORDPRESS.COM HTML ...

Tomcat multi-instance deployment and configuration principles

1. Turn off the firewall and transfer the softwar...

Detailed explanation of the use of Refs in React's three major attributes

Table of contents Class Component Functional Comp...

Install Docker on CentOS 7

If you don't have a Linux system, please refe...

9 ways to show and hide CSS elements

In web page production, displaying and hiding ele...