Skip to content
This repository has been archived by the owner on Apr 28, 2018. It is now read-only.

How to use PortBindings property of HostConfig? #174

Closed
artisticcheese opened this issue Jan 10, 2017 · 8 comments
Closed

How to use PortBindings property of HostConfig? #174

artisticcheese opened this issue Jan 10, 2017 · 8 comments
Assignees
Labels

Comments

@artisticcheese
Copy link

Hello,

I'm having hard time understanding how to do port mapping with PortBindings property of HostConfig.
Adding -p 88:88 to docker run results in following docker condfiguration, how do I do that while utilizing Docker-Powershell?


"PortBindings": {
    "80/tcp": [
        {
            "HostIp": "",
            "HostPort": "88"
        }
    ]
},
@swernli
Copy link
Contributor

swernli commented Jan 10, 2017

This is definitely an area where we need improved support in our cmdlets. We are considering some helper cmdlets that let users more easily create host configurations in the correct format without needing to muddle through the arcane .NET needed to get this object set up. That said, at least there is a way to make it work for now:

$pb = new-object Docker.DotNet.Models.PortBinding
$pb.HostPort = "88"
$hostConfig = new-object Docker.DotNet.Models.HostConfig
$hostConfig.PortBindings = [System.Collections.Generic.Dictionary[string, System.Collections.Generic.iList[Docker.DotNet.Models.PortBinding]]]::new()
$hostConfig.PortBindings.Add("80/tcp",[System.Collections.Generic.List[Docker.DotNet.Models.PortBinding]]::new([Docker.DotNet.Models.PortBinding[]]@($pb)))
$c = New-Container -HostConfiguration $hostConfig microsoft/nanoserver
(Get-ContainerDetail $c).HostConfig.PortBindings

That last command should show the result:

Key    Value
---    -----
80/tcp {Docker.DotNet.Models.PortBinding}

And docker inspect confirms the same (trimmed output):

"PortBindings": {
                "80/tcp": [
                    {
                        "HostIp": "",
                        "HostPort": "88"
                    }
                ]
            },

@artisticcheese
Copy link
Author

What am I doing wrong? Port mapping is still not being created

$pb = new-object Docker.DotNet.Models.PortBinding; $pb.HostPort = "8833"
$hostConfig = new-object Docker.DotNet.Models.HostConfig
$hostConfig.PortBindings = [System.Collections.Generic.Dictionary[string, System.Collections.Generic.iList[Docker.DotNet.Models.PortBinding]]]::new()
Run-ContainerImage -HostConfiguration $hostConfig -ImageIdOrName microsoft/iis -Detach
PS C:\Users\gsuvalia> docker ps
CONTAINER ID        IMAGE                  COMMAND                   CREATED             STATUS              PORTS                  NAMES
5a908436c54c        microsoft/iis          "C:\\ServiceMonitor..."   23 seconds ago      Up 4 seconds                               keen_heyrovsky

@swernli
Copy link
Contributor

swernli commented Jan 19, 2017

You are setting the PortBindings to a new, empty object, but not adding the $pb into it.  You are missing the line where you call Add:

$hostConfig.PortBindings.Add("80/tcp",[System.Collections.Generic.List[Docker.DotNet.Models.PortBinding]]::new([Docker.DotNet.Models.PortBinding[]]@($pb)))

@artisticcheese
Copy link
Author

Still does not work

Import-module docker
$hostConfig = [Docker.DotNet.Models.HostConfig]::new()
$pb = new-object Docker.DotNet.Models.PortBinding
$pb.HostPort = "20003"
$hostConfig.PortBindings = [System.Collections.Generic.Dictionary[string, System.Collections.Generic.iList[Docker.DotNet.Models.PortBinding]]]::new()
$hostConfig.PortBindings.Add("80/tcp",[System.Collections.Generic.List[Docker.DotNet.Models.PortBinding]]::new([Docker.DotNet.Models.PortBinding[]]@($pb)))
Run-ContainerImage -ImageIdOrName "microsoft/iis:latest" -Detach -HostConfiguration $hostConfig -Configuration $config

First one is created by Service Fabric and second one created by script above

