Practice questions & answers
1
What is a Docker image?
-
1.
A running instance of a software package
-
2.
A read-only template with instructions for creating a Docker container
-
3.
A virtual machine with its own kernel
-
4.
A configuration file for Docker Compose
Show explanation
A Docker image is a read-only blueprint that includes everything needed to run an application, including the code, runtime, libraries, and environment variables.
2
Which Dockerfile instruction specifies the base image to use?
-
1.
START
-
2.
BASE
-
3.
FROM
-
4.
RUN
Show explanation
The FROM instruction initializes a new build stage and sets the Base Image for subsequent instructions.
3
What is the primary difference between a Docker image and a Docker container?
-
1.
Images are mutable, containers are immutable
-
2.
A container is a running instance of an image
-
3.
Images are larger than containers
-
4.
Containers are used for development, images for production
Show explanation
An image is a static template, while a container is a live, executable instance of that image.
4
Which command is used to build an image from a Dockerfile in the current directory?
-
1.
docker create .
-
2.
docker build -t my-app .
-
3.
docker run my-app
-
4.
docker image new .
Show explanation
The 'docker build' command with the '-t' flag tags the image, and the '.' specifies the current directory as the build context.
5
In a Dockerfile, what is the purpose of the WORKDIR instruction?
-
1.
To install dependencies
-
2.
To set the working directory for any subsequent instructions
-
3.
To define the entry point of the application
-
4.
To map local folders to the container
Show explanation
WORKDIR sets the working directory for any RUN, CMD, ENTRYPOINT, COPY, and ADD instructions that follow it.
6
Which instruction is best for installing Python packages via pip in a Dockerfile?
-
1.
CMD ["pip", "install", "-r", "requirements.txt"]
-
2.
RUN pip install -r requirements.txt
-
3.
ADD pip install -r requirements.txt
-
4.
ENV pip install -r requirements.txt
Show explanation
The RUN instruction executes commands in a new layer on top of the current image and commits the results.
7
Why should you copy 'requirements.txt' and run 'pip install' before copying the rest of your Python source code?
-
1.
To keep the Dockerfile shorter
-
2.
To ensure the code is compatible with the packages
-
3.
To leverage Docker's layer caching and speed up builds
-
4.
To prevent the container from crashing
Show explanation
Docker caches layers. If requirements.txt hasn't changed, Docker will skip the expensive pip install step even if your source code has changed.
8
What is the purpose of the .dockerignore file?
-
1.
To list files that should be deleted after the build
-
2.
To prevent local files and directories from being copied into the image
-
3.
To specify which images to ignore during a pull
-
4.
To hide sensitive environment variables
Show explanation
The .dockerignore file allows you to exclude files (like .git, __pycache__, or .env) from the build context, reducing image size and protecting secrets.
9
Which instruction defines the default command to run when a container starts?
-
1.
ENTRYPOINT
-
2.
RUN
-
3.
CMD
-
4.
START
Show explanation
CMD provides defaults for an executing container. There can only be one CMD instruction in a Dockerfile.
10
What is the difference between CMD and ENTRYPOINT?
-
1.
CMD cannot be overridden, ENTRYPOINT can
-
2.
ENTRYPOINT is used for arguments, CMD for the binary
-
3.
ENTRYPOINT sets the main executable, while CMD provides default arguments that can be overridden
-
4.
RUN is preferred over both
Show explanation
ENTRYPOINT defines the executable that will run, while CMD provides default arguments that the user can override from the command line.
11
How do you map port 8000 on your host to port 5000 in a Docker container?
-
1.
-p 5000:8000
-
2.
--port 8000:5000
-
3.
--map 8000:5000
-
4.
-p 8000:5000
Show explanation
The -p flag uses the format host_port:container_port.
12
Which environment variable is commonly set in Python Dockerfiles to ensure logs are sent straight to the terminal?
-
1.
PYTHONPATH=.
-
2.
PYTHONUNBUFFERED=1
-
3.
PYTHONDONTWRITEBYTECODE=1
-
4.
DEBUG=True
Show explanation
Setting PYTHONUNBUFFERED=1 ensures that Python output is sent directly to stdout/stderr without being buffered, allowing for real-time logging in Docker.
13
What is the default filename for Docker Compose configurations?
-
1.
docker.yml
-
2.
docker-compose.yaml
-
3.
compose-config.json
-
4.
docker-stack.yml
Show explanation
Docker Compose looks for a file named docker-compose.yml or docker-compose.yaml by default.
14
Which command starts all services defined in a docker-compose.yml file in the background?
-
1.
docker-compose start
-
2.
docker-compose up -d
-
3.
docker-compose run
-
4.
docker-compose deploy
Show explanation
The 'up' command creates and starts containers, and the '-d' flag runs them in detached mode (background).
15
In Docker Compose, what does the 'volumes' key achieve?
-
1.
It increases the CPU limit
-
2.
It persists data by mapping a host directory to a container directory
-
3.
It defines the network speed
-
4.
It lists the images to download
Show explanation
Volumes are used to persist data generated by and used by Docker containers, mapping host paths to container paths.
16
How can you specify that a web service should wait for a database service to start in Docker Compose?
-
1.
Using the 'wait_for' key
-
2.
Using the 'depends_on' key
-
3.
Using the 'links' key
-
4.
Using the 'after' key
Show explanation
The 'depends_on' key expresses startup and shutdown dependencies between services.
17
Which command allows you to see the logs of a running Docker container?
-
1.
docker view logs
-
2.
docker logs
-
3.
docker cat logs
-
4.
docker-compose tail
Show explanation
'docker logs' fetches the logs of a specific container.
18
What is a multi-stage build in Docker?
-
1.
Running multiple containers at once
-
2.
Using multiple FROM statements to create a smaller final image
-
3.
Building an image on multiple operating systems
-
4.
A build that uses both Docker and Kubernetes
Show explanation
Multi-stage builds allow you to use multiple FROM statements, copying only the necessary artifacts from one stage to another to keep the final image size small.
19
Which base image is typically the smallest for a Python application?
-
1.
python:3.9
-
2.
python:3.9-slim
-
3.
python:3.9-alpine
-
4.
ubuntu:latest
Show explanation
The alpine images are based on Alpine Linux, a security-oriented, lightweight Linux distribution, making them much smaller than slim or standard images.
20
How do you execute a bash shell inside a running container named 'my-python-app'?
-
1.
docker shell my-python-app
-
2.
docker exec -it my-python-app bash
-
3.
docker run my-python-app bash
-
4.
docker attach my-python-app
Show explanation
'docker exec -it' allows you to run an interactive command (like bash) inside an already running container.
21
What does the 'COPY .' instruction do in a Dockerfile?
-
1.
Copies the Dockerfile to the image
-
2.
Copies all files from the current host directory to the current WORKDIR in the image
-
3.
Copies the image to the host
-
4.
Copies the container logs to the host
Show explanation
'COPY .' copies all files and folders from the current build context on the host to the current working directory in the image.
22
Which command stops and removes all containers, networks, and images defined in a Compose file?
-
1.
docker-compose stop
-
2.
docker-compose down
-
3.
docker-compose rm
-
4.
docker-compose clear
Show explanation
'docker-compose down' stops containers and removes containers, networks, volumes, and images created by 'up'.
23
How do you pass environment variables to a container at runtime using 'docker run'?
-
1.
-v VAR=VAL
-
2.
--env-file .env
-
3.
-e VAR=VAL
-
4.
Both -e and --env-file are correct
Show explanation
You can use -e for individual variables or --env-file to pass a file containing multiple variables.
24
What is the purpose of the EXPOSE instruction?
-
1.
It opens a port on the host machine automatically
-
2.
It functions as documentation between the image developer and the person running the container
-
3.
It encrypts the traffic on that port
-
4.
It maps the container port to a random host port
Show explanation
EXPOSE does not actually publish the port; it serves as documentation to indicate which ports are intended to be published.
25
In Docker Compose, how do services communicate with each other by default?
-
1.
Using the host's IP address
-
2.
Using the service name as the hostname
-
3.
Using a shared file system
-
4.
They cannot communicate by default
Show explanation
Docker Compose sets up a single network for all services, and each container is reachable by other containers via its service name.
26
Which Docker command lists all running containers?
-
1.
docker images
-
2.
docker ps
-
3.
docker list
-
4.
docker containers
Show explanation
'docker ps' lists all currently running containers. 'docker ps -a' lists all containers, including stopped ones.
27
What is the benefit of using 'python:slim' over 'python:alpine' for some Python packages?
-
1.
Slim is smaller than Alpine
-
2.
Slim includes common build tools and glibc, avoiding compatibility issues with some C-extensions
-
3.
Alpine is deprecated
-
4.
Slim is faster at runtime
Show explanation
Many Python wheels are built for glibc (used in slim/Debian). Alpine uses musl, which can require recompiling C-extensions, leading to slower builds.
28
Which instruction should be used to run a script that performs setup tasks every time the container starts?
-
1.
RUN
-
2.
CMD
-
3.
ENTRYPOINT
-
4.
WORKDIR
Show explanation
ENTRYPOINT is ideal for 'wrapper' scripts that perform initialization before starting the main application.
29
How do you remove a Docker image from your local machine?
-
1.
docker rm
-
2.
docker rmi
-
3.
docker delete
-
4.
docker-compose down
Show explanation
'docker rmi' (remove image) is used to delete images, while 'docker rm' is used to delete containers.
30
What does the 'build: .' key in a docker-compose.yml file indicate?
-
1.
Download the image from Docker Hub
-
2.
Build the image using the Dockerfile in the current directory
-
3.
Use the current directory as a volume
-
4.
Restart the container if it fails
Show explanation
The 'build' key specifies the path to the build context (the directory containing the Dockerfile).
31
Which Dockerfile instruction is used to set environment variables that persist during runtime?
-
1.
ARG
-
2.
SET
-
3.
ENV
-
4.
VAR
Show explanation
ENV sets environment variables that are available both during the build and when the container is running.
32
What is the difference between ADD and COPY in a Dockerfile?
-
1.
They are identical
-
2.
ADD can fetch remote URLs and extract tar files; COPY is for basic local file copying
-
3.
COPY is faster than ADD
-
4.
ADD is the newer version of COPY
Show explanation
While both copy files, ADD has extra features like downloading from URLs and auto-extracting compressed archives.
33
Which command is used to see the resource usage (CPU, Memory) of running containers?
-
1.
docker info
-
2.
docker stats
-
3.
docker top
-
4.
docker inspect
Show explanation
'docker stats' provides a live stream of resource usage statistics for your containers.
34
How can you ensure a container restarts automatically if it crashes?
-
1.
Using the 'restart: always' policy
-
2.
Using the 'retry' instruction in Dockerfile
-
3.
Manually running a cron job
-
4.
Docker containers always restart by default
Show explanation
The 'restart' policy in Docker or Docker Compose (e.g., 'always', 'on-failure') manages container restarts.
35
What is the 'build context' in Docker?
-
1.
The operating system of the host
-
2.
The set of files available to the Docker daemon during the 'docker build' process
-
3.
The memory limit for the build
-
4.
The version of Docker installed
Show explanation
The build context is the set of files located at the specified path or URL, which are sent to the Docker daemon to build the image.