Docker Containerization Dockerfile and Image Building 1 — Questions and Answers
Question 1: Which Dockerfile instruction specifies the base image for a new build?
- BASE
- FROM (Correct answer)
- IMAGE
- SOURCE
Correct answer: FROM
The FROM instruction is mandatory and must be the first instruction in a Dockerfile; it sets the base image from which you are building.
Question 2: What is the key difference between the ADD and COPY instructions in a Dockerfile?
- COPY supports remote URLs; ADD only handles local files
- ADD supports remote URLs and auto-extracts tar archives; COPY only copies local files as-is (Correct answer)
- ADD and COPY are functionally identical
- COPY auto-extracts archives; ADD requires manual extraction
Correct answer: ADD supports remote URLs and auto-extracts tar archives; COPY only copies local files as-is
ADD has additional functionality: it can fetch files from remote URLs and automatically extract compressed archives like .tar.gz, while COPY is a simpler, more transparent instruction for local files.
Question 3: Which Dockerfile instruction sets the working directory for all subsequent RUN, CMD, ENTRYPOINT, COPY, and ADD instructions?
- DIR
- PATH
- WORKDIR (Correct answer)
- CD
Correct answer: WORKDIR
WORKDIR sets the working directory inside the container, and if the directory does not exist it will be created automatically.
Question 4: What is the purpose of a .dockerignore file?
- To prevent Docker from pulling certain base images
- To specify files and directories to exclude from the build context sent to the Docker daemon (Correct answer)
- To ignore syntax errors in the Dockerfile
- To disable specific Docker daemon features during build
Correct answer: To specify files and directories to exclude from the build context sent to the Docker daemon
A .dockerignore file lists patterns of files and directories to exclude from the build context, which reduces build time and prevents sensitive files from being inadvertently added to the image.
Question 5: Which Dockerfile instruction sets a persistent environment variable that is available both during the build and when the container runs?
- SET
- VAR
- ENV (Correct answer)
- ARG
Correct answer: ENV
ENV sets environment variables that persist in the final image and are accessible to running containers, unlike ARG which is only available at build time.
Question 6: What does the EXPOSE instruction in a Dockerfile actually do?
- It automatically publishes the port to the host machine
- It blocks all ports except the ones specified
- It documents which network ports the container intends to listen on at runtime (Correct answer)
- It configures the container's firewall rules
Correct answer: It documents which network ports the container intends to listen on at runtime
EXPOSE is documentation only; it informs Docker and users which ports the application listens on, but does not actually publish or open the port — that requires -p or -P flags at runtime.
Question 7: Which Dockerfile instruction defines the default command to run when a container starts, and can be overridden by arguments passed to 'docker run'?
- RUN
- CMD (Correct answer)
- EXECUTE
- START
Correct answer: CMD
CMD provides default arguments for container execution; if the user provides arguments to 'docker run', the CMD defaults are overridden, unlike ENTRYPOINT which is harder to override.
Which Dockerfile instruction specifies the base image for a new build?