PS C:\Windows\system32> docker ps
CONTAINER ID        IMAGE                       COMMAND                   CREATED             STATUS              PORTS                   NAMES
388e13af1c66        artisticcheese/iis:latest   "cmd /S /C 'powers..."    16 seconds ago      Up 14 seconds       0.0.0.0:20001->80/tcp   sf-2-ea2ad3b0-e29a-4c1c-9eca-1a635164b788
9bdf02887eba        microsoft/iis:latest        "C:\\ServiceMonitor..."   16 seconds ago      Up 10 seconds                               berserk_heyrovsky

@swernli
Copy link
Contributor

swernli commented Jan 19, 2017

If you perform a docker inspect on the second container, do you see the port binding listed in the host config?

@artisticcheese
Copy link
Author

artisticcheese commented Jan 19, 2017

I can see it but it's missing bunch of other mapping in other areas (network settings for example) of inspect file. See comparison below
Service Fabric generated one

[
    {
        "Id": "388e13af1c66de33c20a2fdf54524b39139d30a7d896ef43626a6f2a54b7bc3b",
        "Created": "2017-01-19T22:49:08.4195225Z",
        "Path": "cmd",
        "Args": [
            "/S",
            "/C",
            "powershell.exe c:\\startup.ps1; del c:\\startup.ps1; C:\\ServiceMonitor.exe w3svc"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 1948,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2017-01-19T22:49:10.045483Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:3819a5d15953b6daf12f631713662dc29a67defc4e958cce15cf7f39a7d2d4d8",
        "ResolvConfPath": "",
        "HostnamePath": "",
        "HostsPath": "",
        "LogPath": "E:\\images\\containers\\388e13af1c66de33c20a2fdf54524b39139d30a7d896ef43626a6f2a54b7bc3b\\388e13af1c66de33c20a2fdf54524b39139d30a7d896ef43626a6f2a54b7bc3b-json.log",
        "Name": "/sf-2-ea2ad3b0-e29a-4c1c-9eca-1a635164b788",
        "RestartCount": 0,
        "Driver": "windowsfilter",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": null,
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {
                "80/tcp": [
                    {
                        "HostIp": "",
                        "HostPort": "20001"
                    }
                ]
            },
            "RestartPolicy": {
                "Name": "",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": null,
            "CapAdd": null,
            "CapDrop": null,
            "Dns": null,
            "DnsOptions": null,
            "DnsSearch": null,
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "",
            "Cgroup": "",
            "Links": null,
            "OomScoreAdj": 0,
            "PidMode": "",
            "Privileged": false,
            "PublishAllPorts": false,
            "ReadonlyRootfs": false,
            "SecurityOpt": null,
            "UTSMode": "",
            "UsernsMode": "",
            "ShmSize": 0,
            "ConsoleSize": [
                0,
                0
            ],
            "Isolation": "process",
            "CpuShares": 0,
            "Memory": 0,
            "CgroupParent": "",
            "BlkioWeight": 0,
            "BlkioWeightDevice": null,
            "BlkioDeviceReadBps": null,
            "BlkioDeviceWriteBps": null,
            "BlkioDeviceReadIOps": null,
            "BlkioDeviceWriteIOps": null,
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpusetCpus": "",
            "CpusetMems": "",
            "Devices": null,
            "DiskQuota": 0,
            "KernelMemory": 0,
            "MemoryReservation": 0,
            "MemorySwap": 0,
            "MemorySwappiness": null,
            "OomKillDisable": null,
            "PidsLimit": 0,
            "Ulimits": null,
            "CpuCount": 0,
            "CpuPercent": 0,
            "IOMaximumIOps": 0,
            "IOMaximumBandwidth": 0
        },
        "GraphDriver": {
            "Name": "windowsfilter",
            "Data": {
                "dir": "E:\\images\\windowsfilter\\388e13af1c66de33c20a2fdf54524b39139d30a7d896ef43626a6f2a54b7bc3b"
            }
        },
        "Mounts": [],
        "Config": {
            "Hostname": "388e13af1c66",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "80/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "containeradmin=contadmin",
                "containerpassword=A123456!",
                "Fabric_ApplicationHostId=ea2ad3b0-e29a-4c1c-9eca-1a635164b788",
                "Fabric_ApplicationHostType=Activated_SingleCodePackage",
                "Fabric_ApplicationId=PI_CalculatorType_App2",
                "Fabric_ApplicationName=fabric:/PI_Calculator",
                "Fabric_CodePackageInstanceId=131292594170112489",
                "Fabric_CodePackageName=Code",
                "Fabric_Endpoint_PI_Web_ServiceTypeEndpoint=20001",
                "Fabric_Folder_App_Log=C:\\ProgramData\\SF\\host1\\Fabric\\work\\Applications\\PI_CalculatorType_App2\\log",
                "Fabric_Folder_App_Temp=C:\\ProgramData\\SF\\host1\\Fabric\\work\\Applications\\PI_CalculatorType_App2\\temp",
                "Fabric_Folder_App_Work=C:\\ProgramData\\SF\\host1\\Fabric\\work\\Applications\\PI_CalculatorType_App2\\work",
                "Fabric_Folder_Application=C:\\ProgramData\\SF\\host1\\Fabric\\work\\Applications\\PI_CalculatorType_App2",
                "Fabric_NodeId=81b2453aa85c889f01eca394823d12cf",
                "Fabric_NodeIPOrFQDN=containerhost1",
                "Fabric_NodeName=host1",
                "Fabric_RuntimeConnectionAddress=localhost:19003",
                "Fabric_ServicePackageInstanceId=131292594170092574",
                "Fabric_ServicePackageName=PI_Web_ServicePkg",
                "Fabric_ServicePackageVersionInstance=1.0:4.5:131289746513285146",
                "FabricPackageFileName=C:\\ProgramData\\SF\\host1\\Fabric\\Fabric.Package.current.xml"
            ],
            "Cmd": null,
            "ArgsEscaped": true,
            "Image": "artisticcheese/iis:latest",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": [
                "cmd",
                "/S",
                "/C",
                "powershell.exe c:\\startup.ps1; del c:\\startup.ps1; C:\\ServiceMonitor.exe w3svc"
            ],
            "OnBuild": null,
            "Labels": {}
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "e82eeb46591a3b842715231e396bfb563fd52d4df80ec6bf02eca481f840a177",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "80/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "20001"
                    }
                ]
            },
            "SandboxKey": "e82eeb46591a",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "",
            "Gateway": "",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "",
            "IPPrefixLen": 0,
            "IPv6Gateway": "",
            "MacAddress": "",
            "Networks": {
                "nat": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "2c5a8847d03d21fd0febe1e2431cd4435e2747d27d38facab63bdecd40c12c8c",
                    "EndpointID": "5d303f4925e909ed2dd5f2c6234f3690ba7594a4bf25c4d40a61d81fafedad7c",
                    "Gateway": "",
                    "IPAddress": "172.28.23.187",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "00:15:5d:6f:11:1b"
                }
            }
        }
    }
]

