2个Docker容器可以(或应该)通过localhost相互交互吗? [英] Can (or should) 2 docker containers interact with each other via localhost?

查看:811
本文介绍了2个Docker容器可以(或应该)通过localhost相互交互吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在对我们的微服务应用进行docker化,我遇到了一些发现问题.

We're dockerizing our micro services app, and I ran into some discovery issues.

该应用程序的配置如下:

The app is configured as follows:

当服务以非本地"模式启动时,它将使用Consul作为其发现注册表. 当服务以本地"模式启动时,它会自动为每个服务绑定一个地址(例如tcp://localhost:61001,tcp://localhost:61002等.硬编码地址)

When the a service is started in 'non-local' mode, it uses Consul as its Discovery registry. When a service is started in 'local' mode, it automatically binds an address per service (For example, tcp://localhost:61001, tcp://localhost:61002 and so on. Hard coded addresses)

在对应用程序进行docker化之后(目前仅适用于本地模式),每个服务都是一个容器(用docker-compose编排的Docker映像.如果有关系,则使用docker-machine编排) 但是一个服务不能与另一服务交互,因为它们不在同一台计算机上,并且tcp://localhost:61001显然将不起作用.

After dockerizing the app (for local mode only, for now) each service is a container (Docker images orchestrated with docker-compose. And with docker-machine, if that matters) But one service can not interact with another service since they are not on the same machine and tcp://localhost:61001 will obviously not work.

使用docker-compose和链接,并将localhost指定为别名(服务:本地主机)无效.有两种容器可以共享"同一本地主机的方法吗?

Using docker-compose with links and specifying localhost as an alias (service:localhost) didn't work. Is there a way for 2 containers to "share" the same localhost?

如果没有,解决此问题的最佳方法是什么? 我考虑过为每个服务使用特定的主机名,然后在docker-compose的links部分中指定主机名. (但我怀疑这是否是一种优雅的解决方案) 还是可以使用Consul的dockerized版本并与其集成?

If not, what is the best way to approach this? I thought about using specific hostname per service, and then specify the hostname in the links section of the docker-compose. (But I doubt that this is the elegant solution) Or maybe use a dockerized version of Consul and integrate with it?

这篇文章:如何在两个不同的Docker之间共享localhost容器?提供了一些有关为什么不应该混淆localhost的见解-但我仍然对这里的正确方法感到困惑.

This post: How to share localhost between two different Docker containers? provided some insights about why localhost shouldn't be messed with - but I'm still quite puzzled on what's the correct approach here.

谢谢!

推荐答案

但是一个服务不能与另一服务交互,因为它们不在同一台机器上,并且tcp://localhost:61001显然不起作用.

But one service can not interact with another service since they are not on the same machine and tcp://localhost:61001 will obviously not work.

实际上,他们可以.没错,tcp://localhost:61001将不起作用,因为在容器中使用localhost会引用容器本身,类似于默认情况下localhost在任何系统上的工作方式.这意味着您的服务不能共享同一主机.如果需要它们,则可以将一个容器用于这两种服务,尽管这实际上不是最佳设计,因为它违反了Docker Compose的主要目的之一.

Actually, they can. You are right that tcp://localhost:61001 will not work, because using localhost within a container would be referring to the container itself, similar to how localhost works on any system by default. This means that your services cannot share the same host. If you want them to, you can use one container for both services, although this really isn't the best design since it defeats one of the main purposes of Docker Compose.

理想的方法是使用docker-compose链接,您所参考的指南显示了如何定义它们,但是要真正使用它们,您需要在URL中使用链接容器的名称,就像链接的容器的名称在原始容器的/etc/hosts中定义了IP映射(不是实际上是这样,但您可以这样理解).如果要将其更改为与链接容器的名称不同的名称,则可以使用链接别名,在所引用的同一指南中对此进行了说明.

The ideal way to do it is with docker-compose links, the guide you referenced shows how to define them, but to actually use them you need to use the linked container's name in URLs as if the linked container's name had an IP mapping defined in the original container's /etc/hosts (not that it actually does, but just so you get the idea). If you want to change it to be something different from the name of the linked container, you can use a link alias, which are explained in the same guide you referenced.

例如,使用这样的docker-compose.yml文件:

For example, with a docker-compose.yml file like this:

a:
  expose:
    - "9999"
b:
  links:
    - a

a上监听0.0.0.0:9999的情况下,b可以通过从b内部向tcp://a:9999发出请求来与a进行交互.也可以将它封装到b中并运行

With a listening on 0.0.0.0:9999, b can interact with a by making requests from within b to tcp://a:9999. It would also be possible to shell into b and run

ping a

会将ping请求从b容器发送到a容器.

which would send ping requests to the a container from the b container.

因此,最后,请尝试使用链接容器的文字名称(或链接别名,如果链接是用别名定义的),将请求URL中的localhost替换.这意味着

So in conclusion, try replacing localhost in the request URL with the literal name of the linked container (or the link alias, if the link is defined with an alias). That means that

tcp://<container_name>:61001

应该代替

tcp://localhost:61001

只需确保在docker-compose.yml中定义了链接.

Just make sure you define the link in docker-compose.yml.

希望这会有所帮助

这篇关于2个Docker容器可以(或应该)通过localhost相互交互吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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