简体   繁体   中英

How to get a Docker container's IP address, located within a bridge network, from a windows host?

A bridge network has been created in docker using the following:

docker network create blognetwork

And a container has been created in that network with the following:

docker run --name=blogdb --network=blognetwork -p 3306:3306 -e --bind-address=0.0.0.0 -e=MYSQL_ROOT_PASSWORD=mypassword -detach mysql

How do I access the ip address of the new container, within the "blognetwork", from a windows host?

How do I get the ip address of the form XXXX, on its own? I know I can use the following to get a large json output with this data in it:

docker network inspect blognetwork

You can get directly the ip by using docker inspect and a proper format:

docker inspect containerId --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}'

More info in the Docker documentation.

You can get directly the IP address by using docker inspect. Docker inspect provides detailed information on constructs controlled by Docker.

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

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

If you are on Windows, use double quotes " instead of single quotes ' around the curly braces.

For more information Docker inspect documentation .

这是获取容器信息和容器 IP 的命令。

docker inspect 'ContainerID'

Since you ran

docker run -p 3306:3306 ...

you should access the service via port 3306 using your host's DNS name or IP address.

In the very specific case where you're at a shell prompt or browser URL entry on the machine where you ran docker run , assuming you're not using Docker Machine or Docker Toolbox, in this case and in this case only, you can use localhost:3306 to access the container. Never ever use localhost for anything else around Docker unless you're totally clear what it means. (If you ever start writing a question that uses the words "...the localhost of..." then localhost isn't what you want.)

If you are trying to reach this container from another container, since you ran

docker run --name=blogdb --network=blognetwork ...

you can use the name blogdb as a host name, and Docker provides an internal DNS service that can resolve it to something that will reach the container.

You never want the Docker-internal IP address, and should never run docker inspect to try to find it. The two biggest problems with it are that it's unreachable from off-host and that it changes when the container is deleted and recreated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM