Cloud and Container Basics
$ docker run -d -p 8080:80 nginxA container is started with the command above from the image nginx, a web server that listens on port 80. The host's IP address is 192.0.2.10, and there are no restrictions such as a firewall. Which of the following is the URL to use to reach this web server from a browser on another PC on the same network?
- Ahttp://192.0.2.10:80/
- Bhttp://(container IP address):8080/
- Chttp://192.0.2.10:8080/
Show answer and explanation
Answer: C. http://192.0.2.10:8080/
The option is written as -p host port:container port, so -p 8080:80 forwards traffic arriving at port 8080 on the host to port 80 in the container. From another PC, connect to port 8080 at the host's IP address. The container's IP address generally belongs to a virtual network inside the host and cannot be reached directly from other PCs. EXPOSE 80 in a Dockerfile only records which port the container uses; it does not publish a host port by itself, so -p is needed.
- A. Nothing is assigned to port 80 on the host. 80 is the port on the container side.
- B. The container's IP address belongs to a virtual network inside the host and generally cannot be reached from other PCs. Also, the container listens on 80, not 8080.
- C. Correct. The first number is the host port and the second is the container port.