[英]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 中创建了桥接网络:
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?如何从 Windows 主机访问“blognetwork”中新容器的 IP 地址?
How do I get the ip address of the form XXXX, on its own?如何自行获取 XXXX 形式的 IP 地址? I know I can use the following to get a large json output with this data in it:
我知道我可以使用以下内容来获取包含此数据的大型 json 输出:
docker network inspect blognetwork
You can get directly the ip by using docker inspect and a proper format:您可以使用 docker inspect 和正确的格式直接获取 ip:
docker inspect containerId --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}'
More info in the Docker documentation. Docker 文档中的更多信息。
You can get directly the IP address by using docker inspect.您可以使用 docker inspect 直接获取 IP 地址。 Docker inspect provides detailed information on constructs controlled by Docker.
Docker inspect 提供有关由 Docker 控制的构造的详细信息。
Modern Docker client syntax is:现代 Docker 客户端语法是:
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_name_or_id
Old Docker client syntax is:旧的 Docker 客户端语法是:
docker inspect --format '{{ .NetworkSettings.IPAddress }}' container_name_or_id
These commands will return the Docker container's IP address.这些命令将返回 Docker 容器的 IP 地址。
If you are on Windows, use double quotes " instead of single quotes ' around the curly braces.如果您使用的是 Windows,请在花括号周围使用双引号 " 而不是单引号 '。
For more information Docker inspect documentation .有关更多信息Docker 检查文档。
这是获取容器信息和容器 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.您应该使用主机的 DNS 名称或 IP 地址通过端口 3306 访问该服务。
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.在非常特殊的情况下,您在运行
docker run
的机器上的 shell 提示符或浏览器 URL 条目中,假设您没有使用 Docker Machine 或 Docker Toolbox,在这种情况下(仅在这种情况下),您可以使用localhost:3306
访问容器。 Never ever use localhost
for anything else around Docker unless you're totally clear what it means.永远不要将
localhost
用于 Docker 周围的任何其他东西,除非您完全清楚它的含义。 (If you ever start writing a question that uses the words "...the localhost of..." then localhost isn't what you want.) (如果您开始写一个使用“...的本地主机...”字样的问题,那么 localhost 不是您想要的。)
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.您可以使用名称
blogdb
作为主机名,Docker 提供了一个内部 DNS 服务,可以将其解析为将到达容器的内容。
You never want the Docker-internal IP address, and should never run docker inspect
to try to find it.您永远不需要 Docker 内部 IP 地址,也不应该运行
docker inspect
来尝试找到它。 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.它的两个最大问题是它无法从主机外访问,并且当容器被删除和重新创建时它会发生变化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.