Skip to content

Commit 5ceec82

Browse files
Introduce Docker debugging with Docker Compose orchestration support (#471)
1 parent 68eff11 commit 5ceec82

12 files changed

+440
-24
lines changed

.dockerignore

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
**/.classpath
2+
**/.dockerignore
3+
**/.env
4+
**/.git
5+
**/.gitignore
6+
**/.project
7+
**/.settings
8+
**/.toolstarget
9+
**/.vs
10+
**/.vscode
11+
**/*.*proj.user
12+
**/*.dbmdl
13+
**/*.jfm
14+
**/azds.yaml
15+
**/bin
16+
**/charts
17+
**/docker-compose*
18+
**/Dockerfile*
19+
**/node_modules
20+
**/npm-debug.log
21+
**/obj
22+
**/secrets.dev.yaml
23+
**/values.dev.yaml
24+
LICENSE
25+
README.md

BUILDGUIDE.md

+31
Original file line numberDiff line numberDiff line change
@@ -184,3 +184,34 @@ Managed SNI can be enabled on Windows by enabling the below AppContext switch:
184184
Scaled decimal parameter truncation can be enabled by enabling the below AppContext switch:
185185

186186
**"Switch.Microsoft.Data.SqlClient.TruncateScaledDecimal"**
187+
188+
## Debugging SqlClient on Linux from Windows
189+
190+
For enhanced developer experience, we support debugging SqlClient on Linux from Windows, using the project "**Microsoft.Data.SqlClient.DockerLinuxTest**" that requires "Container Tools" to be enabled in Visual Studio. You may import configuration: [VS19Components.vsconfig](./tools/vsconfig/VS19Components.vsconfig) if not enabled already.
191+
192+
This project is also included in `docker-compose.yml` to demonstrate connectivity with SQL Server docker image.
193+
194+
To run the same:
195+
1. Build the Solution in Visual Studio
196+
2. Set `docker-compose` as Startup Project
197+
3. Run "Docker-Compose" launch configuration.
198+
4. You will see similar message in Debug window:
199+
```log
200+
Connected to SQL Server v15.00.4023 from Unix 4.19.76.0
201+
The program 'dotnet' has exited with code 0 (0x0).
202+
```
203+
5. Now you can write code in [Program.cs](/src/Microsoft.Data.SqlClient/tests/DockerLinuxTest/Program.cs) to debug SqlClient on Linux!
204+
205+
### Troubleshooting Docker issues
206+
207+
There may be times where connection cannot be made to SQL Server, we found below ideas helpful:
208+
209+
- Clear Docker images to create clean image from time-to-time, and clear docker cache if needed by running `docker system prune` in Command Prompt.
210+
211+
- If you face `sni.dll not found` errors when debugging, try updating below properties in netcore\Microsoft.Data.SqlClient.csproj file and try again:
212+
```xml
213+
<OSGroup>Unix</OSGroup>
214+
<TargetsWindows>false</TargetsWindows>
215+
<TargetsUnix>true</TargetsUnix>
216+
```
217+
```

src/.dockerignore

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
**/.classpath
2+
**/.dockerignore
3+
**/.env
4+
**/.git
5+
**/.gitignore
6+
**/.project
7+
**/.settings
8+
**/.toolstarget
9+
**/.vs
10+
**/.vscode
11+
**/*.*proj.user
12+
**/*.dbmdl
13+
**/*.jfm
14+
**/azds.yaml
15+
**/bin
16+
**/charts
17+
**/docker-compose*
18+
**/Dockerfile*
19+
**/node_modules
20+
**/npm-debug.log
21+
**/obj
22+
**/secrets.dev.yaml
23+
**/values.dev.yaml
24+
LICENSE
25+
README.md

src/Microsoft.Data.SqlClient.sln

+212
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
2+
3+
FROM mcr.microsoft.com/dotnet/core/runtime:3.1-buster-slim AS base
4+
WORKDIR /app
5+
6+
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
7+
WORKDIR /sqlclient
8+
COPY . .
9+
10+
ARG PROJNAME="Microsoft.Data.SqlClient.DockerLinuxTest"
11+
ARG PROJFILE=$PROJNAME".csproj"
12+
ARG DLLFILE=$PROJNAME".dll"
13+
14+
WORKDIR /sqlclient/src/Microsoft.Data.SqlClient/tests/DockerLinuxTest
15+
RUN dotnet build $PROJFILE -c Release -o /app/build -p:OSGroup=Unix -p:GenerateDocumentationFile=false
16+
17+
FROM build AS publish
18+
RUN dotnet publish $PROJFILE -c Release -o /app/publish -p:OSGroup=Unix -p:GenerateDocumentationFile=false
19+
20+
FROM base AS final
21+
WORKDIR /app
22+
COPY --from=publish /app/publish .
23+
ENTRYPOINT ["dotnet", $DLLFILE]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
5+
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
6+
<DockerfileContext>..\..\..\..</DockerfileContext>
7+
<OSGroup>Unix</OSGroup>
8+
</PropertyGroup>
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.9.10" />
11+
</ItemGroup>
12+
<ItemGroup>
13+
<ProjectReference Include="..\..\netcore\src\Microsoft.Data.SqlClient.csproj" />
14+
</ItemGroup>
15+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using Microsoft.Data.SqlClient;
7+
8+
namespace Microsoft.Data.SqlClient.DockerLinuxTest
9+
{
10+
class Program
11+
{
12+
static string server = "microsoft.sqlserver";
13+
static string user = "sa";
14+
// Provide password as set in docker-compose.yml
15+
static string pwd = "P@ssw0rd!123";
16+
17+
static void Main(string[] args)
18+
{
19+
using (SqlConnection sqlConnection = new SqlConnection($"Server={server}; UID={user}; PWD={pwd}"))
20+
{
21+
sqlConnection.Open();
22+
Console.WriteLine($"Connected to SQL Server v{sqlConnection.ServerVersion} from {Environment.OSVersion.VersionString}");
23+
// Write your code here to debug inside Docker Linux containers.
24+
}
25+
}
26+
}
27+
}
28+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"profiles": {
3+
"Microsoft.Data.SqlClient.DockerLinuxTest": {
4+
"commandName": "Project"
5+
},
6+
"Docker": {
7+
"commandName": "Docker"
8+
}
9+
}
10+
}

src/docker-compose.dcproj

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" Sdk="Microsoft.Docker.Sdk">
3+
<PropertyGroup Label="Globals">
4+
<ProjectVersion>2.1</ProjectVersion>
5+
<DockerTargetOS>Linux</DockerTargetOS>
6+
<ProjectGuid>f5df2fdc-c860-4cb3-8b24-7c903c6fc076</ProjectGuid>
7+
</PropertyGroup>
8+
<PropertyGroup>
9+
<DockerServiceName>microsoft.data.sqlclient.dockertests</DockerServiceName>
10+
</PropertyGroup>
11+
<ItemGroup>
12+
<None Include=".dockerignore" />
13+
<None Include="docker-compose.override.yml">
14+
<DependentUpon>docker-compose.yml</DependentUpon>
15+
</None>
16+
<None Include="docker-compose.yml" />
17+
</ItemGroup>
18+
</Project>

src/docker-compose.override.yml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
version: '3.4'
2+
services:
3+
microsoft.data.sqlclient.dockertests:

src/docker-compose.yml

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
version: '3.4'
2+
3+
services:
4+
microsoft.data.sqlclient.dockertests:
5+
image: ${DOCKER_REGISTRY-}microsoftdatasqlclientdockerlinuxtest
6+
build:
7+
context: ../
8+
dockerfile: src/Microsoft.Data.SqlClient/tests/DockerLinuxTest/Dockerfile
9+
depends_on:
10+
- microsoft.sqlserver
11+
12+
microsoft.sqlserver:
13+
image: mcr.microsoft.com/mssql/server:2019-latest
14+
environment:
15+
- SA_PASSWORD=P@ssw0rd!123
16+
- ACCEPT_EULA=Y
17+
ports:
18+
- "5434:1433"
+32-24
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,33 @@
11
{
2-
"tool": "Credential Scanner",
3-
"suppressions": [
4-
{
5-
"file": "TdsServerCertificate.pfx",
6-
"_justification": "The dummy certificate used for internal testing"
7-
},
8-
{
9-
"file": "config.ps1",
10-
"_justification": "Contains dummy passwords used for internal testing"
11-
},
12-
{
13-
"file": "SqlConnectionBasicTests.cs",
14-
"_justification": "Contains dummy passwords used for internal testing"
15-
},
16-
{
17-
"file": "ExceptionTest.cs",
18-
"_justification": "Contains dummy passwords used for internal testing"
19-
},
20-
{
21-
"file": "TDSServerArguments.cs",
22-
"_justification": "Contains dummy passwords used for internal testing"
23-
}
24-
]
25-
}
2+
"tool": "Credential Scanner",
3+
"suppressions": [
4+
{
5+
"file": "TdsServerCertificate.pfx",
6+
"_justification": "The dummy certificate used for internal testing"
7+
},
8+
{
9+
"file": "config.ps1",
10+
"_justification": "Contains dummy passwords used for internal testing"
11+
},
12+
{
13+
"file": "SqlConnectionBasicTests.cs",
14+
"_justification": "Contains dummy passwords used for internal testing"
15+
},
16+
{
17+
"file": "ExceptionTest.cs",
18+
"_justification": "Contains dummy passwords used for internal testing"
19+
},
20+
{
21+
"file": "TDSServerArguments.cs",
22+
"_justification": "Contains dummy passwords used for internal testing"
23+
},
24+
{
25+
"file": "Program.cs",
26+
"_justification": "Contains dummy passwords used for docker testing"
27+
},
28+
{
29+
"file": "docker-compose.yml",
30+
"_justification": "Contains dummy passwords used for docker testing"
31+
}
32+
]
33+
}

0 commit comments

Comments
 (0)