Docker Practice Questions & Quiz

80 questions / 10 random questions

development commands design operations and security
Try a 10-question Docker quiz

Random questions, instant feedback, and review for missed questions.

Start quiz →

View recommended Docker resources →

Included topics (80 questions)

Q1

For a Node.js app, you want dependencies to be cached in the image while keeping rebuilds fast when source files change. What should the Dockerfile copy first?

Answer: package.json and the lock file

Copying dependency manifests first lets the dependency installation layer be cached. Source-only changes are less likely to force dependency installation again.

Q2

In a development environment, you want source changes on the host to appear immediately inside the container. What is the most natural approach?

Answer: Bind-mount the source directory

A bind mount exposes host files directly to the container, which is common for local development.

Q3

In a development Compose file, you want to access an app listening on port 3000 from host port 3000. Which setting is appropriate?

Answer: ports: ["3000:3000"]

ports publishes host ports to container ports. 3000:3000 maps host port 3000 to container port 3000.

Q4

An app inside a container cannot reach a database on the host by connecting to localhost. What is the best explanation?

Answer: localhost inside the container refers to the container itself

Containers have their own network namespace, so localhost inside the container means the container itself, not the host.

Q5

You want development and production to differ only in startup command and mounts. What Compose approach is appropriate?

Answer: Layer a development override file on top of the base Compose file

Compose can merge multiple files. Keeping shared definitions in a base file and overriding development-only commands or mounts is maintainable.

Q6

You want to reuse a dependency cache across builds without including it in the final image. Which BuildKit feature is appropriate?

Answer: RUN --mount=type=cache

BuildKit cache mounts can reuse package-manager caches across builds without directly baking them into image layers.

Q7

In a hot-reload development container, you want source files from the host but dependencies from inside the container. What setup is common?

Answer: Bind-mount source files and isolate the dependency directory with a named volume

A common pattern is to bind-mount source files while keeping OS-sensitive dependency directories such as node_modules in a named volume managed by the container.

Q8

When sharing a Dockerfile across a development team, which style should be avoided for stable OS package installation?

Answer: Putting apt-get upgrade -y in a normal application image build

Application images should install only required packages. Broad upgrades make builds less predictable and blur responsibility with base image maintenance.

Q9

You want environment-specific app settings while keeping the image common. What is appropriate?

Answer: Pass them at runtime as environment variables or external configuration

Keeping configuration outside the image and injecting it at runtime through environment variables, files, or secret management makes one image usable across environments.

Q10

For local development, you want to start multiple services together and let them communicate by service name. What fits best?

Answer: Docker Compose

Compose defines services, networks, volumes, and environment variables in a file and starts multiple containers together.

Q11

You want to list images stored locally. Which command should you use?

Answer: docker image ls

docker image ls lists local images and shows tags, image IDs, sizes, and related information.

Q12

You want to start a container in the background and automatically remove it when it stops. Which option combination is appropriate?

Answer: docker run -d --rm ...

-d runs detached, and --rm removes the container after it exits. This is useful for temporary test containers.

Q13

You want to list containers including stopped ones. Which command is appropriate?

Answer: docker ps -a

docker ps lists running containers; adding -a includes stopped containers.

Q14

You want to follow stdout logs from a running container. Which command should you use?

Answer: docker logs -f <container>

docker logs -f follows container logs. It works well when the application writes logs to stdout/stderr.

Q15

You want to open a temporary shell inside a running container for investigation. Which command is appropriate?

Answer: docker exec -it <container> sh

docker exec runs an additional command in a running container. -it enables interactive shell use.

Q16

You want to build an image named myapp:dev from a Dockerfile. Which command is appropriate?

Answer: docker build -t myapp:dev .

docker build -t name:tag context builds an image from a Dockerfile and assigns a tag.

Q17

You want to clean up stopped containers, unused networks, dangling images, and build cache. Which command is a suitable candidate?

Answer: docker system prune

docker system prune cleans unused resources. Review what will be removed so needed volumes or images are not deleted.

Q18

