❔ "Cannot assign requested address [::1]:27017" when connecting to MongoDB on Docker.

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.
2 Replies
circles.png
circles.pngOP2y ago
i shouldnt ping roles should i
Accord
Accord2y ago
Looks like nothing has happened here. 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