简体   繁体   English

如何从 Windows 主机获取位于桥接网络内的 Docker 容器的 IP 地址?

[英]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.

相关问题 从主机访问Docker MySQL服务器,而不需要容器的IP地址 - Accessing a Docker MySQL server from the host machine, without needing the container's IP address 从docker mysql容器切换到主机的mysql,而无需使用network-mode =“ host” - Switch from docker mysql container to host's mysql without using network-mode=“host” 如何将 docker 容器上的 php 脚本与网络主机连接,同时在网络主机上使用 mysql Z05B6053C41A2130AFD68BDAB3E 容器 - How connect a php script on docker container with network host, with a mysql docker container also on network host 无法在 docker 的桥接网络中连接到 mysql - can not connect to mysql in docker's bridge network 从主机网络的sql-client连接到docker容器时出错 - Error in connecting to docker container from sql-client of Host network 从Web Api .Net核心容器连接到MySQL容器? 如何获得IP地址? - Connect to MySQL container from Web Api .Net Core Container? How to get Ip Address? Docker-compose Giving static IP 在网络模式:桥接 - Docker-compose Giving static IP in network mode : bridge 如何从docker容器连接主机'localhost' - How to connect host 'localhost' from docker container 如何将stdout从docker容器重定向到主机 - How to redirect stdout from docker container to host 如何通过 MySQL 转储文件从主机恢复数据到 Docker 容器。 Windows - How to restore Data from a MySQL dump file, from host to Docker container via. Windows
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM