2017-08-10 111 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」 –