You want to start services defined in a Compose file in the background. Which command is appropriate?

Answer: docker compose up -d

docker compose up -d creates and starts Compose services in detached mode.

Q19

You want detailed JSON metadata for a container or image. Which command should you use?

Answer: docker inspect

docker inspect shows detailed JSON metadata such as IP addresses, mounts, environment variables, and settings.

Q20

You want to copy a configuration file from a container to the host. Which command is appropriate?

Answer: docker cp <container>:/path/file ./file

docker cp copies files between host and container, which is useful for retrieving configuration during investigation.

Q21

Which design best keeps a production image small and avoids including build tools?

Answer: Use a multi-stage build and copy only artifacts into the final stage

Multi-stage builds separate build and runtime stages, allowing the final image to contain only required artifacts.

Q22

Why should container images usually avoid packing too many independent long-running processes into one container?

Answer: It makes monitoring, restart, scaling, and log separation harder

Containers are easier to operate when responsibilities are focused. Too many responsibilities make failure isolation and scaling boundaries unclear.

Q23

You want production to reuse the exact intended image. Which tagging approach is safer?

Answer: Use version tags or image digests for deployment

Relying only on mutable latest can point to different content over time. Version tags or digests improve traceability.

Q24

What is the main purpose of maintaining .dockerignore before using COPY . . in a Dockerfile?

Answer: To exclude unnecessary or sensitive files from the build context

.dockerignore reduces files sent to the build context and helps prevent secrets or large unnecessary files from being included.

Q25

Why should database data usually not be stored only in a container writable layer?

Answer: Data can be lost when the container is removed or recreated

A container writable layer is tied to the container lifecycle. Database data should be persisted with volumes or external database services.

Q26

In a web app image, what does EXPOSE 8080 mean?

Answer: It documents as metadata that the container intends to listen on port 8080

EXPOSE is metadata about intended listening ports. Host publishing still requires docker run -p or Compose ports.

Q27

You tried to change production runtime settings using only Dockerfile ARG and it failed. What is the likely reason?

Answer: ARG is mainly a build-time variable; runtime settings use ENV or external injection

ARG is primarily used during build. Values that change at runtime should be passed through environment variables, configuration files, or secret management.

Q28

What is the benefit of copying frequently changed files later in a Dockerfile?

Answer: Stable earlier layer cache is easier to reuse

Dockerfile cache works by instruction order. Placing stable dependency steps earlier and frequently changed source copies later improves efficiency.

Q29

Why is writing app logs to stdout often preferred over writing only to files inside production containers?

Answer: Container runtimes and logging platforms can collect them more easily

Writing container logs to stdout/stderr makes integration with Docker and cloud log collectors easier.

Q30

Which design helps avoid leaving build-time secrets in an image?

Answer: Use mechanisms such as BuildKit secret mounts that avoid persisting secrets in layers

Secrets handled with Dockerfile ENV or COPY can remain in layer history. BuildKit secret mounts reduce exposure.

Q31

In standalone Docker, you want a container to restart automatically after a failure. Which setting is appropriate?

Answer: A restart policy such as --restart=unless-stopped

A restart policy tells the Docker daemon how to handle container exits. It serves a different purpose from --rm.

Q32

You want to view container CPU and memory usage in real time. Which command is appropriate?

Answer: docker stats

docker stats displays CPU, memory, network, and block I/O usage per container.

Q33

For production operations, what should an image provide to make container health easier to assess?

Answer: HEALTHCHECK or an easy-to-monitor health endpoint

Health checks help determine not only whether a process exists, but whether the application can respond correctly.

Q34

You want to inspect a running container's configuration, mounts, and networks. Which command is a good first choice?

Answer: docker inspect <container>

docker inspect returns detailed container information as JSON, making it useful for checking mounts, networks, and environment variables.

Q35

The host disk is running low and you want a breakdown of Docker disk usage. Which command should you use?

Answer: docker system df

docker system df shows disk usage summaries for images, containers, volumes, and build cache.

Q36

For production-like testing, you want to set a container memory limit in standalone Docker. Which option is representative?

