Sample questions
Question 1Easy
What is the difference between RUN and CMD in a Dockerfile?
- CMD executes at build time; RUN sets the default start command
- RUN is only for Windows images, and CMD only for Linux images
- They are interchangeable; both run at build time and at start-up
- RUN executes at build time; CMD sets the default start command
Answer: D. RUN executes at build time; CMD sets the default start command
Explanation
RUN creates a new image layer (for example, installing packages). CMD only records a default command, which arguments to docker run can override.
Step-by-step solution
- RUN executes during docker build and creates an image layer (e.g. installing packages).
- CMD records the default command run when a container starts.
- CMD can be overridden by arguments to docker run.
Why the other options are wrong
- A. That reverses them.
- B. Both work on any platform.
- C. They run at different times.
Tip: RUN at build, CMD at start.
Question 2Easy
What does docker run -p 8080:80 nginx do with ports?
- Maps host port 8080 to container port 80
- Maps container port 8080 to host port 80
- Limits the container to 80 MB of memory
- Opens both ports inside the container only
Answer: A. Maps host port 8080 to container port 80
Explanation
The format is -p HOST:CONTAINER, so requests to localhost:8080 on the host reach port 80 inside the container.
Step-by-step solution
- -p uses the format HOST:CONTAINER.
- 8080:80 maps host port 8080 to container port 80.
- So localhost:8080 reaches nginx on port 80.
Why the other options are wrong
- B. That reverses the order.
- C. Memory limits use -m.
- D. -p publishes ports to the host.
Tip: -p outside:inside.
Question 3Easy
Which command lists all containers, including stopped ones?
docker ps -adocker imagesdocker psdocker list --all
Answer: A. docker ps -a
Explanation
docker ps shows only running containers. -a (--all) includes stopped ones.
Step-by-step solution
- docker ps lists running containers.
- -a (--all) includes stopped ones.
Why the other options are wrong
- B. docker images lists images, not containers.
- C. Without -a only running containers are shown.
- D. docker list is not a Docker command.
Tip: ps = running; ps -a = all.
Question 4Easy
Which Dockerfile instruction sets the base image?
- FROM
- IMAGE
- USE
- BASE
Answer: A. FROM
Explanation
A Dockerfile normally starts with FROM <image>, for example FROM python:3.12-slim.
Step-by-step solution
- Every Dockerfile build starts from a base image.
- The FROM instruction names it, e.g. FROM python:3.12-slim.
- Later instructions add layers on top.
Why the other options are wrong
- B. IMAGE is not a Dockerfile instruction.
- C. USE is not a Dockerfile instruction.
- D. BASE is not a Dockerfile instruction.
Tip: Dockerfiles start FROM something.
Question 5Easy
What is the difference between a Docker image and a container?
- Images run only on Linux hosts, containers only on Windows hosts
- A container is the template, and an image is a running instance
- An image is a read-only template; a container is an instance of it
- They are the same thing; the terms are used interchangeably
Answer: C. An image is a read-only template; a container is an instance of it
Explanation
You can start many containers from one image. Each container adds its own writable layer on top of the image.
Step-by-step solution
- An image is a read-only template: files, libraries and settings.
- A container is a running (or stopped) instance created from it.
- Each container adds its own writable layer.
Why the other options are wrong
- A. Both run on many operating systems.
- B. That reverses the terms.
- D. They are distinct concepts.
Tip: Image = blueprint; container = running copy.
Question 6Medium
Why do Node.js Dockerfiles usually copy package.json and run npm install before copying the rest of the source code?
- To avoid installing devDependencies in the final image
- It makes the final container start noticeably faster
- npm refuses to run unless package.json is copied first
- So the dependency layer is cached when only source changes
Answer: D. So the dependency layer is cached when only source changes
Explanation
Docker reuses a cached layer as long as its inputs are unchanged. Dependencies change less often than source code, so this ordering avoids reinstalling them on every build.
Step-by-step solution
- Each Dockerfile step is a cached layer, reused if its inputs are unchanged.
- package.json changes rarely; source changes often.
- Copying it and installing first lets that layer be cached when only source changes.
Why the other options are wrong
- A. Excluding devDependencies uses npm ci --omit=dev, not ordering.
- B. Order affects build time, not container start time.
- C. npm does not require this order.
Tip: Order Dockerfile steps from least to most frequently changed.
Question 7Medium
A database runs in a container. How do you keep its data when the container is removed and recreated?
- Store the data in a volume mounted into the container
- It is not possible; container data is always temporary
- Run docker commit after every write to save the data
- Use a larger base image so there is room for the data
Answer: A. Store the data in a volume mounted into the container
Explanation
A container's writable layer is deleted with the container. Volumes live outside that lifecycle.
Step-by-step solution
- A container's writable layer is deleted with the container.
- A volume lives outside that lifecycle.
- Mount the volume at the database's data path to keep the data.
Why the other options are wrong
- B. Volumes make persistence possible.
- C. docker commit is for images, not live data.
- D. Image size does not persist data.
Tip: Data that must survive: put it in a volume.
Question 8Hard
What is the main benefit of a multi-stage Docker build?
- Build tools stay in an earlier stage; only output is copied
- It runs the stages in parallel so containers start faster
- It runs several containers at once from a single image
- It builds the image for several CPU architectures at once
Answer: A. Build tools stay in an earlier stage; only output is copied
Explanation
With several FROM stages, COPY --from=build copies only the artifacts into the final stage, so compilers and source code are not shipped.
Step-by-step solution
- The first stage has compilers and build tools.
- It builds the application.
- The final stage copies only the output (
COPY --from=build), giving a smaller, safer image.
Why the other options are wrong
- B. Stages are about content, not start-up speed.
- C. One image still runs one container per docker run.
- D. Multi-architecture builds use buildx platforms.
Tip: Build in a fat stage, ship a thin one.
Question 9Hard
A Dockerfile has ENTRYPOINT ["python", "app.py"] and CMD ["--port", "8000"]. What runs for docker run myimg --port 9000?
python app.py --port 8000--port 9000python app.py --port 9000python app.py --port 8000 --port 9000
Answer: C. python app.py --port 9000
Explanation
With the exec form, CMD supplies default arguments to ENTRYPOINT. Arguments given to docker run replace CMD but not ENTRYPOINT.
Step-by-step solution
- ENTRYPOINT is fixed: python app.py.
- CMD supplies default arguments: --port 8000.
- Arguments to docker run replace CMD, giving python app.py --port 9000.
Why the other options are wrong
- A. The run arguments replace CMD's default.
- B. ENTRYPOINT is not replaced by run arguments.
- D. CMD is replaced, not appended to.
Tip: ENTRYPOINT stays; CMD gets replaced.
Practise all 9 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →