Day 19 - Docker Interview Q&A
1. What is Docker?
Answer:
Docker is an open-source containerization platform used to build, package, and run applications inside lightweight, portable containers. It helps manage the entire lifecycle of containers β building images, running containers, pushing/pulling images from registries, etc.
You can add:
βIn my projects, I use Docker to write Dockerfiles, build images, run containers, optimize image size, and push artifacts to registries like Docker Hub/ECR.β
2. How are Containers different from Virtual Machines?
Answer:
| Containers | Virtual Machines |
| Lightweight | Heavyweight |
| Share the host OS kernel | Have full guest OS |
| Start in milliseconds | Start in minutes |
| Only need application + dependencies | Need OS + kernel + libraries |
| Image size is small (MBs) | Large images (GBs) |
Never say containers βdonβt have an OSβ β correct answer is:
They include only minimal system libraries, not a full OS.
3. Explain the Docker Lifecycle.
Answer:
The Docker lifecycle includes:
Write Dockerfile
Build image β
docker buildRun container β
docker runTag & Push image to registry (Docker Hub, ECR, GCR)
Pull image on any environment
Manage containers (start/stop/remove/prune)
4. What are the main Docker components?
Answer:
Docker Client (CLI) β sends commands
Docker Daemon β core engine that executes actions
Docker Images β read-only templates
Docker Containers β running instances of images
Docker Registry β stores images (Docker Hub, ECR, private registry)
Daemon is the βheartβ of Docker β if it stops, Docker actions cannot be executed.
5. Difference between COPY and ADD in Dockerfile?
Answer:
COPY β Copies files/folders from local machine β image (preferred)
ADD β Same as COPY + supports downloading from URL or auto-extracting archives.
Use COPY unless you specifically need ADDβs special features.
6. Difference between CMD and ENTRYPOINT?
Answer:
| CMD | ENTRYPOINT |
| Provides default arguments | Provides main executable |
| Can be overridden using CLI | Not overridden by default |
docker run image ls β ls replaces CMD | docker run image ls β ls becomes argument |
Best practice: Use ENTRYPOINT for the main command and CMD for default arguments.
Example:
ENTRYPOINT ["python", "app.py"]
CMD ["--port", "8000"]
7. What are Docker networking types? What is the default?
Answer:
bridge β default network for containers
host β container shares host network
overlay β used in multi-host (Swarm/Kubernetes)
macvlan β container appears as a physical device on network
none β no network
8. How do you isolate networking between containers?
Answer:
Create a custom bridge network:
docker network create secure_net
docker run --network secure_net ...
Containers on different networks cannot talk to each other unless explicitly connected.
9. What is a Multi-Stage Docker Build?
Answer:
It allows you to use multiple FROM statements and copy only the required build artifacts into the final image.
Why?
Reduces image size
Removes build tools from production image
Improves security
Example: Reduce image from ~800MB β 1MB using scratch/alpine.
10. What are Distroless Images?
Answer:
Distroless images (e.g., gcr.io/distroless/...) are minimal images that contain only:
your application
required runtime dependencies
They do not contain:
β shell (sh, bash)
β package managers (apt, yum)
β OS utilities (ping, curl)
Benefit:
Extremely secure, tiny, no attack surface.
π₯ Real-Time Docker Challenges (Must-Know for Interviews)
1. Docker Daemon β Single Point of Failure
Docker daemon is one single process
If daemon crashes β containers may stop or fail
Modern solution: Podman (daemonless, rootless).
2. Docker Daemon Runs as Root
By default, daemon runs with root privileges
If a container is compromised, host becomes vulnerable
Solution:
Use rootless Docker
Use Podman (runs fully rootless)
Always set
USERin Dockerfile
3. Image Size Issues
Developers often install unnecessary tools
Leads to huge (GB-sized) images
Slow deploys, security risks
Solutions:
Multi-stage builds
Distroless images
Base images like
alpine
4. Networking Misconfigurations
Wrong port mappings
Misuse of host network
Containers unintentionally communicating
Solution:
Custom networks & proper isolation.
5. Security Vulnerabilities
Using outdated base images
Running containers as root
Storing secrets inside images
Solution:
Scan images (Trivy, Anchore)
Use secrets manager
Use non-root user in Dockerfile