Docker MCQs – Docker Images

21.) What does the EXPOSE instruction in a Dockerfile do?

A) Publishes a port to the host machine
B) Maps container ports to host ports
C) Opens all container ports
D) Specifies the ports the container listens on

Answer: Option D

Explanation: The EXPOSE instruction specifies which ports the container listens on during runtime.

22.) What is the difference between a Docker image and a container?

A) A container is a running instance of an image
B) An image is a running instance of a container
C) Containers and images are the same
D) Images store logs, while containers run processes

Answer: Option A

Explanation: A container is a running instance of a Docker image.

23.) How do you export a Docker image to a file?

A) docker save
B) docker export
C) docker push
D) docker tar

Answer: Option A

Explanation: The docker save command exports a Docker image as a .tar file.

24.) What is an image ID in Docker?

A) A unique identifier for a container
B) A hash that uniquely identifies a Docker image
C) A tag assigned to an image
D) A unique identifier for Docker Hub

Answer: Option B

Explanation: An image ID in Docker is a unique identifier assigned to each Docker image. It’s a SHA256 hash that ensures the image’s uniqueness and allows Docker to reference the image efficiently.

25.) Which command removes all unused Docker images?

A) docker rmi
B) docker prune
C) docker system prune
D) docker clean

Answer: Option C

Explanation: The docker system prune command removes all unused Docker images, containers, networks, and volumes.

26.) What happens if you use docker build without a tag?

A) The build will fail
B) Docker assigns a default tag of latest
C) The image is tagged with the username
D) The image is built without a tag

Answer: Option B

Explanation: If no tag is specified, Docker assigns the default tag latest.

27.) Which command optimizes the size of a Docker image by removing intermediate layers?

A) docker prune
B) docker image clean
C) docker build –squash
D) docker optimize

Answer: Option C

Explanation: The docker build –squash command merges image layers to reduce the final image size, although it may need to be explicitly enabled.

28.) What happens if multiple CMD instructions are used in a Dockerfile?

A) All CMD instructions are executed sequentially.
B) The last CMD instruction overrides the previous ones.
C) Docker throws an error during the build process.
D) Only the first CMD instruction is executed.

Answer: Option B

Explanation: If multiple CMD instructions are used, only the last CMD is retained, and the others are overridden.

Leave a Reply

Your email address will not be published. Required fields are marked *