Skip to main content

Command Palette

Search for a command to run...

Docker Interview Questions and Answers

Published
5 min readView as Markdown
A

Anand Mohan is a Software Developer turned DevOps Engineer and a Public speaker. Anand has worked with multiple startups and MNCs and helped them scale their Development and Deployment and community. Anand currently working with HeaTrec Solutions. In his leisure time, Anand writes about Web Development, Cloud technologies, and DevOps . Anand also creates educational content on multiple platforms like Twitter and LinkedIn and Own Blogging Websites.

1. Basic Docker Questions

1.1 What is Docker?

Answer:
Docker is a containerization platform that allows developers to package applications and dependencies into lightweight, portable containers. It ensures that applications run consistently across different environments.

1.2 What are Docker’s key components?

Answer:

  • Docker Engine: The core service that runs containers.

  • Docker CLI: Command-line tool to interact with Docker.

  • Docker Images: Read-only templates for containers.

  • Docker Containers: Running instances of images.

  • Docker Hub: Public registry for storing images.

  • Docker Compose: Tool for managing multi-container applications.

1.3 How is Docker different from Virtual Machines (VMs)?

FeatureDocker (Containers)Virtual Machines
SizeLightweight (MBs)Heavy (GBs)
Boot TimeSecondsMinutes
PerformanceFasterSlower
IsolationProcess-levelFull OS
Resource UsageShares host kernelRequires full OS

2. Docker Architecture & Workflow

2.1 Explain the Docker architecture.

Answer:
Docker follows a client-server architecture with:

  1. Docker Client: Runs docker commands.

  2. Docker Daemon: Manages images, containers, and networks.

  3. Docker Registry (Docker Hub/Private Registry): Stores and distributes images.

  4. Docker Containers: Run applications using containerized environments.

2.2 How does Docker work?

Answer:

  1. Developer writes a Dockerfile.

  2. Builds an image using docker build.

  3. Runs a container using docker run.

  4. Pushes image to Docker Hub or a private registry.

  5. Deploys the container on any environment.


3. Docker Commands

3.1 What are the most commonly used Docker commands?

Answer:

CommandDescription
docker build -t myapp .Build an image from a Dockerfile
docker run -d -p 8080:80 myappRun a container in detached mode
docker ps -aList all containers
docker stop <container_id>Stop a running container
docker rm <container_id>Remove a container
docker rmi <image_id>Remove an image
docker logs <container_id>View container logs
docker exec -it <container_id> /bin/shAccess running container shell
docker-compose up -dStart services in docker-compose.yml

3.2 How do you check running containers?

docker ps

3.3 How do you restart a stopped container?

docker start <container_id>

4. Dockerfile & Image Management

4.1 What is a Dockerfile?

Answer:
A Dockerfile is a text file with instructions to automate the creation of a Docker image.

4.2 What are key Dockerfile instructions?

InstructionPurpose
FROMSpecifies the base image
LABELAdds metadata
COPY / ADDCopies files into the container
WORKDIRSets the working directory
RUNExecutes commands at build time
CMD / ENTRYPOINTDefines the startup command
EXPOSEDeclares the container's port
ENVSets environment variables

4.3 Example of a Dockerfile

FROM node:16
WORKDIR /app
COPY . .
RUN npm install
EXPOSE 3000
CMD ["node", "server.js"]

4.4 Difference between COPY and ADD?

FeatureCOPYADD
Basic File Copy
Extract Tar Archives
Supports URLs

5. Docker Networking

5.1 What are different Docker network types?

NetworkDescription
Bridge (Default)Containers communicate on a private network
HostContainer shares the host network
OverlayFor multi-host networking (Swarm)
NoneNo networking, isolated container

5.2 How to connect two containers?

docker network create mynetwork
docker run --network=mynetwork myapp

6. Docker Volumes & Data Persistence

6.1 What is a Docker volume?

Answer:
Docker volumes persist data beyond the container's lifecycle.

6.2 How to create and mount a volume?

docker volume create mydata
docker run -v mydata:/app/data myapp

7. Docker Compose & Multi-Container Apps

7.1 What is Docker Compose?

Answer:
Docker Compose manages multi-container applications using a YAML file.

7.2 Example docker-compose.yml file

version: "3"
services:
  web:
    image: nginx
    ports:
      - "8080:80"
  app:
    build: .
    depends_on:
      - db
  db:
    image: postgres
    environment:
      POSTGRES_PASSWORD: mypassword

7.3 Commands for Docker Compose

docker-compose up -d  # Start all services
docker-compose down   # Stop all services
docker-compose ps     # Check running services

8. Docker Security & Best Practices

8.1 How to secure Docker containers?

Run as non-root user (USER appuser)
Use minimal base images (e.g., alpine)
Limit container resources (--memory=512m --cpu=0.5)
Use multi-stage builds to reduce attack surface
Enable container scanning (docker scan myimage)


9. Docker Swarm & Kubernetes

9.1 What is Docker Swarm?

Answer:
Docker Swarm is Docker’s native orchestration tool for managing multiple containers across nodes.

9.2 What is Kubernetes?

Answer:
Kubernetes (K8s) is an orchestration platform for deploying, managing, and scaling containerized applications.

9.3 Docker Swarm vs Kubernetes

FeatureDocker SwarmKubernetes
ComplexitySimpleComplex
ScalingManual or automaticAdvanced scaling
NetworkingBuilt-inRequires setup
StorageLimitedAdvanced options

10. Advanced Docker Questions

10.1 What is a multi-stage build?

Multi-stage builds reduce final image size by separating build & runtime environments.

# Build stage
FROM node:16 AS builder
WORKDIR /app
COPY . .
RUN npm install && npm run build

# Final stage
FROM nginx:alpine
COPY --from=builder /app/dist /usr/share/nginx/html

10.2 How to optimize Docker images?

✅ Use .dockerignore to exclude unnecessary files
✅ Use multi-stage builds
✅ Minimize RUN layers
✅ Use alpine base images


Final Thoughts

These Docker interview questions cover everything from basics to advanced topics like security, networking, volumes, Kubernetes, and Swarm.

Would you like mock interview questions or real-world Docker scenario-based problems? 🚀

More from this blog

Frappe Devops - An ultimate place for Frappe Users

23 posts