Docker has revolutionized software development by enabling lightweight, portable, and consistent application deployment through containerization. For software engineers, understanding Docker is critical not only for day-to-day tasks but also for excelling in technical interviews. This guide consolidates essential Docker concepts, commands, and best practices into a structured format tailored for interview preparation.
Docker is an open-source platform that automates application deployment using containers—lightweight, isolated environments that package code, dependencies, and configurations. Containers share the host OS kernel, making them faster and more resource-efficient than virtual machines (VMs).
Why Docker?
nginx:latest) used to create containers.| Image | Container |
|---|---|
| Static template (read-only) | Runtime instance (read-write) |
Built via docker build | Started via docker run |
Example: ubuntu:20.04 | Running Ubuntu instance |
A text file with instructions to automate image creation:
dockerfile
FROM alpine:latest RUN apk add --no-cache nginx COPY index.html /var/www/html/ CMD ["nginx", "-g", "daemon off;"]
Key Instructions:
FROM: Base image.RUN: Execute commands during build.COPY: Add files from host to image.CMD: Default command to run.docker pull nginx:latestdocker push username/repo:tagdocker createdocker startdocker stop (graceful) or docker kill (forceful)docker rmExample: Create a custom network and connect containers:
bash
docker network create my-net docker run --network my-net --name app1 nginx
Expose container ports to the host:
bash
docker run -p 8080:80 nginx # Host:8080 → Container:80
| Volume | Bind Mount |
|---|---|
| Managed by Docker | Maps host directory |
| Persistent and shareable | Tied to host filesystem |
Example: Create and mount a volume:
bash
docker volume create my-vol docker run -v my-vol:/data alpine
docker volume lsdocker volume pruneExample docker-compose.yml:
yaml
version: '3.8'
services:
web:
image: nginx
ports:
- "8080:80"
db:
image: postgres
environment:
POSTGRES_PASSWORD: password
Key Commands:
docker-compose updocker-compose up --scale web=3docker-compose.override.yml.docker secret.| Docker Swarm | Kubernetes |
|---|---|
| Simpler setup | Complex but highly scalable |
| Built-in service discovery | Extensive ecosystem |
Deploy in Swarm:
bash
docker swarm init docker service create --name web -p 8080:80 nginx
Optimize image size by separating build and runtime stages:
dockerfile
# Build stage FROM golang:1.17 AS builder WORKDIR /app COPY . . RUN go build -o app # Runtime stage FROM alpine:latest COPY --from=builder /app/app . CMD ["./app"]
docker logs <container-id>docker exec -it <container-id> /bin/bashdocker network inspect my-netdocker exec app1 ping app2docker run, docker-compose, and troubleshooting tools like docker logs.By internalizing these concepts and practicing commands, you’ll confidently tackle Docker interview questions. Focus on real-world scenarios, optimize your Dockerfiles, and stay updated with industry trends. Happy containerizing! 🐳