Skip to content

Commit

Permalink
Introduce Docker debugging with Docker Compose orchestration support (d…
Browse files Browse the repository at this point in the history
  • Loading branch information
cheenamalhotra authored Apr 21, 2020
1 parent 68eff11 commit 5ceec82
Show file tree
Hide file tree
Showing 12 changed files with 440 additions and 24 deletions.
25 changes: 25 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
**/.classpath
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/azds.yaml
**/bin
**/charts
**/docker-compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
LICENSE
README.md
31 changes: 31 additions & 0 deletions BUILDGUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,34 @@ Managed SNI can be enabled on Windows by enabling the below AppContext switch:
Scaled decimal parameter truncation can be enabled by enabling the below AppContext switch:

**"Switch.Microsoft.Data.SqlClient.TruncateScaledDecimal"**

## Debugging SqlClient on Linux from Windows

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.

This project is also included in `docker-compose.yml` to demonstrate connectivity with SQL Server docker image.

To run the same:
1. Build the Solution in Visual Studio
2. Set `docker-compose` as Startup Project
3. Run "Docker-Compose" launch configuration.
4. You will see similar message in Debug window:
```log
Connected to SQL Server v15.00.4023 from Unix 4.19.76.0
The program 'dotnet' has exited with code 0 (0x0).
```
5. Now you can write code in [Program.cs](/src/Microsoft.Data.SqlClient/tests/DockerLinuxTest/Program.cs) to debug SqlClient on Linux!
### Troubleshooting Docker issues
There may be times where connection cannot be made to SQL Server, we found below ideas helpful:
- 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.
- If you face `sni.dll not found` errors when debugging, try updating below properties in netcore\Microsoft.Data.SqlClient.csproj file and try again:
```xml
<OSGroup>Unix</OSGroup>
<TargetsWindows>false</TargetsWindows>
<TargetsUnix>true</TargetsUnix>
```
```
25 changes: 25 additions & 0 deletions src/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
**/.classpath
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/azds.yaml
**/bin
**/charts
**/docker-compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
LICENSE
README.md
212 changes: 212 additions & 0 deletions src/Microsoft.Data.SqlClient.sln

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions src/Microsoft.Data.SqlClient/tests/DockerLinuxTest/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/core/runtime:3.1-buster-slim AS base
WORKDIR /app

FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /sqlclient
COPY . .

ARG PROJNAME="Microsoft.Data.SqlClient.DockerLinuxTest"
ARG PROJFILE=$PROJNAME".csproj"
ARG DLLFILE=$PROJNAME".dll"

WORKDIR /sqlclient/src/Microsoft.Data.SqlClient/tests/DockerLinuxTest
RUN dotnet build $PROJFILE -c Release -o /app/build -p:OSGroup=Unix -p:GenerateDocumentationFile=false

FROM build AS publish
RUN dotnet publish $PROJFILE -c Release -o /app/publish -p:OSGroup=Unix -p:GenerateDocumentationFile=false

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", $DLLFILE]
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<DockerfileContext>..\..\..\..</DockerfileContext>
<OSGroup>Unix</OSGroup>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.9.10" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\netcore\src\Microsoft.Data.SqlClient.csproj" />
</ItemGroup>
</Project>
28 changes: 28 additions & 0 deletions src/Microsoft.Data.SqlClient/tests/DockerLinuxTest/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using Microsoft.Data.SqlClient;

namespace Microsoft.Data.SqlClient.DockerLinuxTest
{
class Program
{
static string server = "microsoft.sqlserver";
static string user = "sa";
// Provide password as set in docker-compose.yml
static string pwd = "P@ssw0rd!123";

static void Main(string[] args)
{
using (SqlConnection sqlConnection = new SqlConnection($"Server={server}; UID={user}; PWD={pwd}"))
{
sqlConnection.Open();
Console.WriteLine($"Connected to SQL Server v{sqlConnection.ServerVersion} from {Environment.OSVersion.VersionString}");
// Write your code here to debug inside Docker Linux containers.
}
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"profiles": {
"Microsoft.Data.SqlClient.DockerLinuxTest": {
"commandName": "Project"
},
"Docker": {
"commandName": "Docker"
}
}
}
18 changes: 18 additions & 0 deletions src/docker-compose.dcproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" Sdk="Microsoft.Docker.Sdk">
<PropertyGroup Label="Globals">
<ProjectVersion>2.1</ProjectVersion>
<DockerTargetOS>Linux</DockerTargetOS>
<ProjectGuid>f5df2fdc-c860-4cb3-8b24-7c903c6fc076</ProjectGuid>
</PropertyGroup>
<PropertyGroup>
<DockerServiceName>microsoft.data.sqlclient.dockertests</DockerServiceName>
</PropertyGroup>
<ItemGroup>
<None Include=".dockerignore" />
<None Include="docker-compose.override.yml">
<DependentUpon>docker-compose.yml</DependentUpon>
</None>
<None Include="docker-compose.yml" />
</ItemGroup>
</Project>
3 changes: 3 additions & 0 deletions src/docker-compose.override.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
version: '3.4'
services:
microsoft.data.sqlclient.dockertests:
18 changes: 18 additions & 0 deletions src/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
version: '3.4'

services:
microsoft.data.sqlclient.dockertests:
image: ${DOCKER_REGISTRY-}microsoftdatasqlclientdockerlinuxtest
build:
context: ../
dockerfile: src/Microsoft.Data.SqlClient/tests/DockerLinuxTest/Dockerfile
depends_on:
- microsoft.sqlserver

microsoft.sqlserver:
image: mcr.microsoft.com/mssql/server:2019-latest
environment:
- SA_PASSWORD=P@ssw0rd!123
- ACCEPT_EULA=Y
ports:
- "5434:1433"
56 changes: 32 additions & 24 deletions tools/credScan/CredScanSuppressions.json
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
{
"tool": "Credential Scanner",
"suppressions": [
{
"file": "TdsServerCertificate.pfx",
"_justification": "The dummy certificate used for internal testing"
},
{
"file": "config.ps1",
"_justification": "Contains dummy passwords used for internal testing"
},
{
"file": "SqlConnectionBasicTests.cs",
"_justification": "Contains dummy passwords used for internal testing"
},
{
"file": "ExceptionTest.cs",
"_justification": "Contains dummy passwords used for internal testing"
},
{
"file": "TDSServerArguments.cs",
"_justification": "Contains dummy passwords used for internal testing"
}
]
}
"tool": "Credential Scanner",
"suppressions": [
{
"file": "TdsServerCertificate.pfx",
"_justification": "The dummy certificate used for internal testing"
},
{
"file": "config.ps1",
"_justification": "Contains dummy passwords used for internal testing"
},
{
"file": "SqlConnectionBasicTests.cs",
"_justification": "Contains dummy passwords used for internal testing"
},
{
"file": "ExceptionTest.cs",
"_justification": "Contains dummy passwords used for internal testing"
},
{
"file": "TDSServerArguments.cs",
"_justification": "Contains dummy passwords used for internal testing"
},
{
"file": "Program.cs",
"_justification": "Contains dummy passwords used for docker testing"
},
{
"file": "docker-compose.yml",
"_justification": "Contains dummy passwords used for docker testing"
}
]
}

0 comments on commit 5ceec82

Please sign in to comment.