Docker-让新容器与预先运行的容器通信 [英] Docker - Have new container communicate with pre-running container

查看:79
本文介绍了Docker-让新容器与预先运行的容器通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在一对Docker容器之间建立一些非常网络,到目前为止,我所看到的所有文档都比我想做的要复杂得多。 / p>

我的用例很简单:



容器1已经在运行,并且正在监听28016端口
容器2将在容器1之后之后启动,并需要在端口28016上连接到容器1。



我知道我可以通过Docker进行设置-易于编写,但是容器1的寿命很长,对于此用例,我不想将其关闭。容器2需要启动并通过端口28016自动连接到容器1。而且,两个容器都在同一台计算机上运行。我无法弄清楚该怎么做。



我在容器1的dockerfile中公开了28016,并使用-p 28016:28016运行它。要使Container 2连接到Container 1,我需要做什么?

解决方案

有几种解决方法。大多数不要求您发布端口。



使用用户定义的网络



如果您开始用户定义的网络中长期运行的容器,因为这样docker将处理

  docker网络创建服务网络
docker run --net = service-network --name Container1 service-image

如果您然后启动在同一网络中的临时容器,它将能够通过名称引用长期运行的容器。例如:

  docker run --name Container2 --net = service-network ephemeral-image 



使用现有的容器网络命名空间



您只需在其中运行临时容器长期运行的容器的网络名称空间:

  docker run --name Container2 --net = container:Container1 ephemeral-image 

在这种情况下,可以通过 localhost:28016



访问主机上的服务



由于您已在主机上发布服务主机 -p 28016:28016 ,则可以使用主机地址引用该访问,该地址将从容器内部成为默认网关。您可以通过以下方式获取该信息:

  address = $(ip route | awk'$ 1 == default {print $ 3 }')

您的服务将在 $ {address}上可用: 28016


I'm trying to setup some very simple networking between a pair of Docker containers and so far all the documentation I've seen is far more complex than for what I am trying to do.

My use case is simple:

Container 1 is already running and is listening on port 28016 Container 2 will start after container 1 and needs to connect to container 1 on port 28016.

I am aware I can set this up via Docker-Compose with ease, however Container 1 is long-lived and for this use case, I do not want to shut it down. Container 2 needs to start and automatically connect to container 1 via port 28016. Also, both containers are running on the same machine. I cannot figure out how to do this.

I've exposed 28016 in Container 1's dockerfile, and I'm running it with -p 28016:28016. What do I need to do for Container 2 to connect to Container 1?

解决方案

There are a few ways of solving this. Most don't require you to publish the ports.

Using a user defined network

If you start your long-running container in a user-defined network, because then docker will handle

docker network create service-network
docker run --net=service-network --name Container1 service-image

If you then start your ephemeral container in the same network, it will be able to refer to the long-running container by name. E.g:

docker run --name Container2 --net=service-network ephemeral-image

Using the existing container network namespace

You can just run the ephemeral container inside the network namespace of the long running container:

docker run --name Container2 --net=container:Container1 ephemeral-image

In this case, the service would be available via localhost:28016.

Accessing the service on the host

Since you've published the service on the host with -p 28016:28016, you can refer to that access using the address of the host, which from inside the container is going to be the default gateway. You can get that with something like:

address=$(ip route | awk '$1 == "default" {print $3}')

And your service would be available on ${address}:28016.

这篇关于Docker-让新容器与预先运行的容器通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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