简体   繁体   English

当容器需要连接宿主机时,为什么 localhost 必须替换为 host.docker.internal?

[英]Why does localhost have to replaced with host.docker.internal when a container needs to connect to the host machine?

I am currently learning the fundamentals of docker.我目前正在学习 docker 的基础知识。 I have learnt that when a container needs to connect to the host machine (let's say I have a local mysql database), instead of using localhost as the domain, it has to use host.docker.internal as the domain.我了解到,当容器需要连接到主机时(假设我有一个本地 mysql 数据库),而不是使用localhost作为域,它必须使用host.docker.internal作为域。

So instead of something like this:所以不是这样的:

createConnection(
 host: "localhost",
 ...
)

I would do this:我会这样做:

createConnection(
 host: "host.docker.internal",
 ...
)

I think I understand why this is the case, but I just wanted to clarify why docker doesn't understand what localhost means, and why it has to be replaced with host.docker.internal .我想我明白为什么会这样,但我只是想澄清为什么 docker 不明白localhost的含义,以及为什么必须将其替换为host.docker.internal

Thanks in advance.提前致谢。

docker understands perfectly well what "localhost" means: When you create a container you are creating an isolated set of processes that can see only each other. docker 非常了解“本地主机”的含义:当您创建容器时,您正在创建一组相互隔离的进程,这些进程只能彼此看到。 They exist in their own cgroup for process isolation and network namespace for network isolation.它们存在于自己的 cgroup 中用于进程隔离和网络命名空间用于网络隔离。

So, when some service in some container listens on "localhost:8080", other processes in that container can connect to that port.因此,当某个容器中的某些服务侦听“localhost:8080”时,该容器中的其他进程可以连接到该端口。 But processes in other containers cannot.但是其他容器中的进程不能。 They might, necessarially, have their own process listening on localhost:8080.他们可能有自己的进程在 localhost:8080 上监听。

In this environment, where each container has its own ip address on a virtual network, and needs to be bridged to the hosts network, localhost will of course mean "the container", not "the containers host OS".在这种环境中,每个容器在虚拟网络上都有自己的 ip 地址,并且需要桥接到主机网络, localhost当然意味着“容器”,而不是“容器主机操作系统”。

Its not a case of docker not understanding.它不是 docker 不理解的情况。 This localhost redirection to the container is fundamental to the isolation that containerization brings.这种 localhost 重定向到容器是容器化带来的隔离的基础。

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

相关问题 MYSQL:连接到本地主机和主机。docker.internal - MYSQL:Connect to both localhost & host.docker.internal 引用 ip 时的 host.docker.internal 与容器名称 - host.docker.internal vs container name when referencing ip 使用 host.docker.internal 从 docker 连接到本地机器(主机)中的 SQL Server - Connect to SQL Server in local machine (host) from docker using host.docker.internal Docker-Compose:当 host.docker.internal 不起作用时连接到主机 - Docker-Compose: connect to host when host.docker.internal doesn't work Docker - host.docker.internal" (192.168.65.2) 拒绝连接 - Docker - host.docker.internal" (192.168.65.2) refused to connect Docker - 如何通过 SSL 连接到 host.docker.internal? - Docker - How do you connect to host.docker.internal on SSL? ์Nginx Linux 容器无法解析主机。docker.internal - ์Nginx Linux container could not resolve host.docker.internal host.docker.internal 主机上正在运行什么? - What is running on host.docker.internal host? 如何将 docker 容器与主机的本地主机 mysql 数据库连接? - How to connect docker container with host machine's localhost mysql database? 为什么我看到两个不同的 host.docker.internal 值以及为什么 cassandra 容器无法绑定到它 - Why am I seeing two different values of host.docker.internal and why the cassandra container can't bind to it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM