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

What to do if you forget the initial password when installing MySQL on Mac

Forgetting the password is a headache. What shoul...

Do you know the common MySQL design errors?

Thanks to the development of the Internet, we can...

How to use ss command instead of netstat in Linux operation and maintenance

Preface When operating and managing Linux servers...

Solution to forget password when installing MySQL on Linux/Mac

Preface This article mainly introduces the releva...

MySQL uses custom sequences to implement row_number functions (detailed steps)

After reading some articles, I finally figured ou...

The problem of form elements and prompt text not being aligned

Recent projects involve the creation of a lot of ...

React sample code to implement automatic browser refresh

Table of contents What is front-end routing? How ...

How to develop Java 8 Spring Boot applications in Docker

In this article, I will show you how to develop a...

Steps to install MySQL using Docker under Linux

As a tester, you may often need to install some s...

How to implement CSS mask full screen center alignment

The specific code is as follows: <style> #t...

Ubuntu 20.04 how to modify the IP address example

illustrate: Today, when continuing the last offic...

Introduction to Kubernetes (k8s)

I had always wanted to learn Kubernetes because i...

Awk command line or script that helps you sort text files (recommended)

Awk is a powerful tool that can perform some task...