❔ ASP.NET Core (in container) is trying to connect to another container via localhost

The connection string specifies mongo as the hostname, but the API still tries to connect to [::1] which is the loopback IPv6 address (localhost) I have a Docker Compose file with containers - backend: ASP.NET Core web API - mongo: MongoDB which looks like this:
version: '3.4'

services:
backend:
image: home-api
ports:
- "2154:2154"
build:
context: ./api
dockerfile: ./api/Dockerfile

mongo:
image: mongo
restart: always
ports:
- "2155:27017"
volumes:
- ./Data:/data/db
environment:
MONGO_INITDB_ROOT_USERNAME: home-api
MONGO_INITDB_ROOT_PASSWORD: home-api
version: '3.4'

services:
backend:
image: home-api
ports:
- "2154:2154"
build:
context: ./api
dockerfile: ./api/Dockerfile

mongo:
image: mongo
restart: always
ports:
- "2155:27017"
volumes:
- ./Data:/data/db
environment:
MONGO_INITDB_ROOT_USERNAME: home-api
MONGO_INITDB_ROOT_PASSWORD: home-api
With the API, I followed two tutorials: - https://learn.microsoft.com/en-us/aspnet/core/web-api/?WT.mc_id=dotnet-35129-website&view=aspnetcore-7.0 - https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-mongo-app?view=aspnetcore-7.0&tabs=visual-studio-code I then containerized it and started mongod in a ./Data directory. When connecting to the database (on the mongo container) with this appsettings.json
{
"HomeDatabase": {
"ConnectionString": "mongodb://home-api:home-api@mongo:27017/Home.Users?authSource=admin",
"DatabaseName": "Home",
"UsersCollectionName": "Users"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
{
"HomeDatabase": {
"ConnectionString": "mongodb://home-api:home-api@mongo:27017/Home.Users?authSource=admin",
"DatabaseName": "Home",
"UsersCollectionName": "Users"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
I recieve an error from the backend container. (attached) New to MongoDB, Docker, and ASP.NET. Let me know if you want any additional information.
Create web APIs with ASP.NET Core
Learn the basics of creating a web API in ASP.NET Core.
Create a web API with ASP.NET Core and MongoDB
This tutorial demonstrates how to create an ASP.NET Core web API using a MongoDB NoSQL database.
19 Replies
Saber
Saber2y ago
don't use mongo, but the connection string looks weird to me. shouldn't it be like mongodb://mongo:27017
circles.png
circles.pngOP2y ago
according to the official mongo docs the connection string has a username and password https://mongodb.github.io/mongo-java-driver/3.11/javadoc/com/mongodb/ConnectionString.html changing the connection string to mongodb://mongo:27017 still has the same error how do i know that when i docker compose up it updates the containers?
jcotton42
jcotton422y ago
pass --build it won't otherwise, iirc there's also --force-recreate
circles.png
circles.pngOP2y ago
oh thanks i got it so that it only has an authorization error where does mongodb store the database? (in the container so i can put it in a volum)
jcotton42
jcotton422y ago
read the docs for the container most of them are on hub.docker.com
jcotton42
jcotton422y ago
a search for "mongo" brings up https://hub.docker.com/_/mongo
mongo - Official Image | Docker Hub
MongoDB document databases provide high availability and easy scalability.
circles.png
circles.pngOP2y ago
now when trying to access mongo i have "authorization failed"
jcotton42
jcotton422y ago
anything in the logs for the mongo container? did you put the credentials back in the connection string?
circles.png
circles.pngOP2y ago
yea they are correct how can i see if the database in the container has the user?
jcotton42
jcotton422y ago
idk mongo, so can't help you there
circles.png
circles.pngOP2y ago
its ok couldnt find user
jcotton42
jcotton422y ago
have the MONGO_INITDB_ROOT_USERNAME and MONGO_INITDB_ROOT_PASSWORD environment keys always been in that compose file? if you added them after the first run, mongo might not read them, and thus not create the user you need
circles.png
circles.pngOP2y ago
they have always been
{"t":{"$date":"2023-01-21T02:35:59.127+00:00"},"s":"I", "c":"ACCESS", "id":20249, "ctx":"conn3","msg":"Authentication failed","attr":{"mechanism":"SCRAM-SHA-1","speculative":false,"principalName":"home-api","authenticationDatabase":"admin","remote":"192.168.16.3:58332","extraInfo":{},"error":"UserNotFound: Could not find user \"home-api\" for db \"admin\""}}
{"t":{"$date":"2023-01-21T02:35:59.127+00:00"},"s":"I", "c":"ACCESS", "id":20249, "ctx":"conn3","msg":"Authentication failed","attr":{"mechanism":"SCRAM-SHA-1","speculative":false,"principalName":"home-api","authenticationDatabase":"admin","remote":"192.168.16.3:58332","extraInfo":{},"error":"UserNotFound: Could not find user \"home-api\" for db \"admin\""}}
thats the line
jcotton42
jcotton422y ago
you could try docker compose down -v, that will remove the built containers and their volumes, to start from a clean slate when you do up again, pay close attention to the mongo logs
circles.png
circles.pngOP2y ago
got it
circles.png
circles.pngOP2y ago
entire log if you wanted it
jcotton42
jcotton422y ago
hmmmmmm lemme run the mongo container locally ok so if I do it locally via DataGrip I can connect oh wait @circles.png did you also clear out ./Data after you did docker compose down -v? b/c if not, it'll probably just be reusing the auth database
circles.png
circles.pngOP2y ago
wow that worked but my (test) data is gone i will mark as solved after i get it working
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server