两个 docker 容器无法通信
Two docker containers cannot communicate
我有两个 docker 容器。一个容器是数据库,另一个是 Web 应用程序。
Web应用通过这个linkhttp://localhost:7200调用数据库。但是,Web 应用程序 docker 容器无法访问数据库容器。
我试过这个 docker-compose.yml,但不起作用:
version: '3'
services:
web:
# will build ./docker/web/Dockerfile
build:
context: .
dockerfile: ./docker/web/Dockerfile
links:
- graph-db
depends_on:
- graph-db
ports:
- "8080:8080"
environment:
- WAIT_HOSTS=graph-db:7200
networks:
- backend
graph-db:
# will build ./docker/graph-db/Dockerfile
build:
./docker/graph-db
hostname: graph-db
ports:
- "7200:7200"
networks:
backend:
driver: "bridge"
所以我有两个容器:
Web 应用程序:http://localhost:8080/reasoner 并且此容器调用位于不同容器中的 http://localhost:7200 中的数据库。
但是,Web 容器无法访问数据库容器。
解决方案
version: '3'
services:
web:
# will build ./docker/web/Dockerfile
build:
context: .
dockerfile: ./docker/web/Dockerfile
depends_on:
- graph-db
ports:
- "8080:8080"
environment:
- WAIT_HOSTS=graph-db:7200
graph-db:
# will build ./docker/graph-db/Dockerfile
build:
./docker/graph-db
ports:
- "7200:7200"
并将网络应用程序代码中的 http://localhost:7200 替换为 http://graph-db:7200
不要使用本地主机在容器之间进行通信。网络是 docker 中的命名空间之一,因此容器内的 localhost 仅连接到该容器,而不连接到您的外部主机,也不会连接到另一个容器。在这种情况下,在您的应用程序中使用服务名称 graph-db
而不是 localhost
来连接到数据库。
您的数据库主机是 graph-db
,您应该在应用程序的数据库配置中使用该名称。例如:http://graph-db:7200
来自 docker 网络文档(桥接网络 - Docker 中的默认网络驱动程序):
Imagine an application with a web front-end and a database back-end.
If you call your containers web and db, the web container can connect
to the db container at db, no matter which Docker host the application
stack is running on.
我有两个 docker 容器。一个容器是数据库,另一个是 Web 应用程序。 Web应用通过这个linkhttp://localhost:7200调用数据库。但是,Web 应用程序 docker 容器无法访问数据库容器。
我试过这个 docker-compose.yml,但不起作用:
version: '3'
services:
web:
# will build ./docker/web/Dockerfile
build:
context: .
dockerfile: ./docker/web/Dockerfile
links:
- graph-db
depends_on:
- graph-db
ports:
- "8080:8080"
environment:
- WAIT_HOSTS=graph-db:7200
networks:
- backend
graph-db:
# will build ./docker/graph-db/Dockerfile
build:
./docker/graph-db
hostname: graph-db
ports:
- "7200:7200"
networks:
backend:
driver: "bridge"
所以我有两个容器: Web 应用程序:http://localhost:8080/reasoner 并且此容器调用位于不同容器中的 http://localhost:7200 中的数据库。 但是,Web 容器无法访问数据库容器。
解决方案
version: '3'
services:
web:
# will build ./docker/web/Dockerfile
build:
context: .
dockerfile: ./docker/web/Dockerfile
depends_on:
- graph-db
ports:
- "8080:8080"
environment:
- WAIT_HOSTS=graph-db:7200
graph-db:
# will build ./docker/graph-db/Dockerfile
build:
./docker/graph-db
ports:
- "7200:7200"
并将网络应用程序代码中的 http://localhost:7200 替换为 http://graph-db:7200
不要使用本地主机在容器之间进行通信。网络是 docker 中的命名空间之一,因此容器内的 localhost 仅连接到该容器,而不连接到您的外部主机,也不会连接到另一个容器。在这种情况下,在您的应用程序中使用服务名称 graph-db
而不是 localhost
来连接到数据库。
您的数据库主机是 graph-db
,您应该在应用程序的数据库配置中使用该名称。例如:http://graph-db:7200
来自 docker 网络文档(桥接网络 - Docker 中的默认网络驱动程序):
Imagine an application with a web front-end and a database back-end. If you call your containers web and db, the web container can connect to the db container at db, no matter which Docker host the application stack is running on.