Unit 2 - Practice Quiz
1 Which Dockerfile instruction is used to set the base image for subsequent instructions?
2
Which instruction sets the working directory for any RUN, CMD, ENTRYPOINT, COPY, and ADD instructions that follow it in the Dockerfile?
3 How do you define environment variables inside a Dockerfile?
4 Which file is used to exclude files and directories from being sent to the Docker daemon as part of the build context?
5 What is the primary command used to build a Docker image from a Dockerfile?
6
In Docker, what is created every time a RUN, COPY, or ADD instruction is executed in a Dockerfile?
7
Which flag is used with the docker build command to name and optionally tag an image?
8 Which command allows you to see the intermediate layers and creation steps of a Docker image?
9 What is the default network driver for newly created standalone Docker containers?
10 Which Docker network driver is primarily used to connect multiple Docker daemons together, allowing swarm services to communicate?
11
Which flag is used in the docker run command to map a port on the host machine to a port inside the container?
12 Which Docker storage option is completely managed by Docker and is the preferred way to persist data?
13 Which storage mechanism relies on the directory structure of the host machine by mounting a specific host path into a container?
14 What strategy does Docker use to share image layers and maximize storage efficiency when running containers?
15 What is the name of the default public registry where Docker looks for images if no custom registry URL is provided?
16 Which command is used to authenticate and log in to a Docker registry?
17 Which Dockerfile instruction functions as documentation to inform users about the network ports the container will listen on at runtime?
18
What is the primary difference between the COPY and ADD instructions in a Dockerfile?
COPY can only copy from URLs, while ADD copies local files.
ADD creates a new volume, while COPY does not.
ADD can extract local tar files and fetch remote URLs, while COPY only copies local files.
19 How do containers running on the same custom user-defined bridge network resolve each other's IP addresses?
/etc/hosts file
20 Which Dockerfile instruction is typically used to provide default arguments or the default executable for a running container?
21
In a Dockerfile, both COPY and ADD can be used to add files to an image. In which of the following scenarios is ADD specifically required over COPY?
--chown flag) during the transfer.
22
A Dockerfile contains ENTRYPOINT ["python", "app.py"] and CMD ["--help"]. What happens if you run the container using the command docker run my-image --port 8080?
python app.py --port 8080, overriding the default CMD.
python app.py --help --port 8080.
--port 8080 and ignores the ENTRYPOINT entirely.
ENTRYPOINT and CMD cannot be used together.
23
You are building a Docker image and notice the build process is sending 2GB of data to the Docker daemon, slowing it down. The large files are in a local ./data folder that is not needed inside the image. What is the most efficient way to resolve this?
docker build --ignore ./data . to exclude the directory.
COPY ./data /app/data instruction from the Dockerfile.
./data directory as a volume at runtime instead of copying it.
data/ to a .dockerignore file in the build context directory.
24 A developer is writing code locally and wants the changes to reflect immediately inside a running container without rebuilding the image. Which storage mechanism is most appropriate?
25
You want to run a web server container and map the host's port $8080$ to the container's port $80$. Which of the following docker run flags correctly achieves this?
--expose 8080:80
-P 8080:80
-p 80:8080
-p 8080:80
26 In Docker's storage architecture, what happens when a running container needs to modify an existing file that originated from the underlying read-only image layers?
27
To optimize a Dockerfile and reduce the final image size, a developer changes three separate RUN instructions (e.g., apt-get update, apt-get install, rm -rf /var/lib/apt/lists/*) into a single RUN instruction chained with &&. Why does this reduce the image size?
.dockerignore file processing.
28
After successfully running docker login to authenticate with a private registry, where does Docker by default store the authentication credentials or tokens on a Linux host?
/var/lib/docker/credentials
~/.ssh/docker_rsa
~/.docker/config.json
/etc/docker/daemon.json
29 Which Docker network driver is best suited for communication between standalone containers running on different Docker daemon hosts within a Swarm cluster?
30
Consider the following Dockerfile snippet:
dockerfile
WORKDIR /app
WORKDIR src
WORKDIR bin
RUN pwd
What will be the output of the RUN pwd command during the build process?
/bin
/src/bin
/app/src/bin
/app/bin
31 You want to analyze an existing Docker image to see the exact commands that were used to build it, layer by layer, along with the size of each layer. Which command should you use?
docker image history <image>
docker logs <image>
docker inspect <image>
docker ps -a --layers
32
Two containers, web and db, are attached to the default bridge network. The web container tries to ping the db container using its name (ping db). What will happen and why?
33 When tagging an image to push to the GitHub Container Registry (GHCR), what is the correct prefix format required for the repository name?
ghcr.io/<username>/<image>:<tag>
github.com/<username>/<image>:<tag>
registry.github.com/<username>/<image>:<tag>
docker.pkg.github.com/<username>/<image>:<tag>
34
You have an image with the ID a1b2c3d4 tagged as myapp:v1. You execute docker tag myapp:v1 myapp:latest. What is the result regarding the image layers and storage?
myapp:v1 tag is renamed to myapp:latest, removing the original v1 tag.
35
What is the primary difference between the ARG and ENV instructions in a Dockerfile?
ARG variables are only available during the build process, whereas ENV variables persist in the final image and are available at runtime.
ARG variables are available during both build time and container runtime, while ENV variables are only available at runtime.
ARG defines a command to run at startup, while ENV defines network ports.
36
You need to back up the data stored in a Docker named volume called db_data. Which of the following approaches is standard practice for backing up a Docker volume?
docker volume backup db_data > backup.tar.
/var/lib/docker/volumes/db_data directory directly while the database container is actively writing to it.
docker export db_container > db_data.tar.
db_data into it, and use tar to archive the contents to a bind-mounted host directory.
37
A developer includes the instruction EXPOSE 80 in their Dockerfile. What is the immediate effect of this instruction when a container is run from this image without any port mapping flags?
38
If a container is started with the flag --network host, what happens to any port mapping instructions (like -p 8080:80) passed in the docker run command?
39
When setting up a private Docker registry internally, a company uses the official registry:2 Docker image. If they do not configure TLS (HTTPS), what step must client machines take to push or pull images from this registry?
insecure-registries list.
--force-http flag with every docker push and docker pull command.
40
Why is running the command docker build -t myapp:1.0 . from the root directory of a host machine (i.e., /) considered highly dangerous and inefficient?
root user permanently.
41
A Dockerfile contains the following two instructions:
ENTRYPOINT echo "Hello"
CMD ["World"]
What will be the output when a container is run from this image without any additional command-line arguments?
42
You execute docker build -t myapp . in a directory containing a 10GB file named data.bin. The .dockerignore file contains the line data.bin. The Dockerfile contains the instruction COPY . /app. Which of the following statements is true regarding the build process?
.dockerignore cannot be present in the directory where docker build is executed.
/app directory.
/app, because COPY . overrides the .dockerignore file.
43
Consider the following Dockerfile snippet:
dockerfile
VOLUME /data
RUN echo "test" > /data/test.txt
What will be the state of /data/test.txt when a container is started from the resulting image?
/data directory.
RUN executes during the build phase.
44
You mount an empty host directory to a container directory using a bind mount (-v /empty/host/dir:/app/data), and you mount an empty Docker volume to another container directory (-v my_empty_volume:/app/config). Both /app/data and /app/config already contain files in the underlying container image. What happens to the pre-existing files in both container directories?
/app/data and /app/config are hidden and replaced by the empty host directory and volume.
/app/data are hidden. Files in /app/config are copied into my_empty_volume and remain visible.
/app/config are hidden. Files in /app/data are copied to the host directory and remain visible.
45
Why can containers attached to a user-defined bridge network resolve each other by container name, while containers on the default bridge network cannot?
.local to container names, which the host DNS daemon resolves.
/etc/hosts mapping via the --link flag.
46
A container is running from an image where Layer A (bottom) has file.txt (10MB) and Layer B (top) deletes file.txt. During runtime, a process in the container creates a new file.txt (5MB). How does the Union File System handle the storage size on disk for this specific sequence?
47
Consider a Node.js application Dockerfile. Which sequence of instructions optimally utilizes Docker's layer caching to prevent reinstalling dependencies unless package.json changes?
COPY src/ /app/src/COPY package*.json /app/WORKDIR /appRUN npm install
COPY . /appWORKDIR /appRUN npm install
WORKDIR /appCOPY package*.json ./RUN npm installCOPY . .
WORKDIR /appRUN npm installCOPY package*.json ./COPY . .
48
In a multi-stage Docker build, you have three stages: builder, tester, and the final stage. You want to copy a binary from the builder stage, but you forgot to name the stage using AS builder. The builder is the first FROM instruction. How can you validly copy the binary into the final stage?
COPY --from=builder /app/bin /usr/local/bin (Docker auto-infers names based on image types)
COPY --from=1 /app/bin /usr/local/bin
COPY instructions.
COPY --from=0 /app/bin /usr/local/bin
49
When a developer executes docker login to a private registry on a Linux system without configuring an external credential helper, how are the credentials stored by default?
/etc/docker/daemon.json.
DOCKER_PASS.
~/.docker/config.json as a base64 encoded string, which provides no cryptographic security.
~/.docker/credentials.db.
50 What is the precise definition of a 'dangling image' in Docker, and how is it typically created?
<none>:<none>); created when a new image is built and overwrites an existing tag.
51 You are deploying a container on a Docker Swarm using an overlay network. You need to ensure that the data-plane traffic (payload) between containers across different nodes is encrypted. How is this achieved?
--opt encrypted flag when creating the overlay network, which implements IPSec tunneling.
tlsverify option and providing SSL certificates.
52
A container runs a database listening on port 5432. You start it with docker run -p 5432:5432 my-db. You notice the database is accessible from the public internet. What is the most secure way to modify the -p flag so it is only accessible from the host machine itself?
-p localhost:5432
-p 5432:5432/tcp --internal
-p 0.0.0.0:5432:5432
-p 127.0.0.1:5432:5432
53
What is a primary difference between the COPY and ADD instructions regarding remote URLs and compressed archives?
ADD can download from remote URLs, but will only auto-extract local tar archives. COPY only works with local files and does not extract.
ADD can download from remote URLs and will automatically extract remote tar archives. COPY cannot.
COPY can download from remote URLs, but ADD cannot. Neither can auto-extract archives.
COPY and ADD behave identically, but ADD is deprecated in multi-stage builds.
54 You are writing a Dockerfile and need to pass a variable that is available during the build process but should NOT persist in the final image as an environment variable to prevent leaking sensitive build tokens. Which instruction should you use?
ARG
SET
ENV
EXPORT
55
When inspecting an image with docker image history <image>, you notice several layers have <missing> listed under the IMAGE ID column. What does this indicate?
ENV or LABEL) which do not consume disk space.
56 You want to mount an NFS share directly as a Docker volume. Which of the following commands correctly creates a Docker volume backed by NFS using the local driver?
docker volume create --driver local --opt type=nfs --opt o=addr=192.168.1.100,rw --opt device=:/path/to/dir myvol
docker volume create --type nfs --server 192.168.1.100 --share /path/to/dir myvol
docker run -v nfs://192.168.1.100:/path/to/dir:/data myapp
docker volume create --driver nfs --name myvol
57
A developer wants to push an image to the GitHub Container Registry (GHCR). If their GitHub username is octocat and the image is app-backend, what is the strictly correct fully qualified image tag format they must use?
docker.pkg.github.com/octocat/app-backend:latest
ghcr.io/octocat/app-backend:latest
github.com/octocat/app-backend:latest
registry.github.com/octocat/app-backend:latest
58 A container needs to be completely isolated from the host network stack, have its own MAC address, and appear as a distinct physical device on the local network (e.g., pulling an IP directly from the physical router's DHCP). Which network driver MUST be used?
macvlan
host
overlay
bridge
59
In legacy Docker configurations, the --link flag was used to allow containers to communicate. What happens at the host OS level when containerA is linked to containerB using --link containerB:aliasB?
/etc/hosts file.
60 Assume a Docker image has 5 layers. A container is spawned from this image. The application inside the container attempts to read a file located in the 3rd layer (counting from the bottom). How does the storage driver resolve this read operation?
VOLUME instruction.