What equivalent in Docker.DotNet (NET CORE) docker parameters --expose=[] and -p=[]

Viewed 288

I try to use NET CORE package https://github.com/dotnet/Docker.DotNet to start docker container from NET CORE. Usually my command line looks as

sudo docker run -it \
--name digiwage.$(date +%Y-%m-%d-%H.%M.%S) \
--expose='46003' \
-p '157.XX.XX.85:46003:46003' \
 XXXXXX/daemon:latest

following docker documentation https://docs.docker.com/engine/reference/run/#expose-incoming-ports docker has are two parameters related to port

--expose=[] Expose a port or a range of ports inside the container
-p=[] Publish a container's port or a range of ports to the host

First one is docker exposed port list, second one is ports publishing to docker host. This is different meaning, for example docker host can have a lot of IP address and we need to publish port only to one IP.

I try to start container in this way

    Dim ExposedPorts = New Dictionary(Of String, EmptyStruct)
    ExposedPorts.Add("157.XX.XX.85:46004:46004", New EmptyStruct)
    Dim Prm = New CreateContainerParameters With {
                  .Image = "alexev275/digiwage:latest",
                  .Name = $"digiwage.{Guid.NewGuid}",
                  .Env = New List(Of String),
                  .ExposedPorts = ExposedPorts,
                  .Tty = True
    }

and see in console zero as docker port parameters

enter image description here

I have expected two parameters related to port in CreateContainerParameters, but no, second parameters is absent.

enter image description here

1 Answers

I found workable solution.

enter image description here

    Dim ExposedPorts = New Dictionary(Of String, EmptyStruct)
    Dim ContainerPort = "46003"
    ExposedPorts.Add(ContainerPort, New EmptyStruct)

    Dim OneHostBinding As New PortBinding With {.HostIP = "157.XX.XX.86", .HostPort = "46003"}
    Dim HostBindingList As New List(Of PortBinding)
    HostBindingList.Add(OneHostBinding)
    Dim PortBindings As New Dictionary(Of String, IList(Of PortBinding))
    PortBindings.Add("46003", HostBindingList)

    Dim Labels = New Dictionary(Of String, String)
    Labels.Add("TstKey", "TstVal")
    Dim CMD = New List(Of String)
    CMD.Add("pwd")
    Dim Prm = New CreateContainerParameters With {
                  .Image = "alexev275/digiwage:latest",
                  .Name = $"digiwage.{Guid.NewGuid}",
                  .Env = Env,
                  .ExposedPorts = ExposedPorts,
                  .Labels = Labels,
                  .Tty = True,
                  .HostConfig = New HostConfig With {.PortBindings = PortBindings}
    }
Related