本地主机在Docker容器中是什么意思? [英] What does localhost means inside a Docker container?

查看:967
本文介绍了本地主机在Docker容器中是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说,如果我在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.

在主机系统上的外壳程序中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或Docker Compose YAML文件中的名称以及容器内部的端口号来引用它们.即使目标具有已发布的具有docker run -p选项或Docker Compose ports:设置的端口,也应使用 second (容器内部)端口号.

...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中运行,并且在同一容器中有多个容器,则可以使用localhost在它们之间进行通信.在Pod之间,应该在每个Pod/部署之前设置服务,并使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