xxxxxxxxxx
#Add this shell script in your ~/.bashrc or relevant file:
nano ~/.bashrc
docker-ip() {
docker inspect --format '{{ .NetworkSettings.IPAddress }}' "$@"
}
#Then, to get an IP address of a container, simply do this:
docker-ip YOUR_CONTAINER_ID
#For the new version of the Docker, please use the following:
docker-ip() {
docker inspect --format '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' "$@"
}
xxxxxxxxxx
docker inspect --format='{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -aq)
xxxxxxxxxx
docker inspect -f '{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -aq)
The output will be:
/containerA - 172.17.0.4
/containerB - 172.17.0.3
/containerC - 172.17.0.2
xxxxxxxxxx
#Based on some of the answers I loved, I decided to merge them to a function to get all the IP addresses and another for an specific container. They are now in my .bashrc file.
docker-ips() {
docker inspect --format='{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -aq)
}
docker-ip() {
docker inspect --format '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' "$@"
}
#The first command gives the IP address of all the containers and the second a specific container's IP address.
docker-ips
docker-ip YOUR_CONTAINER_ID
xxxxxxxxxx
docker exec [container-id or container-name] cat /etc/hosts
172.17.0.26 d8bc98fa4088
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.17 mysql
xxxxxxxxxx
docker inspect <CONTAINER ID> | grep -w "IPAddress" | awk '{ print $2 }' | head -n 1 | cut -d "," -f1
xxxxxxxxxx
# using sed
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}} %tab% {{.Name}}' $(docker ps -aq
) | sed 's#%tab%#\t#g' | sed 's#/##g' | sort -t . -k 1,1n -k 2,2n -k 3,3n -k 4,4n
#Also as a bash alias:
docker-ips() { docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}} %tab% {{.Name}}' $(docker ps -aq) | sed 's#%tab%#\t#g' | sed 's#/##g' | sort -t . -k 1,1n -k 2,2n -k 3,3n -k 4,4n }
#Output is sorted by IP address, and tab delimited:
# docker-ips
172.18.0.2 memcached
172.18.0.3 nginx
172.18.0.4 fpm-backup
172.18.0.5 dns
172.18.0.6 fpm-beta
172.18.0.7 exim
172.18.0.8 fpm-delta
172.18.0.9 mariadb
172.18.0.10 fpm-alpha
172.19.0.2 nextcloud-redis
172.19.0.3 nextcloud-db
172.19.0.4 nextcloud
xxxxxxxxxx
#You can use docker inspect <container id>.
#For example:
CID=$(docker run -d -p 4321 base nc -lk 4321);
docker inspect $CID
xxxxxxxxxx
# In order to extract the ip, you can do something like
docker inspect $CID | grep IPAddress | cut -d '"' -f 4, it works fine :) –
# Bringing it all together, this shell alias should list all container ids and their ips:
alias dockerip='docker ps | tail -n +2 | while read cid b; do echo -n "$cid\t"; docker inspect $cid | grep IPAddress | cut -d \" -f 4; done' –
# As mentionned by @user3119830, there is a new option to inspect. Now, you can get the Ip easier with
docker inspect -format '{{ .NetworkSettings.IPAddress }}' ${CID}
# Just a note. The single dash options are being deprecated so that -format will become --format. –
docker inspect -format '{{ .NetworkSettings.IPAddress }}' ${CID} is the new syntax. -format is deprecated, it becomes --format. –
xxxxxxxxxx
# The --format option of inspect comes to the rescue.
# Modern Docker client syntax is:
docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_name_or_id
#Old Docker client syntax is:
docker inspect --format '{{ .NetworkSettings.IPAddress }}' container_name_or_id