I have an application running in a Docker container (Linux) and a SQL Server instance running on my local machine (in Windows). Despite providing the correct credentials in the connection string, I am unable to establish a connection from the application to the SQL Server. Here are the relevant details of my setup:
docker-compose.yml:
version: '3.4'
services:
book-store:
image: ${DOCKER_REGISTRY-}bookstore
extra_hosts:
- "host.docker.internal:host-gateway"
build:
context: .
dockerfile: BookStore/Dockerfile
appsettings.json:
"BookStoreContext": "Server=host.docker.internal,1433;Initial Catalog=BookStoreDb;Persist Security Info=False;User ID=admin;Password=xxxxxxxx;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"
I have verified that the provided username and password are correct and allow me to log in to the SQL Server.
Despite these configurations, I’m still unable to establish a successful connection from my Docker container to the SQL Server.
Could you please help me identify what might be causing this issue and suggest any additional steps or changes I need to make to allow my application in the Docker container to connect to the SQL Server on my local machine?
2
Answers
After doing a couple of steps I was able to make this work finally.
Followed following steps:
After doing this there was the last error:
Which I solved by adding the "TrustServerCertificate=True" attribute to the connection string in the appsettings.json file.
In your docker compose file you should define the bridge network, to be in the same network with the localhost ip. Then they can see each other.