Answer: docker run --memory 512m ...

--memory is a common option for limiting container memory usage.

Q37

Why is manually fixing a production container by entering it usually not a good root-cause fix?

Answer: The change is not reproducible and can be lost on recreation or redeploy

Containers are expected to be recreated. Fixes should be reflected in Dockerfiles, configuration, or deployment definitions so they are reproducible.

Q38

You want to view logs across services managed by Compose. Which command is appropriate?

Answer: docker compose logs

docker compose logs displays logs for services in a Compose project. -f follows them.

Q39

You need to transfer an image to another environment as a file, without using a registry. Which pair is appropriate?

Answer: docker save and docker load

docker save stores an image as a tar archive, and docker load loads it back while preserving tags and layers.

Q40

Before deleting unused Docker resources in operations, what deserves special attention?

Answer: Whether named volumes contain required persistent data

Volumes may contain important data such as databases or uploaded files. Confirm purpose and backups before deletion.

Q41

Which Dockerfile instruction is commonly used to avoid running the app as root?

Answer: USER

USER sets the user for later instructions and runtime processes. Running as non-root is a basic privilege reduction measure.

Q42

You need a private key during build to access a private repository, but you do not want it left in the image. What is appropriate?

Answer: Use BuildKit SSH mounts or secret mounts

BuildKit SSH and secret mounts expose secrets only during build and reduce the risk of leaving them in layers.

Q43

To reduce image vulnerability risk, what should you consider when choosing a base image?

Answer: Use a minimal, maintained, trusted base image

A small, maintained, trusted base helps reduce unnecessary packages and known vulnerabilities.

Q44

What is the main risk of mounting the host Docker socket /var/run/docker.sock into a container?

Answer: The container may gain powerful control over Docker on the host

The Docker socket controls the host Docker daemon. Mounting it can provide near host-level power and must be handled carefully.

Q45

What is the purpose of avoiding unnecessary Linux capabilities in a container?

Answer: To reduce privileges available if the container is compromised

Capabilities split root privileges into smaller units. Dropping unnecessary ones can limit damage after compromise.

Q46

You want to reduce the risk of filesystem tampering inside a container. Which runtime setting is a candidate?

Answer: --read-only

--read-only makes the container root filesystem read-only. Required writable paths should be explicitly provided with tmpfs or volumes.

Q47

When handling Docker images in CI, which step should be added for vulnerability checking?

Answer: Run image scanning and gate releases based on severity

Image scanning in CI helps detect known vulnerabilities in base images and dependencies before release.

Q48

Which approach should be avoided when passing secrets to containers?

Answer: Writing secrets directly into the Dockerfile

Secrets in Dockerfiles can leak through image history or registries. Runtime secret management is the usual approach.

Q49

Why should --privileged not be casually used for production containers?

Answer: It grants powerful near-host privileges and weakens isolation

--privileged grants many permissions and device access. Prefer allowing only required capabilities or devices.

Q50

Which option is known for running Docker Engine without root privileges to reduce impact if the daemon is compromised?

Answer: Rootless mode

Rootless mode runs the Docker daemon and containers as a non-root user. It has limitations and should be evaluated against requirements.

Q51

In a development Compose setup, you want source changes to appear immediately, but you do not want the dependency directory created in the container to be overwritten by the host. Which approach is appropriate?

Answer: Bind-mount the source and assign a separate volume to the dependency directory

A bind mount is useful for live source edits. A separate volume for dependency directories helps avoid hiding container-installed dependencies with an empty or different host directory.

Q52

You want to check the exit code of a recently exited container, including stopped containers. Which command is appropriate to start with?

Answer: docker ps -a

docker ps -a lists containers including stopped ones, and the STATUS column can show exited state and exit code. Use docker inspect when deeper details are needed.

Q53

For a production image, you need compilers and development tools during build, but do not want them left in the final image. Which Dockerfile design is appropriate?

Answer: Use a multi-stage build and copy only build artifacts into the final stage

A multi-stage build keeps build tools in a builder stage and copies only artifacts into the final stage. This helps keep the runtime image smaller and reduces unnecessary attack surface.

