❔ "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: ./apidockerfile: ./api/Dockerfilemongo:image: mongorestart: alwaysports:
  • "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": "*"
}

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.
error.txt6.35KB
Learn the basics of creating a web API in ASP.NET Core.
This tutorial demonstrates how to create an ASP.NET Core web API using a MongoDB NoSQL database.
Was this page helpful?