17. DevOps: How to identify the docker container ip?

Docker-logo

Please note, every docker container can have an ip once it is activated.

How to get ip of a container ?

We can check the activated container ips through below exercise:
Initially, you need to activate the container using run command.
Then the ip will be assigned from the docker default gateway bridge.

As below you need to do the lab session:

Step-1: Activate the container

vskumar@ubuntu:~$ sudo docker run -i -t ubuntu /bin/bash
root@2f71a66eabae:/# ps
PID TTY TIME CMD
1 pts/0 00:00:00 bash
9 pts/0 00:00:00 ps
root@2f71a66eabae:/# exit
exit
^[[Avskumar@ubuntu:~$ sudo docker run -i -t ubuntu ^C
vskumar@ubuntu:~$ sudo docker run ubuntu /bin/bash

Step-2: Let is check the docker containers:
vskumar@ubuntu:~$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
vskumar@ubuntu:~$ sudo docker ps -aq
74943dfce61c
2f71a66eabae
680a896d2c74
a65d0abcfea5

Step-3:Following shows the current docker networks:

vskumar@ubuntu:~$ sudo docker ps -aq
74943dfce61c
2f71a66eabae
680a896d2c74
a65d0abcfea5

vskumar@ubuntu:~$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
74943dfce61c ubuntu “/bin/bash” 34 minutes ago Exited (0) 34 minutes ago pedantic_haibt
2f71a66eabae ubuntu “/bin/bash” 34 minutes ago Exited (0) 34 minutes ago suspicious_kepler
680a896d2c74 ubuntu “/bin/bash” 8 hours ago Exited (0) 8 hours ago tender_ramanujan
a65d0abcfea5 ubuntu:16.04 “/bin/bash” 8 hours ago Exited (0) 8 hours ago competent_albattani

Step4: Making a container active:

The IP is assigned to a container with the below activation.
I named the container as container1.

vskumar@ubuntu:~$ sudo docker run -itd –name=container1 ubuntu:16.04
bfb319cdbfe366b369cb089731f614795677ab3ea4f614066596e9cccf17f57f

Step5: Now check the bridge status and the assigned ips of a default bridge to container1:

vskumar@ubuntu:~$ sudo docker network inspect bridge
[
{
“Name”: “bridge”,
“Id”: “c085bc6ae3691b9d8a43e9fc2a26bddc5809e51a4f3c16338143d4bae2d28151”,
“Created”: “2018-03-08T08:28:53.012490299-08:00”,
“Scope”: “local”,
“Driver”: “bridge”,
“EnableIPv6”: false,
“IPAM”: {
“Driver”: “default”,
“Options”: null,
“Config”: [
{
“Subnet”: “172.17.0.0/16”,
“Gateway”: “172.17.0.1”
}
]
},
“Internal”: false,
“Attachable”: false,
“Ingress”: false,
“ConfigFrom”: {
“Network”: “”
},
“ConfigOnly”: false,
“Containers”: {
“bfb319cdbfe366b369cb089731f614795677ab3ea4f614066596e9cccf17f57f”: {
“Name”: “container1”,
“EndpointID”: “9d5abe4df583946342ab36da0fc76a1d3d4c7a1fdaf2766d18b6dea7cd912eb7”,
“MacAddress”: “02:42:ac:11:00:02”,
“IPv4Address”: “172.17.0.2/16”,
“IPv6Address”: “”
}
},
“Options”: {
“com.docker.network.bridge.default_bridge”: “true”,
“com.docker.network.bridge.enable_icc”: “true”,
“com.docker.network.bridge.enable_ip_masquerade”: “true”,
“com.docker.network.bridge.host_binding_ipv4”: “0.0.0.0”,
“com.docker.network.bridge.name”: “docker0”,
“com.docker.network.driver.mtu”: “1500”
},
“Labels”: {}
}
]

vskumar@ubuntu:~$

Step6: Let us use another conatiner [testcontainer2] to active and get the ip:

sudo docker run -itd –name=testcontainer2 ubuntu:16.04

Step7: You can see the current container with the given names also:

vskumar@ubuntu:~$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d9ad288448ca ubuntu “/bin/bash” 3 minutes ago Up 3 minutes testcontainer1
bfb319cdbfe3 ubuntu:16.04 “/bin/bash” 9 minutes ago Exited (137) 5 minutes ago container1
74943dfce61c ubuntu “/bin/bash” 45 minutes ago Exited (0) 45 minutes ago pedantic_haibt
2f71a66eabae ubuntu “/bin/bash” About an hour ago Exited (0) 45 minutes ago suspicious_kepler
680a896d2c74 ubuntu “/bin/bash” 8 hours ago Exited (0) 8 hours ago tender_ramanujan
a65d0abcfea5 ubuntu:16.04 “/bin/bash” 8 hours ago Exited (0) 8 hours ago competent_albattani
vskumar@ubuntu:~$ clear

vskumar@ubuntu:~$

Step8: Let us check the its ip for the activated container as below:

vskumar@ubuntu:~$ sudo docker inspect -f “{{ .NetworkSettings.IPAddress }}” d9ad288448ca
172.17.0.2

You can see the ip:172.17.0.2

vskumar@ubuntu:~$ sudo docker ps -a^C
vskumar@ubuntu:~$ sudo docker inspect -f “{{ .NetworkSettings.IPAddress }}” d9ad288448ca^C
vskumar@ubuntu:~$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d9ad288448ca ubuntu “/bin/bash” 5 minutes ago Up 5 minutes testcontainer1
bfb319cdbfe3 ubuntu:16.04 “/bin/bash” 11 minutes ago Exited (137) 7 minutes ago container1
74943dfce61c ubuntu “/bin/bash” About an hour ago Exited (0) About an hour ago pedantic_haibt
2f71a66eabae ubuntu “/bin/bash” About an hour ago Exited (0) About an hour ago suspicious_kepler
680a896d2c74 ubuntu “/bin/bash” 8 hours ago Exited (0) 8 hours ago tender_ramanujan
a65d0abcfea5 ubuntu:16.04 “/bin/bash” 8 hours ago Exited (0) 8 hours ago competent_albattani

Step9: Now let us use a third container and verify the ip:

vskumar@ubuntu:~$ sudo docker run -itd –name=testcontainer2 ubuntu:16.04
ee5b7978894bc844ae97d7ea893f1c76b99049a4bb71bedfa01d6e9c55e57867

vskumar@ubuntu:~$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ee5b7978894b ubuntu:16.04 “/bin/bash” 15 seconds ago Up 13 seconds testcontainer2
d9ad288448ca ubuntu “/bin/bash” 9 minutes ago Up 9 minutes testcontainer1
bfb319cdbfe3 ubuntu:16.04 “/bin/bash” 15 minutes ago Exited (137) 10 minutes ago container1
74943dfce61c ubuntu “/bin/bash” About an hour ago Exited (0) About an hour ago pedantic_haibt
2f71a66eabae ubuntu “/bin/bash” About an hour ago Exited (0) About an hour ago suspicious_kepler
680a896d2c74 ubuntu “/bin/bash” 8 hours ago Exited (0) 8 hours ago tender_ramanujan
a65d0abcfea5 ubuntu:16.04 “/bin/bash” 8 hours ago Exited (0) 8 hours ago competent_albattani

vskumar@ubuntu:~$ sudo docker inspect -f “{{ .NetworkSettings.IPAddress }}” ee5b7978894b
172.17.0.3
vskumar@ubuntu:~$

Step10: Now you can run the inspect command to check the ip for the latest activated container.

This way you will have the ips for the running container.
Please note as long as you keep running these conatiners these ips are valid.
Now if you want to use them for any micro services setup you can do after this procedure.
The docker network will have them shown through the VM browser also.

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s