Docker Interview Questions (50+ Detailed Q&A)
1. Fundamentals & Architecture
1. VM vs Container
1. VM vs Container
- VM: Hardware virtualization. Has Guest OS (Heavy). Isolation: Hypervisor level.
- Container: OS Virtualization. Shares Host Kernel (Light). Isolation: Namespaces & Cgroups.
2. Docker Architecture Components
2. Docker Architecture Components
- Daemon (dockerd): Background process. Manages objects.
- Client (docker): CLI. Sends API requests to Daemon.
- Registry: Stores images (Hub, GCR).
- Containerd: High-level runtime (Manages lifecycle).
- Runc: Low-level runtime (Spawns container using Kernel APIs).
docker pull nginx→ CLI sends request to Daemon- Daemon queries Registry for manifest
- Downloads layers (parallel)
- Stores in local cache (
/var/lib/docker)
docker run→ Daemon creates container config- Containerd prepares filesystem (union mount)
- Runc creates namespaces and cgroups
- Runc executes entrypoint process
3. Image Layers (Union File System)
3. Image Layers (Union File System)
RUN, COPY) creates a layer.
CoW (Copy on Write): If a container wants to modify a file from the image, it copies it up to the Writable Layer first.4. Dockerfile: COPY vs ADD
4. Dockerfile: COPY vs ADD
COPY: Local file -> Container. (Preferred).ADD: Can extract tarballs (add file.tar.gz) and download URLs. (Security risk if downloading from internet).
5. ENTRYPOINT vs CMD
5. ENTRYPOINT vs CMD
ENTRYPOINT: The executable to run. (Not easily overridden).CMD: Default arguments to the ENTRYPOINT. (Easily overridden). Pattern:ENTRYPOINT ["npm"],CMD ["start"].
6. What happens when you run `docker run`?
6. What happens when you run `docker run`?
- Check local cache for image.
- If missing, pull from Registry.
- Create new container (
runc). - Allocate File System (RW layer).
- Assign IP (Bridge).
- Execute entry process.
7. Detached vs Interactive Mode
7. Detached vs Interactive Mode
b -d: Detached (Background).b -it: Interactive TTY (Shell access).
8. Docker Context
8. Docker Context
docker context use my-remote-server.9. Image vs Container
9. Image vs Container
- Image: Class (Blueprint). Read-only.
- Container: Object (Instance). Read-Write.
10. Multi-Architecture Builds
10. Multi-Architecture Builds
docker buildx.
Builds images for amd64 and arm64 (M1 Mac) simultaneously using QEMU emulation.
Manifest list points to correct hash for arch.2. Networking & Storage
11. Docker Network Drivers
11. Docker Network Drivers
- Bridge (Default): Private network. NAT. Port Mapping required.
- Host: Shares Host IP. Fast. No port mapping.
- None: No network.
- Overlay: Cross-host (Swarm/K8s).
- Macvlan: Container gets real physical MAC address on LAN.
12. How Bridge Network works (Internals)
12. How Bridge Network works (Internals)
docker0 bridge (virtual switch).
Creates veth pair (virtual cable). One end in Container (eth0), one on Bridge (vethXXX).
Uses IP Tables for NAT (Masquerading) to allow outgoing traffic.13. Container to Container Communication
13. Container to Container Communication
- Same Bridge: By IP or Container Name (if using User-defined Bridge).
- Different Bridge: Cannot communicate by default.
- Legacy:
--link(Deprecated).
14. Exposing Ports (`-p`)
14. Exposing Ports (`-p`)
-p 8080:80.
Maps Host Port 8080 to Container Port 80.
Traffic -> Host IP:8080 -> Docker Proxy -> Container IP:80.15. Volumes vs Bind Mounts
15. Volumes vs Bind Mounts
- Volume: Managed by Docker (
/var/lib/docker/volumes). Best for data persistence. - Bind Mount: Maps arbitrary host path (
/home/user/code) to container. Best for Dev (Live reload).
16. Tmpfs Mount
16. Tmpfs Mount
17. Dangling Images/Volumes
17. Dangling Images/Volumes
- Dangling Image:
<none>:<none>. Result of overwriting a tag (building v1 again). - Dangling Volume: Volume not attached to any container.
- Cleanup:
docker system prune.
18. DNS in Docker
18. DNS in Docker
127.0.0.11).
Resolves container names to IPs within User-defined networks.
Default bridge does NOT support name resolution (Legacy).19. IPv6 Support
19. IPv6 Support
daemon.json.20. Backup/Restore Volume
20. Backup/Restore Volume
tar the volume content to the local folder.3. Best Practices & Optimization
21. Minimize Image Size
21. Minimize Image Size
- Use
alpinebase. - Multi-stage builds (Discard build tools).
- Combine
RUNcommands (One layer). - Remove cache (
apt-get clean). .dockerignore.
22. Layer Caching
22. Layer Caching
package.json -> Install -> Copy Source. (Prevents re-installing npm modules when code changes).23. Multi-Stage Builds
23. Multi-Stage Builds
- Size: 87% smaller (1.2GB → 150MB)
- Security: No build tools in production image
- Speed: Faster pulls and deployments
- Secrets: Build-time secrets don’t leak to final image
24. Handling PID 1 (Init Process)
24. Handling PID 1 (Init Process)
tini (--init flag) or dumb-init as entrypoint.25. Non-Root User
25. Non-Root User
RUN adduser -D myuser
USER myuser26. Health Checks
26. Health Checks
HEALTHCHECK CMD curl -f http://localhost/ || exit 1.
Daemon polls container. Status becomes unhealthy.27. .dockerignore
27. .dockerignore
node_modules, .git, .env, Dockerfile.
Reduces build context size (Faster upload to daemon).
Prevents leaking secrets.28. Tagging Strategy
28. Tagging Strategy
latest (Unpredictable).
Use Semantic Versioning (v1.0.1) or Commit SHA.29. ARG vs ENV
29. ARG vs ENV
ARG: Available during Build time only.ENV: Available during Run time (and Build time).
30. Flattening Images
30. Flattening Images
docker export -> docker import.
Merges all layers into one. Loses history. Good for distribution size.4. Troubleshooting & Operations
31. Docker Exec
31. Docker Exec
docker exec -it <id> /bin/sh.
Debug files/network inside.32. Logs
32. Logs
docker logs -f <id>.
Reads from STDOUT/STDERR.
Driver: Default json-file. Can change to syslog, awslogs.33. Inspect
33. Inspect
docker inspect <id>.
Returns big JSON: IP address, Env vars, Mounts, State (OOMKilled?).34. Container Exits Immediately
34. Container Exits Immediately
CMD ["echo", "hi"] -> Prints hi -> Exits).
Fix: Must run a foreground process (Web server).35. Connection Refused (Localhost)
35. Connection Refused (Localhost)
127.0.0.1 inside container.
Fix: Must listen on 0.0.0.0 to accept traffic from outside (Host).36. OOMKilled
36. OOMKilled
docker stats.37. Pruning
37. Pruning
docker system prune -a.
Dangerous. Deletes stopped containers, unused networks, and unused images.38. Docker Events
38. Docker Events
docker events.
Stream of real-time server actions (create, die, pull).
Good for monitoring tools.39. Stats
39. Stats
docker stats.
Live stream of CPU/RAM/Net I/O usage per container.40. Restart Policies
40. Restart Policies
no(Default).always(Even if manually stopped? No, only on crash/daemon restart).on-failure(Only if exit code != 0).unless-stopped.
5. Security & Ecosystem
41. Namespaces
41. Namespaces
PID: Process IDs.NET: Networking.MNT: Filesystem.User: User IDs.
42. Cgroups (Control Groups)
42. Cgroups (Control Groups)
43. Docker Socket Security
43. Docker Socket Security
/var/run/docker.sock lets container control the Host Docker Daemon.
Risk: Container can delete host containers, spawn privileged containers (Root on host).
Avoid unless necessary (CI/CD agents).44. Privileged Mode
44. Privileged Mode
--privileged. Gives container all Capabilities (Root on Host). Access to devices (/dev).
Security Nightmare.45. Content Trust (Notary)
45. Content Trust (Notary)
export DOCKER_CONTENT_TRUST=1.
Only run signed images.46. Docker Compose
46. Docker Compose
docker-compose.yml).
Services, Networks, Volumes.
One command startup (up).47. Docker Swarm
47. Docker Swarm
48. Podman vs Docker
48. Podman vs Docker
- Podman: Daemonless. Rootless by default. OCI Compliant.
- Docker: Daemon required. Root by default.
49. Distroless Images
49. Distroless Images
50. Seccomp Profiles
50. Seccomp Profiles
reboot(), swapoff(), etc.5. Docker Medium Level Questions
41. Docker Compose Services
41. Docker Compose Services
42. Docker Compose Networks
42. Docker Compose Networks
43. Environment Variables
43. Environment Variables
44. Health Checks
44. Health Checks
45. Build Args vs ENV
45. Build Args vs ENV
46. Docker Registry
46. Docker Registry
47. Docker Prune
47. Docker Prune
48. Container Logs
48. Container Logs
49. Docker Stats
49. Docker Stats
50. Docker Inspect
50. Docker Inspect
6. Docker Advanced Level Questions
51. Multi-Architecture Builds
51. Multi-Architecture Builds
52. BuildKit Cache Mounts
52. BuildKit Cache Mounts
53. Docker Content Trust
53. Docker Content Trust
54. User Namespaces
54. User Namespaces
55. AppArmor Profiles
55. AppArmor Profiles
56. Resource Constraints
56. Resource Constraints
57. Docker Swarm Mode
57. Docker Swarm Mode
58. Docker Secrets
58. Docker Secrets
59. Distroless Images
59. Distroless Images
60. Docker Socket Security
60. Docker Socket Security