❔ 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:
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
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
don't use mongo, but the connection string looks weird to me. shouldn't it be like
mongodb://mongo:27017
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?pass
--build
it won't otherwise, iirc
there's also --force-recreate
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)
read the docs for the container
most of them are on hub.docker.com
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.
now when trying to access mongo i have "authorization failed"
anything in the logs for the mongo container?
did you put the credentials back in the connection string?
yea
they are correct
how can i see if the database in the container has the user?
idk mongo, so can't help you there
its ok
couldnt find user
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 needthey have always been
thats the line
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 logsgot it
entire log if you wanted it
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 databasewow that worked but my (test) data is gone
i will mark as solved after i get it working
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.