Q54

In production, you want container logs to be collected by a centralized logging system. What is an appropriate basic application-side policy?

Answer: Write logs to stdout and stderr, and let the runtime platform collect them

Writing logs to stdout and stderr makes it easier for Docker, orchestrators, and logging drivers to collect them. Keeping logs only inside container files can cause loss or missed collection.

Q55

To make image sources easier to trust, CI should pin the base image reference more reproducibly. What is better than relying only on a tag?

Answer: Pin the reference by image digest

A tag can later point to a different image. Pinning by digest identifies the image content more precisely and improves reproducibility and traceability.

Q56

Many images have been pulled locally. You first want to list repository names, tags, image IDs, and sizes. Which command is appropriate?

Answer: docker image ls

docker image ls lists images present locally. It is a good first check for pulled image tags, IDs, creation time, and size.

Q57

After repeated builds and pulls, dangling images shown as <none> remain. You want to first clean only unreferenced dangling images without removing images needed by containers. Which operation is appropriate?

Answer: Run docker image prune and review the deletion target before proceeding

docker image prune removes dangling images by default. Broader prune options such as -a or volume deletion have larger impact, so choose them only after checking the target.

Q58

You pulled nginx:1.27 locally and want to also refer to it as web-proxy:test for internal testing. Which operation adds another reference name without duplicating the image content?

Answer: docker tag nginx:1.27 web-proxy:test

docker tag SOURCE_IMAGE TARGET_IMAGE assigns another repository name or tag to an existing image. It does not duplicate the image content; it adds another reference to the same image ID.

Q59

You want to confirm whether a locally pulled image matches the expected digest. Which command is appropriate for checking detailed metadata such as RepoDigests?

Answer: docker image inspect <image>

docker image inspect outputs detailed local image metadata as JSON. It can show RepoDigests, IDs, labels, and creation details, which helps trace pulled images.

Q60

You need to move a locally pulled tagged image into an offline test environment as a file. Which pair preserves tags and layers for transport?

Answer: Save with docker save, then load with docker load at the destination

For offline image transport, use docker save to create a tar archive and docker load at the destination. export/import targets a container filesystem and serves a different purpose from preserving image tags and layers.

Q61

You want to explain the relationship between an image and a container to a teammate new to Docker. Which explanation is most appropriate?

Answer: An image is a template before execution, and a container is a runtime instance started from that image

A Docker image is a read-only template containing an application and runtime environment. A container is created from that image and runs as a process-oriented runtime unit.

Q62

You want to start a container from redis:7, which is not present locally. What is the normal Docker flow?

Answer: If the image is not local, Docker pulls it from a registry, then creates and starts a container

When a command such as docker run redis:7 references an image that is not local, Docker pulls it from the configured registry and then creates and starts the container.

Q63

You want to access a web server listening on port 80 inside a container from the host browser at http://localhost:8080. Which port publishing option is appropriate?

Answer: -p 8080:80

The order is -p host_port:container_port. To forward host port 8080 to container port 80, use -p 8080:80.

Q64

You want database data to remain even if the container is removed. Which Docker-managed storage resource is generally used?

Answer: named volume

For data such as database files that should outlive the container lifecycle, a Docker-managed named volume is commonly used.

Q65

You want a temporary test container to be automatically removed when it exits. Which run option avoids leaving a stopped container behind?

Answer: --rm

docker run --rm ... automatically removes the container when it exits. It is useful for short tests or one-shot commands where you do not want stopped containers left behind.

Q66

In Compose, an API must start only after the database is ready to accept connections. Which setting waits for a successful health check rather than only container startup?

Answer: Set the database dependency to condition: service_healthy under depends_on

Define a healthcheck on the database and use condition: service_healthy in the API's depends_on; Compose then waits for the database to become healthy.

Q67

An application takes 40 seconds to initialize, but its health check runs every 5 seconds and records early failures. Which field defines an explicit startup grace period?

Answer: start_period

