skip to Main Content

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


  1. Chosen as BEST ANSWER

    After doing a couple of steps I was able to make this work finally.

    Followed following steps:

    • Open SQL Server Configuration Manager on your local machine.
    • Go to SQL Server Network Configuration > Protocols for MSSQLSERVER.
    • Enable TCP/IP protocol and make sure it's set to "Enabled".
    • After enabling TCP/IP, open its properties and go to the "IP Addresses" tab.
    • Under the IPAll section, set the TCP Port to 1433 (the default port).
    • Restart the SQL Server service to apply the changes.

    enter image description here

    After doing this there was the last error:

    Microsoft.Data.SqlClient.SqlException: 'A connection was successfully established with the server, but then an error occurred during the pre-login handshake. (provider: TCP Provider, error: 35 - An internal exception was caught)'

    AuthenticationException: The remote certificate was rejected by the provided RemoteCertificateValidationCallback.

    Which I solved by adding the "TrustServerCertificate=True" attribute to the connection string in the appsettings.json file.

    "BookStoreContext": "Server=172.26.192.1,1433;Initial Catalog=BookStoreDb;Persist Security Info=False;User ID=admin;Password=xxxxxx;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=True;Connection Timeout=30;"
    

  2. 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.

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