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

Ansible automated operation and maintenance deployment method for Linux system

Ansible is a new automated operation and maintena...

MySQL slow_log table cannot be modified to innodb engine detailed explanation

background Getting the slow query log from mysql....

How to deploy and start redis in docker

Deploy redis in docker First install Docker in Li...

How to create a responsive column chart using CSS Grid layout

I have been playing around with charts for a whil...

Put frameset in body through iframe

Because frameset and body are on the same level, y...

JavaScript canvas to load pictures

This article shares the specific code of JavaScri...

Install mysql offline using rpm under centos 6.4

Use the rpm installation package to install mysql...

A brief analysis of MySQL cardinality statistics

1. What is the cardinality? Cardinality refers to...

Summary of commonly used SQL in MySQL operation tables

1. View the types of fields in the table describe...

Native JavaScript to achieve the effect of carousel

This article shares the specific code for JavaScr...

IE6 distortion problem

question: <input type="hidden" name=...

Tutorial on installing lamp-php7.0 in Centos7.4 environment

This article describes how to install lamp-php7.0...

Example of how to quickly build a Redis cluster with Docker

What is Redis Cluster Redis cluster is a distribu...