start_period provides initialization time before normal consecutive-failure handling applies, reducing false unhealthy states for slow-starting services.

Q68

On a single Docker host, growing json-file logs are filling the disk. Which container setting commonly bounds log usage?

Answer: --log-opt max-size=10m --log-opt max-file=3

For the json-file logging driver, max-size and max-file control rotation size and retained files. Apply the settings when creating or recreating the container.

Q69

You want to build amd64 and arm64 images from one Dockerfile and publish them under one tag. Which command approach is appropriate?

Answer: docker buildx build --platform linux/amd64,linux/arm64 --push ...

buildx build with multiple --platform values and --push publishes per-architecture images plus a manifest that groups them under one tag.

Q70

You need SIGTERM to reach the application correctly so it can shut down within the grace period. Which Dockerfile startup form is appropriate?

Answer: The exec form ENTRYPOINT ["myapp", "--serve"]

The exec form avoids an unnecessary shell and normally runs the application as PID 1, allowing it to receive Docker stop signals directly. The app must also handle SIGTERM.

Q71

An application spawns child processes but does not reap exited children, leaving zombies. Which runtime option is an appropriate mitigation while the app is being fixed?

Answer: docker run --init ...

--init inserts a small init process as PID 1 to help forward signals and reap orphaned or zombie processes. Application process management should still be corrected.

Q72

In Compose, API and database services should start normally, while a debug admin UI should start only when requested. Which feature fits?

Answer: Assign profiles: ["debug"] to the admin UI service

Compose profiles let you exclude selected services from normal startup and enable them only when a profile such as --profile debug is requested.

Q73

A web process must listen on port 80 without receiving unnecessary Linux capabilities. Which option best follows least privilege?

Answer: --cap-drop ALL --cap-add NET_BIND_SERVICE

Dropping all capabilities and adding back only NET_BIND_SERVICE can satisfy low-port binding while limiting privileges. Verify whether the app or base image needs anything else.

Q74

You want to prevent processes in a container from gaining additional privileges through mechanisms such as setuid binaries. Which security option is appropriate?

Answer: --security-opt no-new-privileges=true

no-new-privileges prevents a process and its children from gaining more privileges than they already have. Combine it with non-root execution and capability reduction.

Q75

Before startup, you want to inspect the final configuration produced by merging multiple Compose files and environment variables. Which command is appropriate?

Answer: docker compose config

docker compose config renders the Compose model after file merging, variable interpolation, and normalization, making it useful for pre-start validation.

Q76

A Compose database network should be reachable only by attached application services and have no direct external connectivity. Which network setting is appropriate?

Answer: internal: true

internal: true creates an externally isolated Compose network. Attach only required services and avoid publishing the database port to the host.

Q77

You want copied files owned by a non-root user without adding a separate RUN chown layer. Which Dockerfile instruction is appropriate?

Answer: COPY --chown=app:app ./dist /app

COPY --chown sets ownership while files are copied into the image, avoiding a later RUN chown and making non-root ownership explicit.

Q78

A package installed manually in a running container disappeared at the next deployment. What is the appropriate permanent fix?

Answer: Put the change in the Dockerfile, build a new image, and recreate the container

Treat containers as replaceable runtime units and encode package or application changes in the Dockerfile and build process. This makes the environment reproducible and reviewable.

Q79

You plan to back up a database named volume by copying files. What is the best basic policy for avoiding an inconsistent backup during writes?

Answer: Stop or quiesce the database, or use its supported backup mechanism

Copying database files during writes can capture different files at inconsistent points in time. Stop or quiesce writes, coordinate snapshots, or use the database's supported logical or physical backup tools.

Q80

A production container needs a read-only root filesystem while allowing temporary writes only under /tmp. Which runtime options are appropriate?

Answer: --read-only --tmpfs /tmp

Protect the root filesystem with --read-only and explicitly provide tmpfs only for /tmp. Use volumes for paths that require persistence.

certdrill.dev is an independent, unofficial learning site and is not affiliated with LPI Japan, IPA, AWS, Microsoft Azure, or any exam provider. Questions and explanations are original content.