dotnet core プロジェクトと mongodb を docker-compose を使用してコンテナー サービスとして実行しようとしています。どちらのサービスもエラーなく正常に起動します。 mongo と対話するエンドポイントを呼び出すと、タイムアウト エラーが発生します。 docker-compose を使用しているので、接続文字列内の compose サービス名によって mongo サービスを参照できると期待しています。
mongo:27017/api?authSource=api は、以下の docker-compose ファイルにあるように、ユーザー名 api とパスワード passwd123 を指定します。代わりに、次のエラーが発生します。
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)
接続文字列のユーザー/パスが以下の作成ファイルに含まれるものに設定されていることを確認しました。アプリコンテナーを実行すると、サービス名で mongo コンテナーに ping できますが、mongo シェルを使用して root または API ユーザーに接続することはできません。代わりに、次のメッセージが表示されます。mongo シェルからのエラー:
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 テスト プロジェクトを追加しましたが、上記と同じタイムアウト スタック トレース エラーが発生します。
mongodb コンテナをネットワークに追加し、その検出エイリアスを提供する必要がありました。
networks:
credentials_network:
# define an alias for service discovery
aliases:
- mongo
ipv4_address: "169.254.170.4"
yaml ファイルのバージョン「3.7」を試してみましたか? それでもできない場合は、ネットワークを定義せずに作成してみてください。
2
バージョン 2 から 3 への変更のうち、ここで役立つと思われる変更は何ですか?私が見たところ、主に swarm 相互運用性に焦点を当てていますが、何かが欠けている可能性があります。
– アレクサンダー2020 年 9 月 4 日 10:56
バージョン 3 は、次の github の問題とネットワーク構成の必要性のため、オプションではありません: github.com/docker/compose/issues/4366 github.com/docker/docker.github.io/pull/… github.com/awslabs/amazon-ecs-local-container-endpoints/issues/…
– アレクサンダー2020 年 9 月 4 日 11:07