Master the essentials of Docker with this set of Docker MCQ questions and answers designed to test your expertise in creating and managing Docker images, writing Dockerfiles, and optimizing them using best practices. This guide will cover pulling, tagging, and pushing images, multi-stage builds, and techniques for optimizing image sizes.
Multiple Choice Questions (MCQs)
Creating and Managing Docker Images
What is a Docker image? a) A running instance of a container b) A read-only template used to create containers c) A script to automate builds d) A storage volume for containers
Which command is used to build a Docker image? a) docker run b) docker build c) docker create d) docker init
What is the default name of a Dockerfile? a) imagefile b) Dockerfile c) Containerfile d) Buildfile
How can you view the list of available Docker images locally? a) docker ls b) docker list images c) docker images d) docker show
What does the docker rmi command do? a) Removes a running container b) Deletes a Docker image c) Rebuilds an image d) Lists all images
Pulling, Tagging, and Pushing Images
Which command is used to pull a Docker image from Docker Hub? a) docker download b) docker pull c) docker fetch d) docker get
What is the purpose of tagging a Docker image? a) To group multiple containers b) To assign a unique identifier to an image version c) To compress the image d) To rename the image
Which command is used to push a Docker image to a remote repository? a) docker deploy b) docker upload c) docker push d) docker send
What is the default Docker image repository? a) GitHub b) Docker Hub c) AWS ECR d) Google Cloud
How can you rename a Docker image? a) docker rename b) docker retag c) docker tag d) docker alter
Writing Dockerfiles (Best Practices)
What keyword is used in a Dockerfile to specify the base image? a) FROM b) BASE c) IMAGE d) START
Which command in a Dockerfile copies files from the local machine to the image? a) ADD b) COPY c) MOVE d) IMPORT
What is the purpose of the CMD instruction in a Dockerfile? a) To define the default command to run in the container b) To execute a script during build c) To expose container ports d) To define environment variables
How can you reduce the size of a Docker image? a) Use fewer RUN instructions b) Combine multiple RUN commands c) Use smaller base images d) All of the above
What does the .dockerignore file do? a) Ignores build errors b) Excludes files from being copied into the image c) Disables certain Docker commands d) Prevents image compression
Multi-Stage Builds
What is the main purpose of multi-stage builds? a) To speed up the Docker build process b) To reduce the size of the final image c) To allow parallel building of images d) To create multiple containers at once
Which Dockerfile instruction is key to multi-stage builds? a) CMD b) WORKDIR c) FROM d) RUN
In a multi-stage build, what happens to intermediate stages? a) They are saved for debugging purposes b) They are discarded after the build completes c) They are compressed and added to the final image d) They are run as separate containers
How do you copy artifacts from one stage to another in a multi-stage build? a) Using the COPY --from flag b) Using the ADD command c) By mounting a shared volume d) Using the INCLUDE command
Why are multi-stage builds commonly used for production images? a) To simplify the deployment process b) To separate development and production dependencies c) To improve runtime performance d) All of the above
Optimizing Image Sizes
What is an Alpine image in Docker? a) A minimal Linux-based image b) A base image for data processing c) A specialized image for debugging d) An image with pre-installed software
Why should you avoid using latest as a tag in production images? a) It can lead to unpredictable updates b) It makes debugging harder c) It increases image size d) It consumes more memory
How can you remove unused Docker images? a) docker clean b) docker prune c) docker rmi –all d) docker rm images
What is the primary benefit of using a slim image? a) Faster builds b) Reduced attack surface c) Smaller image size d) All of the above
Which of the following can increase Docker image size unnecessarily? a) Including unnecessary files b) Using large base images c) Running multiple RUN commands d) All of the above
How can you clean up dangling images? a) docker cleanup b) docker rmi dangling c) docker image prune d) docker remove unused
Which command lists the size of Docker images? a) docker images –size b) docker image ls –size c) docker ls images –size d) docker inspect size
What does the --squash flag do in docker build? a) Combines layers into a single layer b) Deletes unused layers c) Compresses the image d) Optimizes build cache
Why is layer caching important in Docker builds? a) It speeds up subsequent builds b) It reduces image size c) It allows rollback to previous layers d) All of the above
Which of these is not a best practice for optimizing Docker images? a) Using multi-stage builds b) Avoiding large base images c) Adding temporary files for debugging d) Using .dockerignore
Answer Table
Qno
Answer
1
b) A read-only template used to create containers
2
b) docker build
3
b) Dockerfile
4
c) docker images
5
b) Deletes a Docker image
6
b) docker pull
7
b) To assign a unique identifier to an image version
8
c) docker push
9
b) Docker Hub
10
c) docker tag
11
a) FROM
12
b) COPY
13
a) To define the default command to run in the container
14
d) All of the above
15
b) Excludes files from being copied into the image