3 回答

TA貢獻(xiàn)1852條經(jīng)驗(yàn) 獲得超7個贊
您只需要確保要互相交談的容器在同一網(wǎng)絡(luò)上即可。網(wǎng)絡(luò)是一流的docker構(gòu)造,并不特定于組合。
# front/docker-compose.yml
version: '2'
services:
front:
...
networks:
- some-net
networks:
some-net:
driver: bridge
...
# api/docker-compose.yml
version: '2'
services:
api:
...
networks:
- front_some-net
networks:
front_some-net:
external: true
注意:根據(jù)“項(xiàng)目名稱”為您的應(yīng)用程序網(wǎng)絡(luò)指定名稱,該名稱基于其所在目錄的名稱,在這種情況下front_,添加了前綴
然后,他們可以使用服務(wù)名稱互相交談。從front您可以做到ping api,反之亦然。

TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超7個贊
只是@ johnharris85的一個好答案,當(dāng)您運(yùn)行docker compose文件時,default會創(chuàng)建一個“ ”網(wǎng)絡(luò),因此您可以將其作為外部網(wǎng)絡(luò)添加到另一個compose文件中:
# front/docker-compose.yml
version: '2'
services:
front_service:
...
...
# api/docker-compose.yml
version: '2'
services:
api_service:
...
networks:
- front_default
networks:
front_default:
external: true
對我來說,這種方法更合適,因?yàn)槲也粨碛械谝粋€docker-compose文件,而是想與之通信。

TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超8個贊
從撰寫文件版本3.5開始:
現(xiàn)在可以使用:
version: "3.5"
services:
proxy:
image: hello-world
ports:
- "80:80"
networks:
- proxynet
networks:
proxynet:
name: custom_network
docker-compose up -d將加入一個名為“ custom_network”的網(wǎng)絡(luò)。如果不存在,它將被創(chuàng)建!
root@ubuntu-s-1vcpu-1gb-tor1-01:~# docker-compose up -d
Creating network "custom_network" with the default driver
Creating root_proxy_1 ... done
現(xiàn)在,您可以執(zhí)行以下操作:
version: "2"
services:
web:
image: hello-world
networks:
- my-proxy-net
networks:
my-proxy-net:
external:
name: custom_network
這將創(chuàng)建一個將在外部網(wǎng)絡(luò)上的容器。
我在文檔中找不到任何參考,但是它有效!
- 3 回答
- 1 關(guān)注
- 3518 瀏覽
添加回答
舉報