Docker Notes
Docker Notes
If you master the concepts outlined below, you’ll be well-prepared to answer any interview
question about Docker with confidence. Let’s break it down step by step.
1. Introduction to Docker
What is Docker?
Docker is an open-source platform that allows developers to automate the deployment, scaling,
and management of applications using containerization. It enables applications to run in
isolated environments (containers) with all their dependencies, ensuring consistency across
multiple environments.
Docker helps solve the classic "it works on my machine" problem by allowing developers to
package applications with all required dependencies, libraries, and configurations into a
standardized unit called a container.
2. Benefits of Containerization
Docker leverages containerization, which provides numerous advantages over traditional
deployment methods.
1. Speed: Since containers share the host OS, they start instantly, unlike VMs, which
require booting an entire OS.
2. Efficiency: More containers can run on the same hardware compared to VMs.
3. Flexibility: Containers can be deployed and scaled dynamically.
4. Simplified Dependency Management: Containers package everything needed to run an
application.
1. System Requirements:
o Windows 10 Pro, Enterprise, or Education (1903 or later) OR Windows 11
o WSL 2 (Windows Subsystem for Linux) enabled for best performance
2. Installation Steps:
o Download Docker Desktop from Docker’s official website
o Run the installer and follow the setup instructions
o Enable WSL 2 during installation for better performance
o Verify installation using:
o docker --version
o docker run hello-world
1. System Requirements:
o macOS 10.14 (Mojave) or later
o Apple Silicon (M1, M2) or Intel chip
2. Installation Steps:
o Download Docker Desktop for Mac from Docker’s website
o Install and grant necessary permissions
o Run Docker and verify with:
o docker --version
o docker run hello-world
1. System Requirements:
o 64-bit OS (Ubuntu, Debian, Fedora, CentOS, etc.)
o Kernel version 3.10 or later
2. Installation on Ubuntu/Debian:
3. sudo apt update
4. sudo apt install -y docker.io
5. sudo systemctl start docker
6. sudo systemctl enable docker
7. Installation on CentOS:
8. sudo yum install -y docker
9. sudo systemctl start docker
10. sudo systemctl enable docker
11. Verify Installation:
12. docker --version
13. docker run hello-world
Component Description
Docker Background process running on the host, responsible for managing Docker
Daemon objects (containers, images, networks, volumes).
Command-line interface (CLI) that interacts with the Docker Daemon via the
Docker Client
Docker API.
Read-only templates containing everything needed to run an application (OS,
Docker Images
dependencies, code, etc.).
Docker Running instances of Docker images that execute applications in isolated
Containers environments.
Docker Storage for Docker images (Docker Hub, AWS ECR, Google Artifact Registry,
Registries private registries).
5. Deep Dive into Docker Architecture Components
Let’s explore each component in greater depth.
3. Docker Images
Example Dockerfile:
FROM python:3.9
WORKDIR /app
COPY . /app
RUN pip install -r requirements.txt
CMD ["python", "app.py"]
5. Docker Registries
Summary
Docker is a containerization platform that ensures consistency across environments.
Containers vs. VMs: Containers are lightweight, faster, and more efficient.
Installing Docker varies by OS but follows similar steps.
Docker Architecture consists of the Docker Daemon, Client, Images, Containers, and
Registries.
Core Commands: docker pull, docker run, docker ps, docker stop, docker rm, docker push, etc.
Got it! Let's go in-depth into working with Docker containers so you fully understand how to
handle them in an interview.
The docker run command is used to create and start a new container from an image. It performs
these steps internally:
Basic Usage
This runs an ubuntu container, but since no command is specified, it starts and then
immediately exits.
docker ps -a
3. Managing Containers
Starting a Stopped Container
Restarting a Container
docker restart my_container
Viewing Logs
Runs ls /app inside the container and shows output in the host terminal.
docker rm my_container
docker rm -f my_container
Interview-Level Understanding
Key Questions You Should Be Able to Answer
1. What happens when you run docker run?
o It pulls the image if not available, creates a container, and starts it.
2. How do you run an interactive shell in a container?
o Use docker run -it <image> bash or sh for minimal images.
3. How do you restart a container?
o docker restart <container_name>.
4. Difference between docker exec and docker attach?
o docker exec runs a new process in the container.
o docker attach connects to the container's main process.
5. How do you remove all stopped containers at once?
o docker container prune.
This should give you everything needed to confidently answer any Docker container interview
question! Do you want me to add anything else?
Code
Runtime
System libraries
Dependencies
Environment variables
Configuration files
Docker images are immutable, meaning they cannot be changed once created. Instead,
modifications require building a new image.
Base Image Layer: The starting point of an image (e.g., ubuntu:latest, node:18-alpine).
Intermediate Layers: Created by commands in the Dockerfile (e.g., RUN apt-get update).
Read-Only Layers: All image layers are read-only.
Copy-on-Write Mechanism: When a container is created from an image, Docker adds a read-
write layer on top of the read-only layers, allowing changes.
Docker uses a UnionFS (Union File System) to manage layers efficiently. Popular storage drivers
include:
Command:
How It Works:
docker images
docker images
Example output:
Inspecting an Image
Example:
linux
1. Start a container:
2. docker run -it ubuntu bash
3. Install software inside the container:
4. apt update && apt install -y vim
5. Exit the container:
6. exit
7. Find the container ID:
8. docker ps -a
9. Commit the container as a new image:
10. docker commit <container_id> my_custom_ubuntu
11. Verify the new image:
12. docker images
It lacks reproducibility.
Changes are not version-controlled.
The preferred approach is using a Dockerfile.
Example:
If the image is being used by a running container, you must stop and remove the container
first:
Pull an image docker pull <image> Fetches an image from Docker Hub
Create an image docker commit <container> <image> Saves container changes as a new image
Remove an image docker rmi <image> Deletes an image from local storage
Interview Tips
Explain Docker Images in terms of layers (UnionFS, caching).
Compare docker commit vs. Dockerfile (commit is manual, Dockerfile is best practice).
Describe how images are pulled and stored (local cache, Docker Hub).
Know how to inspect images (docker inspect).
Understand image cleanup strategies (docker image prune).
With this knowledge, you should be well-prepared for any Docker image-related interview
question!
Absolutely! Let's go deep into Dockerfiles & Image Creation so that you can confidently answer
any interview question.
1. Writing a Dockerfile
A Dockerfile is a script-like text file containing instructions to create a Docker image. Each
instruction in the file is executed in order to assemble the image.
# Install dependencies
RUN npm install
2.1 FROM
2.2 WORKDIR
2.4 RUN
2.6 EXPOSE
2.7 ENV
2.8 VOLUME
2.9 LABEL
3. Multi-Stage Builds
Multi-stage builds help reduce image size by using multiple FROM statements.
Key Flags
docker images
1. What is a Dockerfile?
o A script defining instructions to build a Docker image.
2. What is the difference between COPY and ADD?
o COPY only copies files, ADD can handle URLs and extract archives.
Intermediate
Advanced
Conclusion
Mastering Dockerfiles is crucial for creating efficient, secure, and scalable containerized
applications. By understanding instructions, best practices, and advanced techniques like multi-
stage builds, you’ll be well-prepared for real-world development and DevOps interviews.
3. Docker Volumes
Docker volumes are the recommended storage method because they are managed by Docker
and work across different OS platforms. Volumes persist data independently of containers.
1. Creating a Volume
docker volume create my_volume
2. Listing Volumes
docker volume ls
3. Inspecting a Volume
docker volume inspect my_volume
4. Removing a Volume
docker volume rm my_volume
4. Bind Mounts
Bind mounts directly map a directory from the host machine into the container. Unlike
volumes, Docker does not manage these mounts.
Example:
mkdir /tmp/mydata
docker run -d -v /tmp/mydata:/app/data ubuntu
A data-only container can be used to hold volumes, which other containers can access.
1. Backup a Volume
docker run --rm -v my_volume:/data -v $(pwd):/backup ubuntu tar -cvf /backup/my_volume_backup.tar /data
2. Restore a Volume
docker run --rm -v my_volume:/data -v $(pwd):/backup ubuntu tar -xvf /backup/my_volume_backup.tar -C /data
docker run --rm -v my_volume:/data -v $(pwd):/backup ubuntu tar -cvf /backup/backup.tar /data
Restore:
docker run --rm -v my_volume:/data -v $(pwd):/backup ubuntu tar -xvf /backup/backup.tar -C /data
docker network ls
Network
Description Use Case
Type
Default network where containers can
Bridge Running multiple containers on the
communicate using internal IPs but are
(default) same host with isolated networking.
isolated from the host.
The container shares the host machine's When you need low latency and want
Host
network, removing network isolation. to use host ports directly.
Network
Description Use Case
Type
Used in Docker Swarm mode for
Scaling applications across multiple
Overlay communication between containers
nodes.
running on different hosts.
Assigns a real MAC address to the When you need containers to act as
Macvlan container, making it appear as a physical independent network devices (e.g., for
device on the network. legacy applications).
The container has no network interface. Security-sensitive applications where
None
Used for strict isolation. network access is unnecessary.
Key Features:
Use Cases:
Key Features:
Key Features:
Now, containers on my_macvlan_network will appear as if they are real network devices.
Use Cases:
Key Features:
Use Cases:
Security-sensitive workloads
Completely isolated environments
Option Description
-p host_port:container_port Manually maps a specific port.
-P (uppercase) Randomly assigns ports.
Example:
docker ps
5. Network Troubleshooting
5.1 Inspecting Networks
Use docker network inspect to get details about a network.
Conclusion
Understanding Docker networking is essential for working with containers effectively. Here’s a
summary of key takeaways:
With this knowledge, you should be able to answer any interview question about Docker
networking confidently.
Deep Dive into Docker Compose
Docker Compose is an essential tool for managing multi-container Docker applications. It
simplifies the process of defining, configuring, and running multiple Docker containers using a
single YAML file. Below, we will explore every key aspect in detail, ensuring you can confidently
answer any interview question about Docker Compose.
db:
image: mysql:5.7
restart: always
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: mydatabase
MYSQL_USER: user
MYSQL_PASSWORD: password
volumes:
- db_data:/var/lib/mysql
Breaking It Down:
1. Version: Specifies the Compose file format version (3.9 is the latest at the time of
writing).
2. Services: Defines multiple containers that make up the application.
o app: The application container.
o db: A MySQL database container.
3. depends_on: Ensures db starts before app.
4. Ports: Maps container ports to the host.
5. Environment Variables: Passes configuration values to the container.
6. Volumes: Defines persistent storage for the MySQL database.
services:
web:
build: ./frontend
ports:
- "3000:3000"
depends_on:
- api
api:
build: ./backend
ports:
- "5000:5000"
environment:
DATABASE_URL: postgresql://user:password@db/mydatabase
depends_on:
- db
db:
image: postgres:13
restart: always
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: password
POSTGRES_DB: mydatabase
volumes:
- db_data:/var/lib/postgresql/data
volumes:
db_data:
Scaling Services
services:
app:
environment:
- NODE_ENV=production
- API_URL=http://api.example.com
NODE_ENV=production
API_URL=http://api.example.com
Reference it in docker-compose.yml:
services:
app:
env_file:
- .env
volumes:
db_data:
services:
db:
image: mysql
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
interval: 10s
retries: 5
Usage:
docker-compose config
Summary
Docker Compose simplifies multi-container Docker applications.
A docker-compose.yml file defines services, networks, and volumes.
Use docker-compose up and docker-compose down to manage containers.
Environment variables help manage configurations across environments.
Best practices ensure maintainability, scalability, and security.
With this deep understanding, you should be well-prepared for any interview question on
Docker Compose!
Docker Hub is the default public registry provided by Docker, where users can store and share
container images.
Advantages:
Public & Private Repositories: Public repositories allow open sharing of images, while private
repositories (limited in the free tier) enable controlled access.
Official Images: Many official, verified images (e.g., Ubuntu, Nginx, MySQL) are available for
secure and standardized use.
Automated Builds & CI/CD Integration: Supports automated builds from GitHub/GitLab
repositories.
Image Scanning: Security vulnerability scanning for images.
Rate Limits: Free-tier users have download rate limits (100 pulls per 6 hours for anonymous
users, 200 for authenticated users).
Limitations:
A private registry is a self-hosted or cloud-hosted alternative to Docker Hub that provides full
control over image distribution.
Advantages:
Security & Privacy: Images are stored internally, reducing security risks.
Speed & Performance: Local hosting reduces image pull latency.
Cost Control: Avoids rate limits and Docker Hub storage costs.
Access Control: Fully customizable authentication and authorization mechanisms.
By default, the registry stores data inside the container. To persist images across restarts,
mount a volume:
Pulling an Image
docker pull localhost:5000/myimage
Creating a Repository
aws ecr create-repository --repository-name my-app
Login to ACR
az acr login --name myacr
Conclusion
Understanding Docker Registry & Image Distribution is crucial for DevOps, cloud computing,
and containerized applications. Here’s what you should take away:
Got it! I'll go in-depth into Docker security so you can confidently answer any interview
question. Let's break each topic down thoroughly.
By default, containers run as root inside the container, which can be dangerous if an attacker
escapes the container.
Running as a non-root user reduces the impact of an exploit.
FROM alpine
RUN addgroup -S appgroup && adduser -S appuser -G appgroup
USER appuser
CMD ["sh"]
Now, even if an attacker gains control, they won’t have root privileges.
This limits the syscalls a container can make, reducing the attack surface.
2. User Namespaces and Rootless Containers
User namespaces allow mapping container user IDs (UIDs) to non-root UIDs on the host.
If a container runs as root inside, but it's mapped to a non-root user outside, even if the
container is compromised, it can't get root access on the host.
1. Edit /etc/docker/daemon.json:
{
"userns-remap": "default"
}
2. Restart Docker:
Rootless Containers
dockerd-rootless-setuptool.sh install
How It Works
Enable DCT
export DOCKER_CONTENT_TRUST=1
2. Sign an image:
Conclusion
This covers deep Docker security practices. Here’s a quick recap: ✅Least Privilege: Use non-
root users, drop capabilities.
✅User Namespaces: Map container UIDs to non-root host UIDs.
✅Image Security: Scan images, use minimal images, sign images with DCT.
✅Secrets Management: Use Docker secrets or secret managers.
✅Network Security: Isolate networks, disable inter-container communication, use TLS.
Docker Swarm is Docker’s native container orchestration tool. It allows you to manage and
scale containerized applications across multiple nodes in a cluster. Swarm is built into Docker,
making it a lightweight and easy-to-use alternative to Kubernetes.
Cluster Management: Swarm turns multiple Docker nodes into a single virtual system.
Service Scaling: Scale up or down containerized applications easily.
Load Balancing: Distributes traffic across running services.
Rolling Updates & Rollbacks: Deploy new versions without downtime.
Decentralized Design: Uses leader-election among managers.
Security: TLS encryption and mutual authentication.
A service in Docker Swarm is a description of how containers should run across the cluster.
Creating a Service
Listing Services
docker service ls
Inspecting a Service
docker service inspect webserver
Updating a Service
docker service update --image nginx:latest webserver
Scaling Up
docker service scale webserver=5
Increases replicas to 5.
Scaling Down
docker service scale webserver=2
Reduces replicas to 2.
Verifying Scaling
docker service ps webserver
For production, a cluster should have multiple manager and worker nodes.
Example Setup:
Create docker-compose.yml:
version: '3'
services:
web:
image: nginx
ports:
- "80:80"
deploy:
replicas: 3
update_config:
parallelism: 1
delay: 10s
restart_policy:
condition: on-failure
Deploy it:
Advanced Questions
Final Notes
Mastering these concepts will prepare you for any Docker Swarm interview! Let me know if you
want to dive deeper into any topic.
Kubernetes vs. Docker Swarm: A Deep Dive
Kubernetes and Docker Swarm are two of the most popular container orchestration tools, each
with its strengths and weaknesses. This guide will give you an in-depth understanding of both,
so you can confidently answer any interview question on the topic.
Kubernetes:
Docker Swarm:
Docker Swarm is Docker's native container orchestration tool. It is built into Docker and
provides an easy way to manage and deploy containers across multiple nodes. Swarm is simpler
and more lightweight compared to Kubernetes but lacks some advanced features.
Key Takeaways:
Before deploying containers, you need a Kubernetes cluster. You can use:
To install Minikube:
Before deploying, ensure you have a Docker image. If you don’t have one, create it:
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-deployment
spec:
replicas: 3
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: myusername/myapp:v1
ports:
- containerPort: 80
apiVersion: v1
kind: Service
metadata:
name: myapp-service
spec:
selector:
app: myapp
ports:
- protocol: TCP
port: 80
targetPort: 80
type: LoadBalancer
Apply the Service:
Final Thoughts
Would you like a mock interview with Kubernetes and Docker Swarm questions to test your
understanding?
Absolutely! Let's dive deep into each of these advanced Docker topics so that you not only
understand them but can confidently answer any interview question.
1. Using Docker BuildKit for Faster Builds
What is Docker BuildKit?
Docker BuildKit is an advanced build backend for Docker that improves performance, security,
and flexibility. It was introduced in Docker 18.09 and became the default in Docker 23.0.
export DOCKER_BUILDKIT=1
{
"features": {
"buildkit": true
}
}
BuildKit provides additional syntax and features that improve performance. You can explicitly
enable it by adding this at the top of your Dockerfile:
# syntax=docker/dockerfile:1.4
Example usage:
FROM node:18 AS builder
WORKDIR /app
COPY package.json ./
RUN --mount=type=cache,target=/root/.npm npm install
COPY . .
RUN npm run build
FROM node:18
WORKDIR /app
COPY --from=builder /app/dist ./dist
CMD ["node", "dist/index.js"]
In this example:
A multi-architecture (multi-arch) image is a single Docker image that supports multiple CPU
architectures, such as:
x86_64 (AMD64)
ARM64 (e.g., Raspberry Pi, Apple M1/M2)
PowerPC and s390x (IBM mainframes)
Push to a registry:
This ensures your system can emulate different CPU architectures for building multi-arch
images.
docker inspect provides detailed metadata about containers, images, volumes, and networks.
Useful information:
IP address
Mounts and volumes
Network settings
Environment variables
To filter specific details:
2. docker logs
3. docker stats
docker stats
Example output:
cat /sys/fs/cgroup/memory/docker/<container_id>/memory.usage_in_bytes
pipeline {
agent any
stages {
stage('Build Image') {
steps {
sh 'docker build -t myapp:latest .'
}
}
stage('Push to Registry') {
steps {
sh 'docker tag myapp:latest myrepo/myapp:latest'
sh 'docker push myrepo/myapp:latest'
}
}
}
}
stages:
- build
- deploy
build:
stage: build
script:
- docker build -t myrepo/myapp:latest .
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
- docker push myrepo/myapp:latest
Final Thoughts
Understanding these advanced topics gives you a strong foundation in Docker and makes you
well-prepared for any interview question. Do you want to go even deeper into any of these
topics?
In production, logging helps track application behavior, debug issues, and analyze system
health. Since containers are ephemeral (temporary), logging must be centralized.
Logging Strategies:
Comparison Table
Prometheus
Grafana
services:
prometheus:
image: prom/prometheus
ports:
- "9090:9090"
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml
grafana:
image: grafana/grafana
ports:
- "3000:3000"
Best Practices
Optimization Strategies
1. Docker Swarm
2. docker service create --name web --replicas 3 -p 80:80 nginx
3. NGINX Reverse Proxy
4. upstream backend {
5. server app1:5000;
6. server app2:5000;
7. }
8. server {
9. listen 80;
10. location / {
11. proxy_pass http://backend;
12. }
13. }
14. HAProxy
15. frontend http_front
16. bind *:80
17. default_backend app_servers
18.
19. backend app_servers
20. server app1 app1:5000 check
21. server app2 app2:5000 check
Blue-Green Deployment
Best Practices
Final Thoughts
Logging: Use ELK, Fluentd, or Loki.
Monitoring: Prometheus scrapes metrics, Grafana visualizes.
Optimization: Use smaller images, multi-stage builds.
Load Balancing: NGINX, HAProxy, or Docker Swarm.
Deployments: Rolling updates or blue-green deployments.
Secrets: Use Docker Secrets or .env files.
Docker allows you to package applications with their dependencies into containers. Running
these containers on cloud providers such as AWS, Azure, and Google Cloud allows for
scalability, flexibility, and cost efficiency. The cloud platforms provide managed services for
running and orchestrating containers efficiently, reducing operational overhead.
ECS is a fully managed container orchestration service that runs Docker containers without
managing servers. It supports two launch types:
Task Definition – Defines which Docker container(s) to run, including CPU, memory, networking,
and IAM roles
Task – A running instance of a Task Definition
Service – Maintains the desired number of tasks and integrates with Load Balancers
Cluster – A logical grouping of tasks and services
EKS is a managed Kubernetes service that allows you to run Docker containers using
Kubernetes.
AWS Fargate runs Docker containers without managing servers. It works with both ECS and EKS.
Advantages of Fargate:
ACI is a serverless container solution that allows you to run containers without managing
infrastructure.
AKS is a fully managed Kubernetes service that allows you to orchestrate Docker containers.
GKE is Google Cloud’s managed Kubernetes service. It automates node provisioning, upgrades,
and scaling.
Azure ACI ✅ ❌ ✅
Azure AKS ❌ ✅ ❌
Google GKE ❌ ✅ ❌
Final Thoughts
Each cloud provider offers multiple ways to run Docker containers. If you want serverless
simplicity, use AWS Fargate, Azure ACI, or Google Cloud Run. If you prefer Kubernetes-based
orchestration, choose EKS, AKS, or GKE. Your choice depends on your use case, scalability
needs, and management preferences.
Got it! Let’s break down each of these topics in extreme detail so that you fully understand
them inside and out. By the end of this, you'll be able to confidently tackle any interview
question related to troubleshooting and debugging in Docker.
Containers run processes inside isolated environments, and their logs are not stored like
regular system logs.
Use docker logs <container_id> to fetch the logs from a container.
Example:
docker logs my_container
Use Cases:
o Identify application errors.
o See startup failures.
o Debug runtime issues.
Adding More Log Details:
o docker logs -f my_container → Follows (streams) logs in real-time.
o docker logs --tail 50 my_container → Shows the last 50 log lines.
o docker logs --timestamps my_container → Adds timestamps to logs.
If a container is running but behaving unexpectedly, you may need to enter it and inspect logs
or processes.
Error Message:
Cannot connect to the Docker daemon at unix:///var/run/docker.sock.
Possible Causes & Fixes:
1. Docker Daemon is Not Running
Restart Docker:
sudo systemctl restart docker
2. Permission Issues (Non-root User)
Add your user to the Docker group:
sudo usermod -aG docker $USER
newgrp docker
Error Message:
Bind for 0.0.0.0:80 failed: port is already allocated
Cause:
o Another process is using the same port.
Fix:
1. Find the conflicting process:
2. sudo netstat -tulnp | grep :80
3. Kill the process or use a different port:
4. docker run -p 8080:80 my_container
Error Message:
No space left on device
Cause:
o Docker is consuming too much disk space.
Fix:
o Remove stopped containers:
o docker container prune
o Remove dangling images:
o docker image prune
o Remove unused volumes:
o docker volume prune
Command:
docker stats
Output Details:
o CPU % Usage
o Memory Usage
o Network I/O
o Block I/O (Disk)
o PIDs (Processes)
Example Output:
CONTAINER ID NAME CPU % MEM USAGE / LIMIT NET I/O
a1b2c3d4e5 my_app 25.3% 250MiB / 2GiB 10MB / 5MB
Troubleshooting:
o If CPU is high, check running processes using docker top.
o If Memory is high, restart the container or optimize memory usage.
Command:
docker network inspect my_network
What It Shows:
o Containers connected to the network.
o Assigned IP addresses.
o Subnet information.
Example Output:
[
{
"Name": "my_network",
"Containers": {
"a1b2c3d4e5": {
"Name": "my_app",
"IPv4Address": "192.168.1.5"
}
}
}
]
Final Thoughts
This covers the essential troubleshooting and debugging techniques for Docker. You now have
the tools to diagnose and fix failing containers, resolve common errors, monitor performance,
and debug networking problems.
Would you like me to generate some practice interview questions for you?