Mine

[
    {
        "Id": "a11669e4f4ead0304482849d9a155e6f54d1dca5d59d4f6289e4597d0d8ef71d",
        "Created": "2017-01-19T22:50:53.2927507Z",
        "Path": "C:\\ServiceMonitor.exe",
        "Args": [
            "w3svc"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 9380,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2017-01-19T22:50:56.7231928Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:7d4c79e586fdf0da8dbf31a6bcd1a441dd8abd18cc587a68a844e7aa18817919",
        "ResolvConfPath": "",
        "HostnamePath": "",
        "HostsPath": "",
        "LogPath": "E:\\images\\containers\\a11669e4f4ead0304482849d9a155e6f54d1dca5d59d4f6289e4597d0d8ef71d\\a11669e4f4ead0304482849d9a155e6f54d1dca5d59d4f6289e4597d0d8ef71d-json.log",
        "Name": "/stoic_williams",
        "RestartCount": 0,
        "Driver": "windowsfilter",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": null,
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {
                "80/tcp": [
                    {
                        "HostIp": "",
                        "HostPort": "20003"
                    }
                ]
            },
            "RestartPolicy": {
                "Name": "",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": null,
            "CapAdd": null,
            "CapDrop": null,
            "Dns": null,
            "DnsOptions": null,
            "DnsSearch": null,
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "",
            "Cgroup": "",
            "Links": null,
            "OomScoreAdj": 0,
            "PidMode": "",
            "Privileged": false,
            "PublishAllPorts": false,
            "ReadonlyRootfs": false,
            "SecurityOpt": null,
            "UTSMode": "",
            "UsernsMode": "",
            "ShmSize": 0,
            "ConsoleSize": [
                0,
                0
            ],
            "Isolation": "process",
            "CpuShares": 0,
            "Memory": 0,
            "CgroupParent": "",
            "BlkioWeight": 0,
            "BlkioWeightDevice": null,
            "BlkioDeviceReadBps": null,
            "BlkioDeviceWriteBps": null,
            "BlkioDeviceReadIOps": null,
            "BlkioDeviceWriteIOps": null,
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpusetCpus": "",
            "CpusetMems": "",
            "Devices": null,
            "DiskQuota": 0,
            "KernelMemory": 0,
            "MemoryReservation": 0,
            "MemorySwap": 0,
            "MemorySwappiness": null,
            "OomKillDisable": null,
            "PidsLimit": 0,
            "Ulimits": null,
            "CpuCount": 0,
            "CpuPercent": 0,
            "IOMaximumIOps": 0,
            "IOMaximumBandwidth": 0
        },
        "GraphDriver": {
            "Name": "windowsfilter",
            "Data": {
                "dir": "E:\\images\\windowsfilter\\a11669e4f4ead0304482849d9a155e6f54d1dca5d59d4f6289e4597d0d8ef71d"
            }
        },
        "Mounts": [],
        "Config": {
            "Hostname": "a11669e4f4ea",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": true,
            "AttachStderr": true,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": null,
            "Cmd": null,
            "ArgsEscaped": true,
            "Image": "microsoft/iis:latest",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": [
                "C:\\ServiceMonitor.exe",
                "w3svc"
            ],
            "OnBuild": null,
            "Labels": {}
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "82bbecb894b50ed3845642594d4174154b820498800e5f422d22bf9a58978e7a",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {},
            "SandboxKey": "82bbecb894b5",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "",
            "Gateway": "",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "",
            "IPPrefixLen": 0,
            "IPv6Gateway": "",
            "MacAddress": "",
            "Networks": {
                "nat": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "2c5a8847d03d21fd0febe1e2431cd4435e2747d27d38facab63bdecd40c12c8c",
                    "EndpointID": "4cdb888ec69da74e548e23a255bf6027f62c0264111894ceea55d2de6a2b03a4",
                    "Gateway": "",
                    "IPAddress": "172.28.31.86",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "00:15:5d:6f:1e:ca"
                }
            }
        }
    }
]

@swernli
Copy link
Contributor

swernli commented Jan 19, 2017

Have you tried configuring the missing ones on your input objects? Maybe NetworkSettings.Ports and Config.ExposedPorts also need to be configured.

@artisticcheese
Copy link
Author

Here is the solution for anybody with the same issue. If you want to port map 8080 on container host to port 80 on container

Import-module docker
$config = [Docker.DotNet.Models.Config]::new()
($config.ExposedPorts  = [System.Collections.Generic.Dictionary[string,object]]::new()).Add("80/tcp", $null)
$hostConfig = [Docker.DotNet.Models.HostConfig]::new()
$pb = new-object Docker.DotNet.Models.PortBinding
$pb.HostPort = "8080"
$hostConfig.PortBindings = [System.Collections.Generic.Dictionary[string, System.Collections.Generic.iList[Docker.DotNet.Models.PortBinding]]]::new()
$hostConfig.PortBindings.Add("80/tcp",[System.Collections.Generic.List[Docker.DotNet.Models.PortBinding]]::new([Docker.DotNet.Models.PortBinding[]]@($pb)))
Run-ContainerImage -ImageIdOrName "microsoft/iis:latest" -Configuration $config -Detach -HostConfiguration $hostConfig -Name $pb.HostPort  

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants