0

I have a server and inside this server a docker container is running, I have a shell script inside the container which needs the ip of the server not the ip of the docker container. Is there any way to get that ?

I googled for long time and I wasn't able to find a similar question.

Please help.

Deepak velu
  • 154
  • 9

1 Answers1

1

You can install the iproute2 package and use the ip command to show the default route like this

ip route show default

To just get the IP address, you can do

ip route show default | awk '{print $3}'

To get it into a variable in a script, do

HOSTIP=$(ip route show default | awk '{print $3}')
Hans Kilian
  • 18,948
  • 1
  • 26
  • 35
  • This command gives the ip of the docker container, but I need the ip address of the server on which the docker container is running – Deepak velu Apr 25 '22 at 12:45
  • It gives the address of the host as seen from the container, i.e. the host's address on the bridge network. I don't know how (or if) you can get the host's address on a different network than the bridge network. – Hans Kilian Apr 25 '22 at 12:56