Using local environment SQL SERVER in docker desktop container

Hello,
scenario,

  1. Base operating system is Windows 8.1
  2. Run Windows 11 using VMWare on base operation system (windows 8.1)
  3. Docker desktop is install on Windows 11 (Virutal machine)
  4. An ASP.NET Web API application with CURD opration with local evironment SQL SERVER database connection in appsetting.json

This configuration works with Visual studio 2019.
When try to create Docker container, local evironment SQL SERVER database connection is not etrablish and do not get result

Try to check from Container CLI, the local enviroment SQL SERVER

  • ping
  • netcat

Additional infromation,

when application try to connect local database from docker container, it shows error of

SslException: SSL Handshake failed with OpenSSL error - SSL_ERROR_SSL.

using docker mssql-tools, we are able to connect local environment database instance

run the ms sql tools docker container

docker run -it mcr.microsoft.com/mssql-tools

use command to connect to required database server

sqlcmd -S 192.168.XX.XXX,1433 -U sa -P XXXXX

use command to see whether database connection is estrablish and can use SQL

SELECT name FROM sys.databases

Does anyone has any solution of this type of issue?

Thanks in advance.

What is the role of the Windows 8.1 host in this configuration? Do you run VSCode there or is it in the virtual machine? I don’t understand where is your client and where is your target SQL server to connect to. Please, explain what you have done and where you have done it exactly. Share commands if it is possible.

I have updated my issue details. Can you please check and if any thing you can help.

Thanks for the additional information, but please, answer the questions what I asked. I need to know exactly where your client is and where your server is. I can guess, because you wrote about Docker installed inside the virtual machine, so the mssql tools as a client is probably inside the virtual machine. However, you haven’t answered where is VSCode and what is the role of the host operating system (Windows 8.1).

Let me tell you what I think and correct me if I am wrong.

  1. You have a Windows 8.1 host operating system just to run a virtual machine (at least in this context)
  2. Everything you do with Docker and MSSQL is inside your virtual machine.
  3. You have Visual Studio Code in the virtual machine.
  4. You use VSCode to develop your application with the help of it’s Docker support.
  5. You have an MSSQL server in the virtual machine but not in a container. It is running directly on the Windows 11 VM.
  6. You run the mssql-tools container in which you try to access the MSSQL server on Windows 11 and it works without error message.
  7. You have an application developed by you or just downloaded and configured by you, which also runs inside a container but a different container. This application cannot connect to the database server running on Windows 11. It gives you the SSL error.

If I understand the problem, it is caused by the SSL/TLS support incompatibility between the server and your container in which your application is running. In this case you should check what TLS version the server supports and do the same with the application container. I leave a github issue here that seems related to me: .NET Core and .NET 5 - focal Docker images Won't Connect to SQL Server · Issue #776 · dotnet/SqlClient · GitHub

I’ve been able to connect to SQL Server running on Docker, both Linux (Copy Table from Oracle to SQL Server on Linux) and Windows (Install SQL Server and Oracle on Windows Containers), see if there’s something you’re missing.

Thanks for the reply. please find my answers below.

  1. You have a Windows 8.1 host operating system just to run a virtual machine (at least in this context)
  • Yes. I have a Windows 8.1 host operating system
  1. Everything you do with Docker and MSSQL is inside your virtual machine.
  • No. My Visual studio 2019 & Docker are inside VM-Windows 11 & my database is in-network on Windows SERVER 2012 (operating system of the database)
  1. You have Visual Studio Code in the virtual machine.
  • Yes. I also have Visual studio code, but I am using Visual Studio 2019
  1. You use VSCode to develop your application with the help of it’s Docker support.
  • Yes
  1. You have an MSSQL server in the virtual machine but not in a container. It is running directly on the Windows 11 VM.
  • No. As I have mentioned that SQL SERVER is in network with different machine
  1. You run the mssql-tools container in which you try to access the MSSQL server on Windows 11 and it works without error message.
  • Yes, even when I create only mssql-tools container then also my network database is accessed via mssql-tools container.
  1. You have an application developed by you or just downloaded and configured by you, which also runs inside a container but a different container. This application cannot connect to the database server running on Windows 11. It gives you the SSL error.
  • Application is developed by me. I am using .NET Core API type project and trying to connect the
    database via Microsoft.Data.Sqlclient library.

find below is my scenario

1 Like

thanks for the reply. I am trying to find the exact issue.

Thanks four your detailed description. Yes, somehow every time I hear or read Visual Studio, I think of Visual Studio Code, because I used it more…

Now I understand your infrastructure better, but I don’t have more ideas, so I hope one of our links can help you.

Thanks for the help. We found that the issue is related to SSL/TLS. when I created container, openssl.cnf file has

[system_default_sect]
MinProtocol = TLSv1.2
CipherString = DEFAULT@SECLEVEL=2

this I have change to following

[system_default_sect]
MinProtocol = TLSv1.2
CipherString = DEFAULT@SECLEVEL=1

with this settings currently, I am able connect to the SERVER.

Now, I am facing new issue,

  1. I have .NET API container
  2. I have .NET MVC container

I created a network and try to access via URI .NET API container in .NET MVC container.
my docker compose file

version: '3.4'

networks:
  todo-network:
    name: todo-network

services:
  todo-api:
    image: ${DOCKER_REGISTRY-}todoapi
    build:
      context: .
      dockerfile: ToDo-API/Dockerfile
    expose:
        - 80
    ports:
      - "5100:443"
      - "5101:80"
    environment:
    - ConnectionStrings_DefaultConnection=SERVER=192.168.XX.XXX,51042;Database=XXX;User ID=XX;Password=XXXX;MultipleActiveResultSets=True;
    networks: 
      - todo-network
            
  todo-mvc-app:
    image: ${DOCKER_REGISTRY-}todomvcapp
    build:
      context: .
      dockerfile: ToDo-Mvc-App/Dockerfile
    ports:
      - "5200:443"
      - "5201:80"
    depends_on:
        - todo-api
    environment: 
      - ToDoApiUrl=http://localhost:5101
    networks: 
      - todo-network

but I am getting error “SocketException: Cannot assign requested address”, can you help me for this?

Please, use codeblocks instead of quotes for codes, especially for yaml files in which the indentation is important. I fixed your post.

About the SocketException. It looks like a service cannot listen on a port. But without a more detailed error log, I can’t tell you more. Where did you read this error message? What threw it?

Thanks for the reply. The error is related to port forwarding from the Docker daemon to the VM machine. I have checked the issue on google and found the solution of my issue related to the docker-compose file.

services:
  todo-api:
    image: ${DOCKER_REGISTRY-}todoapi
    environment:
      - ...
    build:
      context: .
      dockerfile: ToDo-API/Dockerfile
    ports:
      - 5101:80

  todo-mvc-app:
    image: ${DOCKER_REGISTRY-}todomvcapp
    environment:
      - ToDoApiUrl=...
    build:
      context: .
      dockerfile: ToDo-Mvc-App/Dockerfile

many thanks for helping related SSL/TLS.