skip to Main Content

I have an existing ASP.NET Core 6 application that is already running with a SQL Server database. I am now trying to containerize the application using Docker and having issues with the database connection. I would appreciate some guidance on how to set up the correct database connection.

I have an ASP.NET Core 6 application with the following connection string in appsettings.Development.json:

"BookStoreContext": "Server=.;Initial Catalog=BookStoreDb;Trusted_Connection=True;MultipleActiveResultSets=true"

After installing Docker and adding the docker-compose.yml file with the necessary configurations, including the SQL Server image, I updated the connection string to:

"BookStoreContext": "Server=database;Initial Catalog=BookStoreDb;User Id=admin;Password=abc@345;"

Error:

When I try to run the Dockerized application, I encounter the following error:

Unhandled exception. Microsoft.Data.SqlClient.SqlException (0x80131904): Login failed for user 'admin'.

docker-compose.yml:

version: '3.4'

services:
  book-store:
    image: ${DOCKER_REGISTRY-}bookstore
    build:
      context: .
      dockerfile: Book-Store/Dockerfile
    depends_on:
      - database

  database:
    image: mcr.microsoft.com/mssql/server:latest
    environment:
      ACCEPT_EULA: Y
      SA_PASSWORD: abc@345 # Replace with your SQL Server SA password
      MSSQL_PID: Express
    ports:
      - "1434:1437" # Map the container's port 1433 to port 1434 on the host machine

I would appreciate any assistance in identifying what I might be missing or doing incorrectly in my Docker setup and connection string configuration. I want to establish a successful connection to the SQL Server database within the Docker container for my ASP.NET Core 6 application, and additionally, how would I login through the already installed instances of SQL server to the instances created from the docker?

2

Answers


  1. Chosen as BEST ANSWER

    Removing the port, and using the User Id as sa in the connection string did solve the problem for me.

    This connection string worked:

    "BookStoreContext": "Server=database;Initial Catalog=BookStoreDb;User Id=sa;Password=Nirv@n@345;"
    

  2. As i saw in the documents of the image mcr.microsoft.com/mssql/server:latest, you should bind the port 1433. But you are binding the port 1437 in your docker compose file.

    https://hub.docker.com/_/microsoft-mssql-server

    However you don’t need to bind any port, because we you up your containers with docker compose, they are in the same network and see each other.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search