DCA · Question #15
You have created a Docker bridge network on a host with three containers attached, how do you make this containers accessible outside of the host?
The correct answer is B. Use either EXPOSE or --publish to access the containers on the bridge network. To make containers on a Docker bridge network accessible from outside the host, you must map container ports to host ports using the --publish (or -p) flag.
Question
You have created a Docker bridge network on a host with three containers attached, how do you make this containers accessible outside of the host?
Options
- AUse network attach to access the containers on the bridge network
- BUse either EXPOSE or --publish to access the containers on the bridge network
- CUse network connect to access the containers on the bridge network
- DUse --link to access the containers on the bridge network Correct
How the community answered
(15 responses)- A7% (1)
- B87% (13)
- C7% (1)
Why each option
To make containers on a Docker bridge network accessible from outside the host, you must map container ports to host ports using the `--publish` (or `-p`) flag.
`docker network attach` is used to attach an already running container to an existing network, not to make it accessible from outside the host.
The `--publish` (or `-p`) flag explicitly maps a container's port to a host's port, making the service accessible on the host's IP address. While `EXPOSE` only documents ports, it works with `-P` to publish them to random host ports, making both relevant for external access.
`docker network connect` is used to connect a container to a network, but does not provide external host access.
The `--link` flag is a legacy feature for inter-container communication on the same Docker host and does not facilitate external access from outside the host.
Concept tested: Exposing Docker container ports
Source: https://docs.docker.com/engine/reference/commandline/run/#publish-or-expose-ports
Topics
Community Discussion
No community discussion yet for this question.