无法从容器内部连接到 docker 容器中的 mongodb 实例 运行
Unable to connect to the mongodb instance running in a docker container from inside the container
我构建了一个 docker 容器,其中包含一个 mongodb 数据库和一个 azure http 触发函数。以下 yaml 是 docker 撰写文件:
version: '3.4'
services:
mongo:
image: mongo
container_name: mongodb
restart: always
ports:
- 37017:27017
storage.emulator:
image: "mcr.microsoft.com/azure-storage/azurite:latest"
container_name: storage.emulator
ports:
- 20000:10000
- 20001:10001
- 20002:10002
my.functions:
image: ${DOCKER_REGISTRY-}myfunctions
build:
context: .
dockerfile: domain/My.Functions/Dockerfile
ports:
- 9080:80
depends_on:
- storage.emulator
- mongo
mongodb 实例运行良好,我可以通过 mongodb://localhost:37017
连接字符串从容器外部连接到它来播种一些数据。
容器内的 Azure 函数 运行 应该通过 mongodb://localhost:27017
连接字符串与 mongodb 实例通信,但根据以下错误消息它失败了:
"Unspecified/localhost:27017", ReasonChanged: "Heartbeat", State:
"Disconnected", ServerVersion: , TopologyVersion: , Type: "Unknown",
HeartbeatException: "MongoDB.Driver.MongoConnectionException: An
exception occurred while opening a connection to the server.
my.functions_1 | ---> System.Net.Sockets.SocketException (99):
Cannot assign requested address
我该如何解决这个问题?为什么 mongodb 在同一容器内的 azure 函数内部不可用?
因为 docker 容器在它们自己的隔离网络上运行。因此,当您尝试连接本地主机时,实际上您正在尝试 my.functions 应用程序,显然它在那部分没有服务 运行。
您应该使用docker-撰写服务名称
mongodb://mongo:27017
我构建了一个 docker 容器,其中包含一个 mongodb 数据库和一个 azure http 触发函数。以下 yaml 是 docker 撰写文件:
version: '3.4'
services:
mongo:
image: mongo
container_name: mongodb
restart: always
ports:
- 37017:27017
storage.emulator:
image: "mcr.microsoft.com/azure-storage/azurite:latest"
container_name: storage.emulator
ports:
- 20000:10000
- 20001:10001
- 20002:10002
my.functions:
image: ${DOCKER_REGISTRY-}myfunctions
build:
context: .
dockerfile: domain/My.Functions/Dockerfile
ports:
- 9080:80
depends_on:
- storage.emulator
- mongo
mongodb 实例运行良好,我可以通过 mongodb://localhost:37017
连接字符串从容器外部连接到它来播种一些数据。
容器内的 Azure 函数 运行 应该通过 mongodb://localhost:27017
连接字符串与 mongodb 实例通信,但根据以下错误消息它失败了:
"Unspecified/localhost:27017", ReasonChanged: "Heartbeat", State: "Disconnected", ServerVersion: , TopologyVersion: , Type: "Unknown", HeartbeatException: "MongoDB.Driver.MongoConnectionException: An exception occurred while opening a connection to the server. my.functions_1 | ---> System.Net.Sockets.SocketException (99): Cannot assign requested address
我该如何解决这个问题?为什么 mongodb 在同一容器内的 azure 函数内部不可用?
因为 docker 容器在它们自己的隔离网络上运行。因此,当您尝试连接本地主机时,实际上您正在尝试 my.functions 应用程序,显然它在那部分没有服务 运行。
您应该使用docker-撰写服务名称
mongodb://mongo:27017