Exposing host ports on Docker w/ Windows containers vs. Docker w/ Linux containers is inconsistent
- Dominant language
- C#
- Stars
- 2.4k
- Forks
- 416
- PR merge metrics
- No merged PRs in 30d
Description
**Output of `dotnet --info`:**
```
.NET Core SDK (reflecting any global.json):
Version: 2.1.500
Commit: b68b931422
Runtime Environment:
OS Name: Windows
OS Version: 10.0.17134
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\2.1.500\
Host (useful for support):
Version: 2.1.6
Commit: 3f4f8eebd8
.NET Core SDKs installed:
2.0.2 [C:\Program Files\dotnet\sdk]
2.1.103 [C:\Program Files\dotnet\sdk]
2.1.200 [C:\Program Files\dotnet\sdk]
2.1.403 [C:\Program Files\dotnet\sdk]
2.1.500 [C:\Program Files\dotnet\sdk]
.NET Core runtimes installed:
Microsoft.AspNetCore.All 2.1.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.App 2.1.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 1.1.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.0.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.0.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
```
**What version of Docker.DotNet?:**
```
3.125.2
```
**Steps to reproduce the issue:**
Attempt to start a SQL Server Docker container using the following code:
```csharp
///
/// Fixture used to run SQL Server
///
public class SqlServerFixture : IAsyncLifetime
{
protected string SqlServerImageName
{
get
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return "microsoft/mssql-server-windows-express";
}
else // Linux or OS X
{
return "mcr.microsoft.com/mssql/server";
}
}
}
protected string SqlServerImageTag
{
get
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return "2017-latest";
}
else // Linux or OS X
{
return "2017-latest-ubuntu";
}
}
}
protected readonly string SqlContainerName = $"sqlserver-{Guid.NewGuid():N}";
protected DockerClient Client;
public string ConnectionString { get; private set; }
public SqlServerFixture()
{
DockerClientConfiguration config;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
config = new DockerClientConfiguration(new Uri("unix://var/run/docker.sock"));
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
config = new DockerClientConfiguration(new Uri("npipe://./pipe/docker_engine"));
else
throw new NotSupportedException($"Unsupported OS [{RuntimeInformation.OSDescription}]");
Client = config.CreateClient();
}
public async Task InitializeAsync()
{
var images = await Client.Images.ListImagesAsync(new ImagesListParameters { MatchName = SqlServerImageName });
if (images.Count == 0)
await Client.Images.CreateImageAsync(
new ImagesCreateParameters { FromImage = SqlServerImageName, Tag = "latest"}, null,
new Progress(message =>
{
Console.WriteLine(!string.IsNullOrEmpty(message.ErrorMessage)
? message.ErrorMessage
: $"{message.ID} {message.Status} {message.ProgressMessage}");
}));
var sqlServerHostPort = ThreadLocalRandom.Current.Next(9000, 10000);
// create the container
await Client.Containers.CreateContainerAsync(new CreateContainerParameters
{
Image = SqlServerImageName,
Name = SqlContainerName,
Tty = true,
HostConfig = new HostConfig
{
PortBindings = new Dictionary>
{
{
"1433/tcp",
new List
{
new PortBinding
{
HostPort = $"{sqlServerHostPort}"
}
}
}
},
},
Env = new string[] { "ACCEPT_EULA=Y", "SA_PASSWORD=l0lTh1sIsOpenSource" }
});
// start the container
await Client.Containers.StartContainerAsync(SqlContainerName, new ContainerStartParameters());
// Provide a 30 second startup delay
await Task.Delay(TimeSpan.FromSeconds(30));
var connectionString = new DbConnectionStringBuilder
{
ConnectionString =
"data source=.;database=akka_persistence_tests;user id=sa;password=l0lTh1sIsOpenSource"
};
connectionString["Data Source"] = $"localhost,{sqlServerHostPort}";
ConnectionString = connectionString.ToString();
}
public async Task DisposeAsync()
{
if (Client != null)
{
await Client.Containers.StopContainerAsync(SqlContainerName, new ContainerStopParameters());
await Client.Containers.RemoveContainerAsync(SqlContainerName,
new ContainerRemoveParameters { Force = true });
Client.Dispose();
}
}
}
```
**What actually happened?:**
Turns out that none of the ports for the _Windows containers_ were ever actually opened - I had to add this to my `CreateContainerParameters` in order to expose the port we use to connect to SQL Server:
```
ExposedPorts = new Dictionary()
{
{ "1433/tcp", new EmptyStruct(){}}
},
```
Our Linux containers, however, worked fine without a hitch.
**What did you expect to happen?:**
I expected to be able to connect to the containers using only the `HostConfig` definition.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.