0

I have a python flask application that is running in a docker container. I have the following code in my flask app:

status.route("/ipaddr", methods=["GET"])
@jwt_required()
def get_external_ip():
    result = subprocess.check_output("ifconfig | grep enp1s0 -A 1 | grep 192 | awk '{print $2}'", shell=True)
    return jsonify(ip_address=result.decode('utf-8').strip())

The command inside check_output is correct. My problem is, this is returning the IP address of the docker image and not the server. I need the IP of the server that comes out as output of this command. When I ran this command in my server bash, it works fine and gives me what i want but since flask is running inside docker, it is not giving me the IP I want. So, my question is: how do I run this command in the SERVER while my flask app is running in a docker container inside that server?

Thanks.

davidism
  • 121,510
  • 29
  • 395
  • 339
SSubedi
  • 79
  • 1
  • 11
  • Use https://docs.docker.com/network/host/ – Diego Torres Milano Mar 09 '23 at 17:20
  • By design, you can't. Several of the answers to [How to get the IP address of the docker host from inside a docker container](https://stackoverflow.com/questions/22944631/how-to-get-the-ip-address-of-the-docker-host-from-inside-a-docker-container) have various techniques, but the most robust is essentially to find a valid host IP address from outside of Docker and then inject it via an environment variable. Why do you want this address? – David Maze Mar 09 '23 at 17:41

1 Answers1

0

Unsure of which IP address, you mean, if you want the PUBLIC, ie. the IP that the world sees you as, you might go with:

curl ifconfig.co
  • Actually, this is my own raspberry-pi like device that runs the server and front-end in separate container. The world cannot even see this. What I want is the IP address assigned by the router to this device. It will be a 192.x.x.x IP which is what the bash command gives me. Again, the command gives me exactly what i want, but only when i run it in the server. when i run this command in flask, it will try to get 192.x.x.x ip of the docker container, which doesn't even exist. so it returns empty string. Does that make sense? Let me know if you need more clarification – SSubedi Mar 09 '23 at 17:09