DevOps Docker & Containerization 2 — Questions and Answers
Question 1: What is a multi-stage Docker build used for?
- To reduce final image size by discarding build-time dependencies (Correct answer)
- To build images on multiple hosts simultaneously
- To run multiple containers within a single Dockerfile
- To create images for multiple architectures at once
Correct answer: To reduce final image size by discarding build-time dependencies
Multi-stage builds use multiple FROM statements to separate the build environment from the runtime environment, keeping the final image small and secure.
Question 2: What is Docker Compose primarily used for?
- Defining and running multi-container Docker applications with a single YAML file (Correct answer)
- Building and pushing Docker images to a registry
- Orchestrating containers across multiple hosts in a cluster
- Monitoring container resource usage and health
Correct answer: Defining and running multi-container Docker applications with a single YAML file
Docker Compose reads a `docker-compose.yml` file to define, configure, and start multiple interconnected containers as a single application.
Question 3: Which Docker command removes all stopped containers, unused images, networks, and build cache?
- docker system prune (Correct answer)
- docker rm -f
- docker rmi -a
- docker clean all
Correct answer: docker system prune
`docker system prune` frees disk space by removing stopped containers, dangling images, unused networks, and build cache in one command.
Question 4: What is a Docker volume used for?
- Persisting data generated by and used by Docker containers (Correct answer)
- Sharing CPU and memory resources between containers
- Encrypting container network traffic
- Limiting container access to the host filesystem
Correct answer: Persisting data generated by and used by Docker containers
Docker volumes provide persistent storage that survives container restarts and deletions, managed by Docker outside the container's writable layer.
Question 5: What does the .dockerignore file do?
- Excludes files and directories from the Docker build context (Correct answer)
- Specifies which containers to ignore during docker ps
- Lists images that should not be pulled from Docker Hub
- Prevents certain Dockerfile instructions from executing
Correct answer: Excludes files and directories from the Docker build context
The .dockerignore file lists patterns of files to exclude from the build context sent to the Docker daemon, reducing build time and preventing sensitive files from being included.
Question 6: What is the purpose of the CMD instruction in a Dockerfile?
- Provides the default command to run when a container starts (Correct answer)
- Runs a command during the image build process
- Sets environment variables inside the container
- Copies files from the host into the image
Correct answer: Provides the default command to run when a container starts
CMD specifies the default command executed when a container is started from the image, and can be overridden by arguments passed to `docker run`.
What is a multi-stage Docker build used for?