更新时间:2023-01-20 07:37:39
Docker CLI 文档 给出这个解决方案(假设您在 Linux 主机上运行):
The Docker CLI docs give this solution (which assumes you are running on a Linux host with ):
有时您需要从容器内连接到 Docker 主机.要启用此功能,请使用 --add-host
标志将 Docker 主机的 IP 地址传递给容器.要查找主机地址,请使用 ip addr show
命令.
Sometimes you need to connect to the Docker host from within your container. To enable this, pass the Docker host’s IP address to the container using the
--add-host
flag. To find the host’s address, use theip addr show
command.
传递给 ip addr show
的标志取决于您在容器中使用的是 IPv4 还是 IPv6 网络.使用以下标志为名为 eth0
的网络设备检索 IPv4 地址:
The flags you pass to ip addr show
depend on whether you are using IPv4 or IPv6 networking in your containers. Use the following flags for IPv4 address retrieval for a network device named eth0
:
$ HOSTIP=`ip -4 addr show scope global dev eth0 | grep inet | awk '{print $2}' | cut -d / -f 1`
$ docker run --add-host=docker:${HOSTIP} --rm -it debian
然后容器内的名称 docker
将映射到主机的 IP 地址.对于您的情况,您可以使用 docker run --add-host=mysql_server:$(hostip) ...
Then the name docker
inside the container will map to the host's IP address. For your case, you could use docker run --add-host=mysql_server:$(hostip) ...
如果使用 Boot2Docker,它会在预定义的地址设置到主机的映射,因此在该平台上,与上述等效的只是一个命令:
If using Boot2Docker, it sets up a mapping to the host at a predefined address, so on that platform the equivalent to the above is just the one command:
$ docker run --add-host=docker:192.168.59.3 --rm -it debian