Docker Basics Bonus Content
30 questions on Docker Basics Tutorial.
Question 1: What is the core difference between a Docker Image and a Docker Container?
- A. Images are written in Python, while Containers are written in Go.
- B. An Image is a read-only blueprint containing libraries and code, while a Container is a live, runnable instance of an Image. β (correct answer)
- C. Images run on servers, while Containers run on desktop systems.
- D. Containers are compile-time files, while Images are run-time files.
Explanation: Images represent the static, packed environment. Containers are execution runtimes spawned from those images.
Question 2: In a Dockerfile, what is the key difference between the COPY and ADD instructions?
- A. COPY compiles code, while ADD only transfers text.
- B. COPY is used for frontend assets, while ADD is used for backend configuration.
- C. Both copy local files, but ADD also supports fetching files from remote URLs and unpacking local tar archives automatically. β (correct answer)
- D. COPY is deprecated and replaced by ADD.
Explanation: COPY is preferred for simple file duplicates. ADD has extra features (URL support, auto-extract) that can invite security risks if not managed.
Question 3: What is the difference between CMD and ENTRYPOINT instructions in a Dockerfile?
- A. CMD is compile-time, while ENTRYPOINT is run-time.
- B. ENTRYPOINT defines the executable command, while CMD specifies default arguments that can be easily overridden when running the container. β (correct answer)
- C. CMD cannot be overridden under any circumstances.
- D. ENTRYPOINT runs before the container filesystem is loaded.
Explanation: ENTRYPOINT configures the primary execution call. CMD parameters are appended to it and can be overridden via command-line arguments.
Question 4: When should you use a Docker Volume instead of a Bind Mount?
- A. When you want to link local development source code directories directly into the container.
- B. When you need Docker to manage persistent container storage completely, decoupling it from the specific host directory layout. β (correct answer)
- C. Volumes are faster but do not support data persistence.
- D. Volumes can only be mounted to database containers.
Explanation: Volumes are fully managed by Docker and isolated from host dependencies, making them portable and safe for backups.
Question 5: What is the primary benefit of utilizing Multi-Stage Builds in a Dockerfile?
- A. It allows the container to run on multiple operating systems simultaneously.
- B. It drastically reduces final image size by separating build tools (compiler, SDK) from the minimal production runtime environment. β (correct answer)
- C. It enables automated database schema migrations during compilation.
- D. It speeds up local CPU processing times during packaging.
Explanation: Multi-stage builds compile code in a builder image, copying only the compiled binaries to the final clean runtime image, leaving compile clutter behind.
Question 6: Which command lists all currently running Docker containers?
- A. docker list
- B. docker ps β (correct answer)
- C. docker containers
- D. docker show
Explanation: docker ps prints active containers. Adding -a lists both running and stopped containers.
Question 7: What does the -p flag do in 'docker run -p 8080:80 nginx'?
- A. Sets the priority of the container.
- B. Maps port 8080 on the host machine to port 80 inside the container. β (correct answer)
- C. Declares the container run mode as private.
- D. Allocates memory pools.
Explanation: Port mapping routes host network traffic into containerized daemon services.
Question 8: How does Docker utilize copy-on-write (CoW) in storage layers?
- A. It deletes parent layers when child layers are written.
- B. It permits containers to share image layers, copying files to the container's read-write layer only when modifications are made, saving storage. β (correct answer)
- C. It requires database transaction logs to execute writes.
- D. It encrypts file blocks during compilation.
Explanation: Copy-on-write maximizes disk efficiency, keeping base image layers read-only and immutable.
Question 9: Which file is used to declare and configure multi-container Docker applications?
- A. Dockerfile
- B. docker-compose.yml β (correct answer)
- C. settings.json
- D. package.json
Explanation: Docker Compose manages multiple containers, networks, and volumes declaratively.
Question 10: What is the purpose of the '.dockerignore' file?
- A. It lists packages that should not be upgraded.
- B. It specifies files and folders to exclude from the build context sent to the Docker daemon, speeding up builds and preventing credential leaks. β (correct answer)
- C. It blocks container internet access.
- D. It deletes temporary logs after container runs.
Explanation: Excluding files (like node_modules or .env) keeps the build context small and secure.
Question 11: What does running a container with the '--init' flag accomplish?
- A. It initializes database tables.
- B. It spawns a tiny init process (like tini) inside the container as PID 1, ensuring proper signal forwarding and reaping of zombie processes. β (correct answer)
- C. It resets container configurations.
- D. It starts the container in background daemon mode.
Explanation: Standard containers lack init systems, causing CMD processes to ignore SIGTERM signals and leak zombies.
Question 12: How do you build a Docker image named 'my-app' from a Dockerfile in the current directory?
- A. docker compile -t my-app
- B. docker build -t my-app . β (correct answer)
- C. docker create my-app
- D. docker make my-app
Explanation: docker build uses the dot (.) to locate the build context directory, compiling the image.
Question 13: What is the difference between 'docker stop' and 'docker kill'?
- A. docker stop runs synchronously, while docker kill is asynchronous.
- B. docker stop sends SIGTERM to allow graceful shutdowns, while docker kill sends SIGKILL to terminate the container instantly. β (correct answer)
- C. docker kill deletes the container files permanently.
- D. There is no difference.
Explanation: stop gives processes time to close connections and files; kill stops execution instantly.
Question 14: Why is running Docker containers as a non-root user recommended?
- A. It reduces memory usage on hosts.
- B. It mitigates container breakout attacks by restricting permissions if an attacker escapes container boundaries. β (correct answer)
- C. Non-root containers are faster.
- D. Standard registries reject root images.
Explanation: Securing containers via USER directives in Dockerfiles limits access permissions on hosts.
Question 15: Which directive in a Dockerfile defines the base image to build upon?
- A. BASE
- B. FROM β (correct answer)
- C. START
- D. RUN
Explanation: FROM initializes the build stage, pointing to parent images (e.g. node:18-alpine).
Question 16: What does the 'docker exec' command do?
- A. Exports container data to files.
- B. Runs a new command inside an already running container instance. β (correct answer)
- C. Deletes stopped container logs.
- D. Configures network routing.
Explanation: exec is standard for debugging (e.g. docker exec -it container_id sh).
Question 17: How do Docker networks (bridge vs host modes) differ?
- A. Bridge is encrypted, while Host is unencrypted.
- B. Bridge mode isolates container networks behind virtual bridges, while Host mode binds containers directly to the host's network interfaces. β (correct answer)
- C. Host mode is slower and deprecated.
- D. Bridge mode does not support port mapping.
Explanation: Host mode removes network virtualization overhead, sharing host IP ports directly.
Question 18: Which command removes all stopped containers, unused networks, and dangling build caches?
- A. docker clean
- B. docker system prune β (correct answer)
- C. docker delete --all
- D. docker reset
Explanation: system prune is standard for clearing system disks of unused container resources.
Question 19: What is the difference between a bind mount and a docker volume?
- A. Bind mounts are faster than volumes.
- B. Bind mounts link to any host directory path, while volumes are hosted inside a directory managed by Docker. β (correct answer)
- C. Volumes do not persist data.
- D. Bind mounts work only on Windows.
Explanation: Volumes are isolated from host file structures, making them clean and portable.
Question 20: What is the purpose of Docker Content Trust (DCT)?
- A. It compresses image payload bytes.
- B. It enforces digital signatures on images, verifying the integrity and publisher origin before downloading. β (correct answer)
- C. It logs container API calls.
- D. It whitelists firewall connections.
Explanation: DCT prevents tampered or spoofed container image downloads in production pipelines.
Question 21: What does the RUN instruction do in a Dockerfile?
- A. Starts the container daemon.
- B. Executes shell commands during the image build process, creating a new image layer. β (correct answer)
- C. Runs the main container application.
- D. Installs Docker desktop.
Explanation: RUN executes installation steps (e.g. apt-get install) during packaging.
Question 22: In Docker Compose, what does 'depends_on' declare?
- A. The environment variable dependencies.
- B. The startup order of services, ensuring dependency containers start before the dependent service. β (correct answer)
- C. The local database connection keys.
- D. The shared file volumes list.
Explanation: depends_on determines order but does not wait for databases to be 'ready' (healthcheck required).
Question 23: Why is chaining commands (e.g. apt-get update && apt-get install -y) inside a single RUN directive preferred?
- A. It speeds up local CPU execution.
- B. It reduces the number of generated image layers and prevents caching issues between update and install commands. β (correct answer)
- C. It automatically logs code errors.
- D. It encrypts package lists.
Explanation: Each RUN creates a layer. Chaining keeps images light and prevents stale package caches.
Question 24: Which command logs into a Docker registry (like Docker Hub)?
- A. docker auth
- B. docker login β (correct answer)
- C. docker sign
- D. docker connect
Explanation: docker login authenticates CLI contexts to push and pull private images.
Question 25: What does it mean if a Docker image is built using an 'Alpine' base?
- A. The image runs only on cloud servers.
- B. The image is built using Alpine Linux, a minimal, security-focused distro that yields extremely small image sizes. β (correct answer)
- C. The image is designed for machine learning.
- D. The image lacks a filesystem.
Explanation: Alpine base images (e.g. python:3.9-alpine) reduce deployment footprint, saving bandwidth.
Question 26: What is the role of the Docker Daemon (dockerd)?
- A. An interface for writing code files.
- B. The background service that manages Docker objects (images, containers, networks, volumes) and processes API requests. β (correct answer)
- C. A database clustering engine.
- D. A network proxy server.
Explanation: dockerd listens to CLI command API requests, executing container lifecycle operations.
Question 27: How do you run a container in the background (detached mode)?
- A. docker run -b
- B. docker run -d β (correct answer)
- C. docker run -bg
- D. docker run -async
Explanation: Detached mode (-d) runs containers in background loops, freeing terminals.
Question 28: What does the WORKDIR instruction do in a Dockerfile?
- A. Sets the path to the node compiler.
- B. Sets the working directory for subsequent instructions (RUN, CMD, ENTRYPOINT, COPY) inside the image. β (correct answer)
- C. Creates a new database directory.
- D. Restricts folder access rights.
Explanation: WORKDIR creates folders automatically if they don't exist, organizing application structures.
Question 29: What is the function of the Docker overlay network driver?
- A. It speeds up file reading times.
- B. It enables communication between containers running on different swarm or host machines without OS-level routing setups. β (correct answer)
- C. It compresses network payloads.
- D. It logs traffic endpoints.
Explanation: Overlay networks virtualize tunnels across hosts, crucial for Docker Swarm scaling.
Question 30: Which command stops all active containers?
- A. docker stop $(docker ps -a -q)
- B. docker stop $(docker ps -q) β (correct answer)
- C. docker delete --active
- D. docker reset
Explanation: docker ps -q yields active container IDs; passing them to stop terminates them.