Search
 
SCRIPT & CODE EXAMPLE
 

SHELL

docker ps with ip

docker inspect --format='{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -aq)
Comment

docker ps with ip

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
Comment

docker ps with ip

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
Comment

docker ps with ip

#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}}' "$@"
}
Comment

docker ps with ip

docker inspect <CONTAINER ID> | grep -w "IPAddress" | awk '{ print $2 }' | head -n 1 | cut -d "," -f1
Comment

docker ps with ip

#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
Comment

docker ps with ip

# using sed

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}} %tab% {{.Name}}' $(docker ps -aq
) | sed 's#%tab%#	#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%#	#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
Comment

docker ps with ip

#You can use docker inspect <container id>.

#For example:

CID=$(docker run -d -p 4321 base nc -lk 4321);
docker inspect $CID
Comment

docker ps with ip

# 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	"; 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. – 
Comment

docker ps with ip

# 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
Comment

PREVIOUS NEXT
Code Example
Shell :: git file line history 
Shell :: get public ipv6 linux 
Shell :: shell read file line by line 
Shell :: github action run shell script 
Shell :: copy to clipboard in gihub readme 
Shell :: nginx: invalid option: "restart" 
Shell :: Update nvm installed node version and keep globally installed packages 
Shell :: install pip 
Shell :: apt check if package is installed 
Shell :: find text in files ubuntu 
Shell :: chmod: changing permissions of : Read-only file system and writeable 
Shell :: how to install gitkraken on fedora 
Shell :: get y.output file yacc remove shift reduce conflict 
Shell :: vuetify install 
Shell :: copy file from ssh to local 
Shell :: * branch master - FETCH_HEAD 
Shell :: pytorch anaconda install windows 
Shell :: your github oauth token for github.com contains invalid characters 
Shell :: git change commit author for all commits 
Shell :: install jq 
Shell :: remove .svn recursively linux 
Shell :: how to update ubuntu 
Shell :: count occurrences of word in unix bash 
Shell :: kill process cmd 
Shell :: Build-tool 32.0.0 rc1 is missing DX at dx.bat 
Shell :: BUILD FAILED (Ubuntu 20.04 using python-build 20180424) 
Shell :: choco install watchman 
Shell :: linux remove last line from file 
Shell :: bash move a list of files 
Shell :: ubuntu kill port 
ADD CONTENT
Topic
Content
Source link
Name
6+1 =