Cloud and Container Basics

$ docker run -d -p 8080:80 nginx

A 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?

  1. Ahttp://192.0.2.10:80/
  2. Bhttp://(container IP address):8080/
  3. 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.
Practice this topic Back to topic

Books on Cloud and Container Basics Ad

As an Amazon Associate I earn from qualifying purchases.

More Cloud and Container Basics questions

  1. Dockerfile (B) above was made from Dockerfile (A), which had RUN npm ci after COPY . ., by changing the order so that only the dependency files are copied first. npm ci takes 120 seconds, npm run build 30 seconds and COPY . . 2 seconds, and the time of the other instructions is negligible. The image is rebuilt 20 times a day after changing only the source code under src (package.json and package-lock.json are unchanged). When the cache from the previous build can be used, which of the following is how much shorter B is than A in total build time per day?
  2. In Docker, which of the following correctly describes named volumes and bind mounts, used to keep data written by a container?
  3. An image is pulled by specifying registry.example.com/team/web:1.4. Which of the following correctly gives the meaning of each part of this reference?
  4. scratch is an empty image that contains no files. Which of the following correctly describes what the final image built with the multi-stage Dockerfile above contains?
  5. In Kubernetes, which of the following is the smallest unit that is deployed and managed, grouping one or more containers that share the same IP address and volumes?