Docker is a high level framework for Linux containers which could be next level for virtualising applications and VM’s. Some times it is require to get ip address of a docker so that you can try to check a service running on it or try to ssh at 22 port on the IP address assigned to docker container.
This post will show you how to get ip address of a running container. To get IP address of a running docker container we should use “docker inspect” command and filter it out with filtering tools like awk etc. First we should know what is the name of docker you want to get IP address.
Surendra-MacBook-Pro in ~
○ → docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7f1f2fb7f0a4 linuxnix/puppetmaster:latest “/sbin/my_init” 3 hours ago Up About an hour 8140/tcp, 0.0.0.0:22->22/tcp puppet_master
From the above output the name of the docker running on the machine is “puppet_master”
Now use docker inspect with some filtering commands to get the IP
docker inspect <container_name>
Example:
Surendra-MacBook-Pro in ~
○ → docker inspect puppet_master | awk -F ‘”‘ ‘/IPAdd/ {print $4}’
172.17.0.8
If you are in the docker container we can use “ip a” to list interfaces
root@1937100e11d2:/# ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 13: eth0@if14: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0 inet 172.17.0.2/16 scope global eth0 valid_lft forever preferred_lft forever inet6 fe80::42:acff:fe11:2/64 scope link valid_lft forever preferred_lft forever root@1937100e11d2:/#
We can use docker inspect and filter for network setting using below command as well.
surendra@sanne-taggle:~/code/sh/test$ docker inspect --format '{{ .NetworkSettings.IPAddress }}' 1937100e11d2 172.17.0.2
Now we can use this IP address to whatever work you want.
Latest posts by Surendra Anne (see all)
- Docker: How to copy files to/from docker container - June 30, 2020
- Anisble: ERROR! unexpected parameter type in action:
Fix - June 29, 2020 - FREE: JOIN OUR DEVOPS TELEGRAM GROUPS - August 2, 2019
- Review: Whizlabs Practice Tests for AWS Certified Solutions Architect Professional (CSAP) - August 27, 2018
- How to use ohai/chef-shell to get node attributes - July 19, 2018