简体   繁体   English

引用 ip 时的 host.docker.internal 与容器名称

[英]host.docker.internal vs container name when referencing ip

When defining services in a docker-compose file, when do I use host.docker.internal for the host's ip and when do I need to use the container's name?在 docker-compose 文件中定义服务时,我什么时候使用host.docker.internal作为主机的 ip 以及何时需要使用容器的名称?

All possible communication flows are illustrated here:此处说明了所有可能的通信流程:

在此处输入图像描述

(1): non-containerized process communicates with a container (1):非容器化进程与容器通信

(2): container communicates with another container (2):容器与另一个容器通信

(3): the opposite direction of (1) (3):与(1)相反的方向


(1): container must forward port to host, so the non-containerized process can access it (1):容器必须将端口转发给主机,这样非容器化的进程才能访问它

services:
   c1:
    ...
     ports:
       - hostport:containerport

(2): container c1 just use the service name (container name - c2) to communicate with c2 (2):容器c1只是使用服务名(容器名-c2)与c2通信

(3): container c2 must Use host.docker.internal to communicate with host (3):容器c2必须使用host.docker.internal与主机通信

If you want to connect from a container to a service on the host, use host.docker.internal .如果要从容器连接到主机上的服务,请使用host.docker.internal Be aware, that this special DNS name is only available on Windows and macOS.请注意,这个特殊的 DNS 名称仅在 Windows 和 macOS 上可用。

Use the container name for networking between containers.使用容器名称在容器之间建立网络。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当容器需要连接宿主机时,为什么 localhost 必须替换为 host.docker.internal? - Why does localhost have to replaced with host.docker.internal when a container needs to connect to the host machine? ์Nginx Linux 容器无法解析主机。docker.internal - ์Nginx Linux container could not resolve host.docker.internal host.docker.internal 主机上正在运行什么? - What is running on host.docker.internal host? 什么是 linux 相当于“host.docker.internal” - What is linux equivalent of “host.docker.internal” 如何将域命名为“host.docker.internal”? - How to CNAME a domain to “host.docker.internal”? 当使用 nerdctl 代替 docker 时,应该使用什么来代替“host.docker.internal”? - what should be used instead of 'host.docker.internal' when using nerdctl in place of docker? Docker - host.docker.internal" (192.168.65.2) 拒绝连接 - Docker - host.docker.internal" (192.168.65.2) refused to connect 等效于 Rancher Desktop 中 Docker 桌面的 'host.docker.internal' - Equivalent to Docker Desktop's 'host.docker.internal' in Rancher Desktop java.net.UnknownHostException:host.docker.internal:AWS EC2 上的名称或服务未知 - java.net.UnknownHostException: host.docker.internal: Name or service not known on AWS EC2 Docker - 如何通过 SSL 连接到 host.docker.internal? - Docker - How do you connect to host.docker.internal on SSL?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM