How to Obtain an IP address Docker containers from the host?

This translation from: How to GET A Docker at The Container's IP address from Host?

Is there a command I can run to get the container's IP address right from the host after a new container is created? After creating a new container, if you can run the command to obtain the IP address of the vessel from the host?

Basically, once Docker creates the container, I want to roll my own code deployment and container configuration scripts. Basically, once Docker create a container, I would like to rolling your own code deployment and container configuration script.


#1st Floor

Reference: https://stackoom.com/question/19zVR/ how to get Docker containers from the host IP address


#2nd Floor

You can use docker inspect <container id> you can usedocker inspect <container id>

Example: Example:

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

#3rd floor

At The --formatthe Option of the Inspect Comes to at The Rescue. The Inspect the --formatoptions can be solved.

Modern Docker client syntax: Modern Docker client syntax:

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_name_or_id

Old Docker client syntax: old Docker client syntax:

docker inspect --format '{{ .NetworkSettings.IPAddress }}' container_name_or_id

These commands will return the Docker container's IP address. These commands return the IP address Docker container.

Mentioned in at The Comments AS: IF you are ON Windows , use Double QUOTES "INSTEAD of SINGLE QUOTES 'around at The Curly braces. As described in Notes: If you are using Windows , use double quotation marks around braces "instead of single quotes '.


#4th floor

In Docker 1.3+, you can also check it via steps below: in Docker 1.3+, you can also check through the following steps:

Enter the running Docker (Linux): Enter the running Docker (Linux):

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

For windows: For Windows:

docker exec [container-id or container-name] ipconfig

#5th Floor

The this Script in your shell the Add ~/.bashrcor Relevant File: Add this shell script to add ~/.bashrcor related documents:

docker-ip() {
  docker inspect --format '{{ .NetworkSettings.IPAddress }}' "$@"
}

Then, to get an IP address of a container, simply do this: Then, the container to obtain an IP address, just follow these steps:

docker-ip YOUR_CONTAINER_ID

For the new version of the Docker, please use the following: For the new version of the Docker, use the following command:

docker-ip() {
        docker inspect --format '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' "$@"
}

#6th floor

To extend the answers ko-dos, here is an alias used to list all the container name and IP address:

alias docker-ips='docker ps | tail -n +2 | while read -a a; do name=${a[$((${#a[@]}-1))]}; echo -ne "$name\t"; docker inspect $name | grep IPAddress | cut -d \" -f 4; done'
Original articles published 0 · won praise 136 · views 830 000 +

Guess you like

Origin blog.csdn.net/xfxf996/article/details/105249456