Reputation: 53
Im running docker-compose file which contains my WebApi and MongoDB I already created following docker-compose file but when im running it I am not able to perform any request.
This is my docker compose file:
version: '3.1'
services:
mongo:
image: mongo
restart: always
environment:
MONGODB_USER: "admin"
MONGODB_DATABASE: "BooksDB"
MONGODB_PASS: "pass"
ports:
- 27017:27017
mongo-express:
image: mongo-express
restart: always
ports:
- 8081:8081
depends_on:
- mongo
webapi:
build: .
restart: always
ports:
- 5000:80
environment:
MongoDB__Host: mongo
depends_on:
- mongo
im receiving following error in docker container log:
fail: Microsoft.AspNetCore.Server.Kestrel[13]
Connection id "0HLPSARCVM003", Request id "0HLPSARCVM003:00000001": An unhandled exception was thrown by the application.
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/localhost:27017" }", EndPoint: "Unspecified/localhost:27017", State: "Disconnected", Type: "Unknown", HeartbeatException: "MongoDB.Driver.MongoConnectionException: An exception occurred while opening a connection to the server. ---> System.Net.Sockets.SocketException: Cannot assign requested address
at System.Net.Sockets.Socket.BeginConnectEx(EndPoint remoteEP, Boolean flowContext, AsyncCallback callback, Object state)
at System.Net.Sockets.Socket.UnsafeBeginConnect(EndPoint remoteEP, AsyncCallback callback, Object state, Boolean flowContext)
at System.Net.Sockets.Socket.BeginConnect(EndPoint remoteEP, AsyncCallback callback, Object state)
at System.Net.Sockets.Socket.ConnectAsync(EndPoint remoteEP) at MongoDB.Driver.Core.Connections.TcpStreamFactory.ConnectAsync(Socket socket, EndPoint endPoint, CancellationToken cancellationToken) 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 ---
do you know what im supposed to change in those docker-compose file ?
Upvotes: 2
Views: 1271
Reputation: 78
The problem seems not to be with your docker-compose file, but with the code that you have written in the webapi service. The log that you have published shows an exception regarding the 'localhost:27017' endpoint, and the webapi should not look to localhost for the MongoDB. Instead try to use the environment variable that you set:
environment:
MongoDB__Host: mongo
This means that the webapi should look to mongo:27017
instead of localhost:27017
. But don't hardcode mongo:27017, but instead read from the environment. The reason is that each service you have defined gets it own IP and hostname on the bridge network. In fact there is no reason for you to expose the 27017 port in your docker-compose file, as the communication will flow directly on the bridge network between services.
Upvotes: 0
Reputation: 1339
The problem is that you're referring to localhost
in your webapi config. Please replace that with the docker hostname of t he mongoDB, in this case mongo:27017
Upvotes: 2