Search code examples
dockerdocker-network

Can Docker map multiple container ports to one host port?


Based on my understanding port mapping is 1 to 1, what I don't understand is why the data structure used for port mapping in container data is like this,

"NetworkSettings": {
[...]
"Ports": {
    "8888/tcp": [
        {
            "HostIp": "0.0.0.0",
            "HostPort": "8888"
        }
    ]
}

The "8888/tcp" key maps to a list instead of single object. Thus in a Java client the data structure for Ports is like this Map<String, List<PortBinding>>, but the List here can only contain 1 element right? Or did I terribly miss something fundamental?


Solution

  • This is perfectly legal:

    docker run -tid -p 8080:80 -p 8090:80 nginx

    "Ports": {
                "443/tcp": null,
                "80/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "8090"
                    },
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "8080"
                    }
                ]
            }
    

    So no, it's not 1 to 1.