I want to get the IP address of a container, I've looked around and chose to add this shell script in my ~/.bashrc
:
docker_ip() {
docker inspect --format '{{ .NetworkSettings.IPAddress }}' "$@"
}
But cuz the containers were connected by custom network, so as below:
"NetworkSettings": {
"Bridge": "",
"SandboxID": "e5b42ebd28c5e00e72ab1b7036bf4b1def4036f75ef66994d14a895fd82929bf",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/e5b42ebd28c5",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "",
"Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"MacAddress": "",
"Networks": {
"mynet": {
"IPAMConfig": null,
"Links": null,
"Aliases": [
"0819292faeb4"
],
"NetworkID": "4d63adc0879a36550310b50d284718772fe81d01d4ec19a6dc919ad7817c1a3d",
"EndpointID": "5bfa1bbaa2c446bc5e93d4f14628b8b9e482dd876ae9b89d558fdcc7cf7d0da1",
"Gateway": "172.18.0.1",
"IPAddress": "172.18.0.5",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:12:00:05",
"DriverOpts": null
}
}
}
the IP is not in
.NetworkSettings.IPAddress
but in :
.NetworkSettings.Networks.${network_name}.IPAddress
as the ${network_name}
is not fixed, I have to modify the shell script in ~/.bashrc
to parse the network name, the code in ~/.bashrc
is as follow:
function docker_ip(){
IP=".NetworkSettings.IPAddress" #initialize IP by the default path
while getopts "n:c:" OPT # n represents custom network name and c represents container ID
do
case "$OPT" in
"n") IP=".NetworkSettings.Networks.${OPTARG}.IPAddress" ;;
#if n is followed by an argument, it indicates that the network is customized, and thus the default path is replaced.
"c") containerID=${OPTARG};;
"?") echo "unrecognized option";;
esac
done
echo"${IP}"
echo"${containerID}"
sudo docker inspect --format "{{${IP}}}" ${containerID} #to get the container's IP
}
The main idea of this script is that if the function is called with arguments such as :
$docker_ip -n ${network name} -c containerID
then the script will use .NetworkSettings.Networks.${network_name}.IPAddress
to get the IP address. If without argument, such as :
docker_ip -p containerID
which represents that the container is connected by default network, then the script will use .NetworkSettings.IPAddress
to get the IP address.
While this does not work properly. When I type $docker_ip -n mynet -c 0819
in the shell prompt the output is supposed to be :
$docker_ip -n mynet -c 0819
.NetworkSettings.Networks.mynet.IPAddress
0819
172.18.0.5
while in fact the output is as follow:
$docker_ip -n mynet -c 0819
.NetworkSettings.IPAddress
0819
As you can see, since the path should be .NetworkSettings.Networks.mynet.IPAddress
rather than .NetworkSettings.IPAddress
so the IP address of container 0819 cannot be found.
Why this variable cannot change? I am a novice to shell script so I wanted to know if it's right to do so and what would be the best way to do that? Additionally, is there simpler way to get the IP address? Thanks in advance :)