DCA · Question #18
Which of the following is true about using the '-P' option when creating a new container?
The correct answer is A. Docker binds each exposed container port to a random port on all the host's interface. The -P (uppercase) option automatically publishes all ports that a container EXPOSEs to a random available port on the host machine across all network interfaces.
Question
Which of the following is true about using the '-P' option when creating a new container?
Options
- ADocker binds each exposed container port to a random port on all the host's interface
- BDocker gives extended privileges to the container.
- CDocker binds each exposed container port to a random port on a specified host interface
- DDocker binds each exposed container port with the same port on the host
How the community answered
(27 responses)- A89% (24)
- B4% (1)
- D7% (2)
Why each option
The `-P` (uppercase) option automatically publishes all ports that a container `EXPOSE`s to a random available port on the host machine across all network interfaces.
When using `-P`, Docker inspects the container image for any `EXPOSE` instructions and automatically maps each exposed container port to an ephemeral, random port on the host machine, applied to all available host network interfaces.
The `-P` option is related to port publishing, not to granting extended privileges; `--privileged` is used for that.
The `-P` option binds to all host interfaces by default, not a specified one. To specify an interface, the full `-p IP:host_port:container_port` syntax is used.
The `-P` option binds to a *random* port on the host, not necessarily the same port as the container's exposed port; that specific mapping requires the `-p host_port:container_port` syntax.
Concept tested: Docker container auto-publishing ports (-P)
Source: https://docs.docker.com/engine/reference/commandline/run/#publish-or-expose-ports
Topics
Community Discussion
No community discussion yet for this question.