Binding a port to a host interface using the REST API

后端 未结 3 2002
南笙
南笙 2021-02-05 11:08

The documentation for the commandline interface says the following:

To bind a port of the container to a specific interface of the host system, use the

相关标签:
3条回答
  • 2021-02-05 11:13

    Starting containers with PortBindings in the HostConfig was deprecated in v1.10 and removed in v1.12.

    Both these configuration parameters should now be included when creating the container.

    POST /containers/create
    
    {
        "Image": image_id,
        "ExposedPorts": {
            "22/tcp": {}
        },
        "HostConfig": {
            "PortBindings": { "22/tcp": [{ "HostPort": "" }] }
        }
    }
    
    0 讨论(0)
  • 2021-02-05 11:14

    This is an undocumented feature. I found my answer on the mailing list:

    When creating the container you have to set ExposedPorts:

    "ExposedPorts": { "22/tcp": {} }
    

    When starting your container you need to set PortBindings:

    "PortBindings": { "22/tcp": [{ "HostPort": "11022" }] }
    

    There already is an issue on github about this.

    0 讨论(0)
  • 2021-02-05 11:20

    I know this question had been answered, I using the above solution and here is how I did it in java using Docker Java Client v3.2.5

        PortBinding portBinding = PortBinding.parse( hostPort + ":" + containerPort);
        HostConfig hostConfig = HostConfig.newHostConfig()
                .withPortBindings(portBinding);
        CreateContainerResponse container =
                dockerClient.createContainerCmd(imageName)
                        .withHostConfig(hostConfig)
                        .withExposedPorts(ExposedPort.parse(containerPort+"/tcp"))
                        .exec();
    
    0 讨论(0)
提交回复
热议问题