dotnet / Docker.DotNet

:whale: .NET (C#) Client Library for Docker API
https://www.nuget.org/packages/Docker.DotNet/
MIT License
2.23k stars 381 forks source link

Not able to bind port to HostIp #658

Closed salilponde closed 9 months ago

salilponde commented 10 months ago

Output of dotnet --info:

.NET SDK:
 Version:   7.0.403
 Commit:    142776d834

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.22621
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\7.0.403\

Host:
  Version:      7.0.13
  Architecture: x64
  Commit:       3f73a2f186

.NET SDKs installed:
  7.0.403 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 3.1.32 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.24 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 7.0.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 3.1.32 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.16 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.20 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.22 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.23 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.24 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 7.0.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 6.0.24 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 7.0.13 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other architectures found:
  x86   [C:\Program Files (x86)\dotnet]
    registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]

Environment variables:
  Not set

global.json file:
  Not found

Learn more:
  https://aka.ms/dotnet/info

Download .NET:
  https://aka.ms/dotnet/download

What version of Docker.DotNet?:

3.125.15

Steps to reproduce the issue:

  1. Run the below code:
const string containerPort = "3306/tcp";
const string hostPort = "3306/tcp";
const string hostIp = "192.168.0.211";

await client.Containers.CreateContainerAsync(new CreateContainerParameters()
{
    Image = "mysql",
    Env = new List<string> { "MYSQL_ROOT_PASSWORD=123" },
    ExposedPorts = new Dictionary<string, EmptyStruct> {
        { containerPort, new EmptyStruct() }
    },
    HostConfig = new HostConfig()
    {
        PortBindings = new Dictionary<string, IList<PortBinding>>
        {
            { containerPort,  new List<PortBinding> { new PortBinding { HostIP = hostIp, HostPort = hostPort } } }
        }
    }
});
  1. Run: docker start CONTAINER_ID_HERE
  2. Run: docker ps

What actually happened?: The exposed port is bound to 127.0.0.1:32781 instead of 192.168.0.211:3306. Whenever HostIp is specified it binds to 127.0.0.1 and a random port each time.

What did you expect to happen?: The exposed port should be bound to 192.168.0.211:3306

This is what I am trying to replicate in .NET: docker run -d --name mysqltest -e MYSQL_ROOT_PASSWORD=test -p 192.168.0.211:3306:3306 mysql

Additional information:

salilponde commented 9 months ago

Container port needs "/tcp" suffix but host port should NOT have "/tcp" suffix.