nerdexam
Docker

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.

Submitted by chiamaka_o· Apr 18, 2026Networking

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)
  • A
    89% (24)
  • B
    4% (1)
  • D
    7% (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.

ADocker binds each exposed container port to a random port on all the host's interfaceCorrect

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.

BDocker gives extended privileges to the container.

The `-P` option is related to port publishing, not to granting extended privileges; `--privileged` is used for that.

CDocker binds each exposed container port to a random port on a specified host interface

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.

DDocker binds each exposed container port with the same port on the host

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

#Docker run#Port mapping#Exposed ports#Network binding

Community Discussion

No community discussion yet for this question.

Full DCA Practice