2017-08-10 70 views
0

我有一个docker-compose文件中定义了两个服务,并且希望将一个服务名称映射到一个hosts条目,以便我可以将数据发送到mongo数据库服务中的端点,例如在我主机我有链接和主机名码头组成

127.0.0.1 dotcomtest.net 

,如果我有一个服务叫做web和一种名为echo-chamber,我怎么设置我的主人在我的web容器(或码头撰写文件)文件,这样就可以发送请求到端点,即http://dotcomtest.net/query_string?

谢谢

+0

你的意思是你想要包含请联系您的主机上的dotcomtest.net?你在使用哪种操作系统? –

+0

是的(我认为是这样),虽然我认为这两个容器可以在自己的网络上说话,所以我认为这将是两个容器之间的通信情况。我使用MacOS – Richlewis

回答

1

这是可能的,当他们都在同一个网络上运行。所以,最好是把他们都在一个docker-compose文件

version: '3' 
services: 
    nginx1: 
    image: nginx 
    ports: 
     - "8082:80" 
    nginx2: 
    image: nginx 
    ports: 
     - "8081:80" 
    networks: 
     default: 
     aliases: 
      - dotcomtest.net 

当我了这个成分,并转到nginx1容器使用

docker-compose exec nginx1 bash 

及以下执行命令

[email protected]:/# apt update && apt install -y curl 
[email protected]:/# curl http://dotcomtest.net 
<!DOCTYPE html> 
<html> 
<head> 
<title>Welcome to nginx!</title> 
<style> 
    body { 
     width: 35em; 
     margin: 0 auto; 
     font-family: Tahoma, Verdana, Arial, sans-serif; 
    } 
</style> 
</head> 
<body> 
<h1>Welcome to nginx!</h1> 
<p>If you see this page, the nginx web server is successfully installed and 
working. Further configuration is required.</p> 

<p>For online documentation and support please refer to 
<a href="http://nginx.org/">nginx.org</a>.<br/> 
Commercial support is available at 
<a href="http://nginx.com/">nginx.com</a>.</p> 

<p><em>Thank you for using nginx.</em></p> 
</body> 
</html> 

正如你可以看到我的nginx1容器有权访问nginx2

+0

多数民众赞成在多数民众赞成在这似乎是所有的工作:-)不完全100%,但如何工作,但无法在码头找到'网络别名' – Richlewis

+0

在'docker-compose'你得到一个默认网络,相当于“码头网络创建”,并且您的每个容器都会自动与此网络关联。现在您可以为这些网络指定别名。当两个容器在同一个网络中运行时,它们可以使用它们的进行寻址,在我们的例子中,服务名称为“nginx1”和“nginx2” –