“Docker PS z IP” Kod odpowiedzi

Docker PS z 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
DreamCoder

Docker PS z IP

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

Docker PS z 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
DreamCoder

Docker PS z IP

#You can use docker inspect <container id>.

#For example:

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

Docker PS z 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}}' "$@"
}
DreamCoder

Docker PS z 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
DreamCoder

Docker PS z 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
DreamCoder

Docker PS z 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\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. – 
DreamCoder

Docker PS z IP

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

Docker PS z IP

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

Odpowiedzi podobne do “Docker PS z IP”

Pytania podobne do “Docker PS z IP”

Więcej pokrewnych odpowiedzi na “Docker PS z IP” w Shell/Bash

Przeglądaj popularne odpowiedzi na kod według języka

Przeglądaj inne języki kodu