Docker & Kubernetes practice questions with answers
Practise Docker and Kubernetes questions at your own pace: images, containers, Dockerfiles, volumes, networking, compose and Kubernetes basics. Pick an answer to see whether you are right and why. Nothing is timed or ranked here; for that, play the Daily Quiz at 4 PM IST.
What is the difference between a Docker image and a container?
easy
- They are the same
- An image is a read-only template; a container is a running instance of an image
- A container is stored in a registry
- Images run, containers are files
Answer: B. An image is a read-only template; a container is a running instance of an image
Explanation: You build an image once and can start many containers from it.
Which file describes how to build an image?
easy
- docker-compose.yml
- Dockerfile
- package.json
- .dockerignore
Answer: B. Dockerfile
Explanation: A Dockerfile lists instructions such as FROM, COPY, RUN and CMD. Compose files describe multi-container setups.
What does docker run -p 8080:3000 my-app do?
easy
- Runs the app on port 8080 inside the container
- Maps port 8080 on the host to port 3000 in the container
- Limits memory to 8080 MB
- Opens 3000 ports
Answer: B. Maps port 8080 on the host to port 3000 in the container
Explanation: The format is host:container. Visiting localhost:8080 reaches the app listening on 3000 inside.
Why use a volume?
easy
- To speed up builds
- To keep data (such as a database's files) outside the container so it survives restarts and removal
- To expose ports
- To shrink images
Answer: B. To keep data (such as a database's files) outside the container so it survives restarts and removal
Explanation: A container's own file system disappears when the container is removed.
Why copy package.json and install dependencies before copying the rest of the source in a Dockerfile?
medium
- It is required by Docker
- Layer caching: if only source files change, the dependency layer is reused and builds are much faster
- It makes the image smaller
- For security
Answer: B. Layer caching: if only source files change, the dependency layer is reused and builds are much faster
Explanation: Each instruction creates a layer that is cached until its inputs change. Put rarely changing steps first.
What is a multi-stage build for?
medium
- Running several containers
- Building in one stage with full tools, then copying only the output into a small runtime image
- Deploying to many clouds
- Parallel tests
Answer: B. Building in one stage with full tools, then copying only the output into a small runtime image
Explanation: Compilers and dev dependencies stay out of the final image, which makes it smaller and safer.
What is the difference between CMD and ENTRYPOINT?
medium
- None
- ENTRYPOINT sets the executable that always runs; CMD gives default arguments that docker run can override
- CMD runs at build time
- ENTRYPOINT only works on Linux
Answer: B. ENTRYPOINT sets the executable that always runs; CMD gives default arguments that docker run can override
Explanation: With ENTRYPOINT ["python", "app.py"] and CMD ["--port", "80"], docker run image --port 90 replaces only the CMD part.
In docker compose, how does a web service reach a db service?
medium
- Through localhost
- By the service name as host name, e.g. postgres://db:5432, on the network compose creates
- Only through published ports
- They cannot talk
Answer: B. By the service name as host name, e.g. postgres://db:5432, on the network compose creates
Explanation: Each container has its own localhost. Compose puts services on a shared network with DNS names.
How do containers differ from virtual machines?
hard
- Containers include a full guest OS
- Containers share the host kernel and isolate processes with namespaces and cgroups; VMs run a separate kernel on a hypervisor
- VMs start faster
- There is no difference
Answer: B. Containers share the host kernel and isolate processes with namespaces and cgroups; VMs run a separate kernel on a hypervisor
Explanation: That is why containers start in seconds and are lighter, but the isolation is weaker than a VM's.
Why should a container not run as root?
hard
- It is slower
- If the app is compromised, root in the container makes escaping to the host and damaging mounted files easier
- Root cannot use the network
- Docker forbids it
Answer: B. If the app is compromised, root in the container makes escaping to the host and damaging mounted files easier
Explanation: Add a USER instruction with an unprivileged user, and drop capabilities you do not need.
In Kubernetes, what is a Deployment responsible for?
hard
- Storing images
- Keeping a desired number of identical pods running and rolling out new versions gradually
- Routing external traffic
- Running database migrations
Answer: B. Keeping a desired number of identical pods running and rolling out new versions gradually
Explanation: A Service gives the pods a stable address; an Ingress routes external HTTP traffic to Services.
Which are good Dockerfile practices? (Select all that apply)
hard
- Use a .dockerignore to keep node_modules and .git out of the build context
- Pin base image versions instead of using latest
- Bake secrets such as API keys into the image
- Use multi-stage builds for compiled apps
Answer: A. Use a .dockerignore to keep node_modules and .git out of the build context; B. Pin base image versions instead of using latest; D. Use multi-stage builds for compiled apps
Explanation: Secrets in an image can be read by anyone who can pull it, even if a later layer deletes them. Pass them at run time instead.