I would like to refer to this article, which explains it shortly but clearly. The Docker CLI actually sets two attributes (in two requests) to achieve the desired result of the one -p PORT
option.
So, you not only need to specify the "ExposedPorts"
attribute in the /containers/create
request, but also the "PortBindings"
attribute in the /containers/{id}/start
request as follows:
{
"id": id,
"PortBindings": {
"container_port/tcp": [
{
"HostIp": "host_ip", // Strings, not numbers here
"HostPort": "host_port"
}
]
}
}
Don't forget/oversee the square brackets as in David Maze's answer.