Shell Script to find Container IP


I developed a shell script to find container IP running on the host machine. This script takes an argument from a user which is "all" either "container name" and based on that it provides IP of containers

Find repo here (find_IP_Address.sh)

In this tutorial, I am gonna explain my script to understand how it works!
as I mentioned my Script name above, run the command below to get help to find out what would be given as an argument. 
find_IP_Address.sh --help--Parameters -> <Name of the container> or <All>--Sample -> sh find_IP_Address <Parameters>
It is not the case I want to explain. I want to Explain how it finds IP for the given argument.

Let's dig the code!

Below code print all containers name and its IP. let see how it is extracting those output.
elif [ "$1" = "All" ]
then
	for container in $(docker ps --format '{{.Names}}')
	do
		for ipadd in $(docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}} {{end}}' $container)
		do
		echo "IP address of $container container :  $ipadd"
		done
	done
we know docker ps command print running containers and --format pretty-prints container output using a Go template (print its value without header)

$ docker ps

CONTAINER ID        IMAGE                        COMMAND                CREATED              STATUS              PORTS               NAMES
4c01db0b339c        ubuntu:12.04                 bash                   17 seconds ago       Up 16 seconds       3300-3310/tcp       webapp
d7886598dbe2        crosbymichael/redis:latest   /redis-server --dir    33 minutes ago       Up 33 minutes       6379/tcp            redis,webapp/db

$ docker ps --format '{{.Names}}'
 webapp
 redis,webapp/db
So the command, for container in $(docker ps --format {{.Names}}') command stores each container names

let see how we extracting each container IP.
As we know docker inspect <containerid> print Jason format output below
$ docker inspect 52249ba75f0f
[
    {
        "Id": "52249ba75f0fa33f93202f4a2d7f83bc71600b8b75ea4db0bc5b56022bf254b6",
        "Created": "2017-11-17T14:38:05.340313315Z",
        "Path": "/bin/bash",
        "Args": [],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 1535,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2017-11-17T14:38:05.638951265Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        }
        .......
To Extract containers IP again the --format command comes to rescue us!

for ipadd in $(docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}} {{end}}' $container)

# {{range.NetwokSetting.Networks}}{{.IPAddress}} extract container IP
# ipadd store ip of container
# Container name passed as container varible in for parent for loop
So the command for ipadd in $(docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}} {{end}}' $container)  command to stores each container IP address

Then, echo "IP address of $container container :  $ipadd" Used to print out Container name and its IP

Viola!

find the full code here

That's pretty much it for TODAY, PEACE

References

https://docs.docker.com/engine/reference/commandline/inspect/










, , , , ,

No comments:

Post a Comment