Docker Questions with Answers

9 practice questions on Docker (Git & DevOps) with answers and step-by-step solutions. Try 9 samples below, then practise all 9 free in the AptiNest app.

Sample questions

Question 1Easy

What is the difference between RUN and CMD in a Dockerfile?

  1. CMD executes at build time; RUN sets the default start command
  2. RUN is only for Windows images, and CMD only for Linux images
  3. They are interchangeable; both run at build time and at start-up
  4. 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

  1. RUN executes during docker build and creates an image layer (e.g. installing packages).
  2. CMD records the default command run when a container starts.
  3. 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?

  1. Maps host port 8080 to container port 80
  2. Maps container port 8080 to host port 80
  3. Limits the container to 80 MB of memory
  4. 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

  1. -p uses the format HOST:CONTAINER.
  2. 8080:80 maps host port 8080 to container port 80.
  3. 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?

  1. docker ps -a
  2. docker images
  3. docker ps
  4. docker list --all

Answer: A. docker ps -a

Explanation

docker ps shows only running containers. -a (--all) includes stopped ones.

Step-by-step solution

  1. docker ps lists running containers.
  2. -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?

  1. FROM
  2. IMAGE
  3. USE
  4. BASE

Answer: A. FROM

Explanation

A Dockerfile normally starts with FROM <image>, for example FROM python:3.12-slim.

Step-by-step solution

  1. Every Dockerfile build starts from a base image.
  2. The FROM instruction names it, e.g. FROM python:3.12-slim.
  3. 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?

  1. Images run only on Linux hosts, containers only on Windows hosts
  2. A container is the template, and an image is a running instance
  3. An image is a read-only template; a container is an instance of it
  4. 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

  1. An image is a read-only template: files, libraries and settings.
  2. A container is a running (or stopped) instance created from it.
  3. 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?

  1. To avoid installing devDependencies in the final image
  2. It makes the final container start noticeably faster
  3. npm refuses to run unless package.json is copied first
  4. 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

  1. Each Dockerfile step is a cached layer, reused if its inputs are unchanged.
  2. package.json changes rarely; source changes often.
  3. 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?

  1. Store the data in a volume mounted into the container
  2. It is not possible; container data is always temporary
  3. Run docker commit after every write to save the data
  4. 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

  1. A container's writable layer is deleted with the container.
  2. A volume lives outside that lifecycle.
  3. 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?

  1. Build tools stay in an earlier stage; only output is copied
  2. It runs the stages in parallel so containers start faster
  3. It runs several containers at once from a single image
  4. 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

  1. The first stage has compilers and build tools.
  2. It builds the application.
  3. 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?

  1. python app.py --port 8000
  2. --port 9000
  3. python app.py --port 9000
  4. python 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

  1. ENTRYPOINT is fixed: python app.py.
  2. CMD supplies default arguments: --port 8000.
  3. 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 →

Related topics

See all Git & DevOps topics · All topics