Search code examples
docker-containerdocker-cli

Connect local SQL Server database from the containerized Asp.net Core Application


I have a very simple "hello world" kind Asp.net Core API Application (swagger included) where I click one API Controller and it fetches the data from the database and returns as JSON. I have tested that working on my system through the debugger and it all good to go inside docker.

I have created a docker file (not docker compose) and build the image finally run the docker container and checked the application is indeed accessible. However, the API Controller which fetches the data from the database, that endpoint failed. I have tried all possible options like

  1. https://medium.com/@vedkoditkar/connect-to-local-ms-sql-server-from-docker-container-9d2b3d33e5e9
  2. How do I connect the local SQL Server database for the ASP.NET Core application running inside either local Docker or Kubernetes?

I have the latest docker version (20.10.2, build 2291f61) which supports host.docker.internal and that does not work either.

I have tried after disabling the firewall, have used the IP Address with port 1433, have used host.docker.internal and everywhere It gives same error to connect to the database. Well, I tried to same IP Address and connect locally through SQL Management Studio and confirmed that I am doing any mistake in typo for username, password or SQL server, and database.

I am having an asp.net core API application where the connection strings store under appsettings.json.

enter image description here

enter image description here None of the suggested approaches is working. Any different option/ clue?


Solution

  • Try to run the container in with host network

    docker run --network host myimagename myappcontainer

    This way myappcontainer will be able to access the mssql running on your machine.