我有一個docker-compose文件中定義了兩個服務,並且希望將一個服務名稱映射到一個hosts條目,以便我可以將數據發送到mongo數據庫服務中的端點,例如在我主機我有鏈接和主機名碼頭組成
127.0.0.1 dotcomtest.net
,如果我有一個服務叫做web
和一種名爲echo-chamber
,我怎麼設置我的主人在我的web容器(或碼頭撰寫文件)文件,這樣就可以發送請求到端點,即http://dotcomtest.net/query_string?
謝謝
我有一個docker-compose文件中定義了兩個服務,並且希望將一個服務名稱映射到一個hosts條目,以便我可以將數據發送到mongo數據庫服務中的端點,例如在我主機我有鏈接和主機名碼頭組成
127.0.0.1 dotcomtest.net
,如果我有一個服務叫做web
和一種名爲echo-chamber
,我怎麼設置我的主人在我的web容器(或碼頭撰寫文件)文件,這樣就可以發送請求到端點,即http://dotcomtest.net/query_string?
謝謝
這是可能的,當他們都在同一個網絡上運行。所以,最好是把他們都在一個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
多數民衆贊成在多數民衆贊成在這似乎是所有的工作:-)不完全100%,但如何工作,但無法在碼頭找到'網絡別名' – Richlewis
在'docker-compose'你得到一個默認網絡,相當於「碼頭網絡創建
你的意思是你想要包含請聯繫您的主機上的dotcomtest.net?你在使用哪種操作系統? –
是的(我認爲是這樣),雖然我認爲這兩個容器可以在自己的網絡上說話,所以我認爲這將是兩個容器之間的通信情況。我使用MacOS – Richlewis