我正在尝试使用docker-compose
将dotnet核心项目和mongodb作为容器服务运行。两种服务都从头开始,没有错误。当我调用与mongo交互的端点时,出现超时错误。由于我使用的是docker-compose
,所以我希望可以通过连接字符串中的组合服务名称来引用mongo
服务。
mongo:27017/api?authSource=api
,具有用户名api
和密码password123
,如下面的docker-compose文件所示。相反,我收到此错误:
System.TimeoutException : A timeout occured after 30000ms selecting a server using CompositeServerSelector{ Selectors = MongoDB.Driver.MongoClient+AreSessionsSupportedServerSelector, LatencyLimitingServerSelector{ AllowedLatencyRange = 00:00:00.0150000 } }. Client view of cluster state is { ClusterId : "1", ConnectionMode : "Automatic", Type : "Unknown", State : "Disconnected", Servers : [{ ServerId: "{ ClusterId : 1, EndPoint : "Unspecified/mongo:27017" }", EndPoint: "Unspecified/mongo:27017", State: "Disconnected", Type: "Unknown", HeartbeatException: "MongoDB.Driver.MongoConnectionException: An exception occurred while opening a connection to the server.
---> System.Net.Internals.SocketExceptionFactory+ExtendedSocketException (00000005, 0xFFFDFFFF): Name or service not known
at System.Net.Dns.InternalGetHostByName(String hostName)
at System.Net.Dns.ResolveCallback(Object context)
--- End of stack trace from previous location where exception was thrown ---
at System.Net.Dns.HostResolutionEndHelper(IAsyncResult asyncResult)
at System.Net.Dns.EndGetHostAddresses(IAsyncResult asyncResult)
at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)
--- End of stack trace from previous location where exception was thrown ---
at MongoDB.Driver.Core.Connections.TcpStreamFactory.ResolveEndPointsAsync(EndPoint initial)
at MongoDB.Driver.Core.Connections.TcpStreamFactory.CreateStreamAsync(EndPoint endPoint, CancellationToken cancellationToken)
at MongoDB.Driver.Core.Connections.BinaryConnection.OpenHelperAsync(CancellationToken cancellationToken)
--- End of inner exception stack trace ---
at MongoDB.Driver.Core.Connections.BinaryConnection.OpenHelperAsync(CancellationToken cancellationToken)
at MongoDB.Driver.Core.Servers.ServerMonitor.HeartbeatAsync(CancellationToken cancellationToken)", LastUpdateTimestamp: "2020-09-03T21:28:59.1614966Z" }] }.
Stack Trace:
at MongoDB.Driver.Core.Clusters.Cluster.ThrowTimeoutException(IServerSelector selector, ClusterDescription description)
at MongoDB.Driver.Core.Clusters.Cluster.WaitForDescriptionChangedHelper.HandleCompletedTask(Task completedTask)
at MongoDB.Driver.Core.Clusters.Cluster.WaitForDescriptionChangedAsync(IServerSelector selector, ClusterDescription description, Task descriptionChangedTask, TimeSpan timeout, CancellationToken cancellationToken)
at MongoDB.Driver.Core.Clusters.Cluster.SelectServerAsync(IServerSelector selector, CancellationToken cancellationToken)
at MongoDB.Driver.MongoClient.AreSessionsSupportedAfterSeverSelctionAsync(CancellationToken cancellationToken)
at MongoDB.Driver.MongoClient.AreSessionsSupportedAsync(CancellationToken cancellationToken)
at MongoDB.Driver.MongoClient.StartImplicitSessionAsync(CancellationToken cancellationToken)
at MongoDB.Driver.MongoCollectionImpl`1.UsingImplicitSessionAsync[TResult](Func`2 funcAsync, CancellationToken cancellationToken)
at MongoDB.Driver.MongoCollectionBase`1.DeleteOneAsync(FilterDefinition`1 filter, DeleteOptions options, Func`2 bulkWriteAsync)
at Tests.AssetRespositoryTest.DeleteAsset(String assetId) in /app/Tests/Repository/AssetRepositoryTests.cs:line 140
at Tests.AssetRespositoryTest.TestWithTransaction() in /app/Tests/Repository/AssetRepositoryTests.cs:line 75
at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__139_0(Object state)
我已经确认我的连接字符串的用户名/密码设置为以下撰写文件中的密码。如果我将exec
放入应用容器,则可以按服务名称对mongo
容器执行ping操作,但是我无法使用mongo shell与root
或api
用户进行连接相反,我从mongo shell中得到了这个错误:
docker-compose exec app bash
mongo --host mongo --port 27017 -u api -p password123 --authenticationDatabase api
2020-09-03T20:28:37.209+0000 E QUERY [js] Error: couldn't connect to server mongo:27017, connection attempt failed: SocketException: Error connecting to mongo:27017 (23.217.138.110:27017) :: caused by :: Connection refused :
connect@src/mongo/shell/mongo.js:344:17
有趣的是,当从主机终端运行相同的mongo shell connect命令时,我可以进行连接,因此这似乎是容器问题。
docker-compose.yml
version: '2'
networks:
# This special network is configured so that the local metadata
# service can bind to the specific IP address that ECS uses
# in production
credentials_network:
driver: bridge
ipam:
config:
- subnet: "169.254.170.0/24"
gateway: 169.254.170.1
services:
# This container vends credentials to your containers
ecs-local-endpoints:
# The Amazon ECS Local Container Endpoints Docker Image
image: amazon/amazon-ecs-local-container-endpoints
volumes:
# Mount /var/run so we can access docker.sock and talk to Docker
- /var/run:/var/run
# Mount the shared configuration directory, used by the AWS CLI and AWS SDKs
# On Windows, this directory can be found at "%UserProfile%\.aws"
- ${USERPROFILE}\\.aws:/home/.aws/
environment:
# define the home folder; credentials will be read from $HOME/.aws
HOME: "/home"
# You can change which AWS CLI Profile is used
AWS_PROFILE: "default"
networks:
credentials_network:
# This special IP address is recognized by the AWS SDKs and AWS CLI
ipv4_address: "169.254.170.2"
app:
depends_on:
- ecs-local-endpoints
- mongo
networks:
credentials_network:
ipv4_address: "169.254.170.3"
build:
context: .
dockerfile: 'Dockerfile.compose'
environment:
ASPNETCORE_ENVIRONMENT: "local"
AWS_DEFAULT_REGION: "us-east-1"
AWS_CONTAINER_CREDENTIALS_RELATIVE_URI: "/creds"
volumes:
- './:/app'
links:
- mongo:mongo
ports:
- 9999:9999
mongo:
image: 'bitnami/mongodb:4.2'
restart: 'always'
environment:
- MONGODB_ROOT_PASSWORD=iamroot
- MONGODB_USERNAME=api
- MONGODB_PASSWORD=password123
- MONGODB_DATABASE=api
ports:
- 27017:27017
mongo-express:
image: mongo-express
restart: always
ports:
- 8081:8081
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: iamroot
depends_on:
- mongo
- app
Dockerfile
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-bionic AS build
WORKDIR /vsdbg
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
unzip \
&& rm -rf /var/lib/apt/lists/* \
&& curl -sSL https://aka.ms/getvsdbgsh \
| bash /dev/stdin -v latest -l /vsdbg
# Not copying anything since it's being mounted and managed by docker-compose volumes
WORKDIR /app
ENV DOTNET_USE_POLLING_FILE_WATCHER 1
EXPOSE 9999
RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4 \
&& echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.0 multiverse" | tee /etc/apt/sources.list.d/mongodb-org-4.0.list \
&& apt-get update \
&& apt-get install -y iputils-ping mongodb-org-shell
ENTRYPOINT dotnet watch --project /app/API/src/Foo.Api/Foo.Api.csproj run --urls=http://+:9999
我将一个xUnit测试项目添加到了exec
并在app
中运行,但同时也出现了以上所示的超时堆栈跟踪错误。
答案 0 :(得分:0)
我需要将mongodb容器添加到网络中,并提供它的发现别名。
networks:
credentials_network:
# define an alias for service discovery
aliases:
- mongo
ipv4_address: "169.254.170.4"
答案 1 :(得分:-1)
您是否尝试过Yaml文件version: '3.7'
?
如果仍然无法执行,请尝试在未定义网络的情况下进行撰写。