将Docker容器连接到本地主机的最简单方法 [英] Easiest way to connect Docker container to local host
问题描述
我想知道是否可以从Docker容器连接到本地主机上的应用程序。
I am wondering if is it possible to connect to an app on local host from Docker container.
我运行两个Docker容器,它们通过 link
选项。但是如何将其中一个容器连接到本地主机?
I run two Docker container which are connected to each other via link
option. But how can I connect one of the containers to the local host?
推荐答案
是的,请使用 docker run --network = container:< container-id>
-network ='container:':重用另一个容器的网络堆栈
--network='container:': reuse another container's network stack
这使您可以运行一个与另一个容器共享相同网络接口(然后为本地主机)的容器。
This let you run a container sharing the same network interface (then localhost) from another container.
或者,您可以使用 host
模式为您的容器提供相同的容器主机具有的网络ips(包括localhost)。 docker run --network = host
:
Alternatively, you can use the host
mode to give your containers the same network ips that the host has (including localhost). docker run --network=host
:
-network ='host' :使用Docker主机网络堆栈
--network= 'host': use the Docker host network stack
文档: https://docs.docker.com/engine/reference/run/#name-name
这篇关于将Docker容器连接到本地主机的最简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!