本地主机在 Docker 容器中是什么意思? [英] What does localhost means inside a Docker container?
问题描述
比如说,如果我在 docker 容器中使用这个命令.
Say, if I use this command inside a docker container.
/opt/lampp/bin/mysql -h localhost -u root -pThePassword
这里的本地主机指的是什么?宿主机的IP还是docker容器自己的IP?
What would the localhost here refer to? The host machine's IP or the docker container's own IP?
推荐答案
从容器内部,localhost
总是指当前容器.它从不涉及另一个容器,也从不涉及在您的物理系统上运行但不在同一容器中的任何其他内容.与 localhost
建立出站连接或将 localhost
配置为数据库主机通常没有用.
From inside a container, localhost
always refers to the current container. It never refers to another container, and it never refers to anything else running on your physical system that's not in the same container. It's not usually useful to make outbound connections to localhost
or configure localhost
as your database host.
从主机系统上的 shell 中,localhost
可以指在 Docker 之外在您的系统上运行的守护进程,或者指您使用 docker run 发布的端口-p
选项.
From a shell on your host system, localhost
could refer to daemons running on your system outside Docker, or to ports you've published with docker run -p
options.
来自不同的系统,localhost
指的是调用它的系统.
From a different system, localhost
refers to the system it's called from.
就 IP 地址而言,localhost
始终为 127.0.0.1,并且该 IP 地址很特殊,并且始终为 localhost
并且行为和上面一样.
In terms of IP addresses, localhost
is always 127.0.0.1, and that IP address is special and is always localhost
and behaves the same way as above.
如果要连接到容器...
...来自另一个容器,最好的方法是确保它们在同一个 Docker 网络上(你从同一个 Docker Compose YAML 文件启动它们;你做了一个 docker network create
然后在同一网络上执行 docker run --net ...
并使用 Docker 的内部 DNS 服务通过容器的 --name<来引用它们/code> 或其在 Docker Compose YAML 文件中的名称和容器内的端口号.即使目标具有带有
docker run -p
选项或 Docker Compose ports:
设置的已发布端口,也请使用 second (container-internal) 端口号.
...from another container, the best way is to make sure they're on the same Docker network (you started them from the same Docker Compose YAML file; you did a docker network create
and then did docker run --net ...
on the same network) and use Docker's internal DNS service to refer to them by the container's --name
or its name in the Docker Compose YAML file and the port number inside the container. Even if the target has a published port with a docker run -p
option or Docker Compose ports:
setting, use the second (container-internal) port number.
...从外部 Docker 空间,确保使用 docker run -p
或 Docker Compose ports:
选项启动容器,然后使用该选项中的第一个端口号连接到主机的 IP 地址或 DNS 名称.
...from outside Docker space, make sure you started the container with a docker run -p
or Docker Compose ports:
option, and connect to the host's IP address or DNS name using the first port number from that option.
...来自同一物理主机上的终端窗口或浏览器,而不是在容器中,在这种情况下,仅在这种情况下,localhost
将始终如一地工作.
...from a terminal window or browser on the same physical host, not in a container, in this case and in this case only, localhost
will work consistently.
除外:
如果你用 --net host
启动了一个容器,localhost
指的是物理主机,你在同一物理主机上的终端窗口"场景.
If you started a container with --net host
, localhost
refers to the physical host, and you're in the "terminal window on the same physical host" scenario.
如果您想在同一个容器中拥有多个服务器,您可以使用 localhost
在它们之间进行通信.
If you've gone out of your way to have multiple servers in the same container, you can use localhost
to communicate between them.
如果您在 Kubernetes 中运行,并且在同一个 Pod 中有多个容器,则可以使用 localhost
在它们之间进行通信.在 Pod 之间,你应该在每个 Pod/deployment 前面设置一个服务,并使用 service-name.namespace-name.svc.cluster.local
形式的 DNS 名称.
If you're running in Kubernetes, and you have multiple containers in the same pod, you can use localhost
to communicate between them. Between pods, you should set up a service in front of each pod/deployment, and use DNS names of the form service-name.namespace-name.svc.cluster.local
.
这篇关于本地主机在 Docker 容器中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!