Docker is an open-source platform for automating the deployment, scaling, and management of applications using containerization. Containers package code, dependencies, and environment into a single, portable unit.
Lightweight, fast, and consistent across environments
Enables microservices and DevOps workflows
Runs on Linux, Windows, Mac
Docker Architecture
Architecture
Docker Client: CLI and API for interacting with Docker
Docker Daemon: Background service managing containers
Docker Registry: Repository for storing images (Docker Hub)
Docker Images: Templates for creating containers
Docker Containers: Running instances of images
Docker vs Virtual Machines
Comparison
Feature
Docker
Virtual Machines
Size
Small (MBs)
Large (GBs)
Startup Time
Seconds
Minutes
Resource Usage
Low
High
Isolation
Process level
Hardware level
Docker Engine & Daemon
Engine
Docker Engine: Complete Docker platform
Docker Daemon: Background service (dockerd)
Manages containers, images, networks, volumes
REST API for client communication
# Check daemon status
sudo systemctl status docker
# Restart daemon
sudo systemctl restart docker
# Remove all unused data
docker system prune -a
# Remove unused volumes
docker volume prune
Copying Files (docker cp)
Copy
Copy files/folders between host and container
Useful for backups, logs, and data transfer
# Copy file from container to host
docker cp mycontainer:/path/in/container/file.txt ./file.txt
# Copy file from host to container
docker cp ./file.txt mycontainer:/path/in/container/file.txt
Docker Build Context
Context
Directory sent to Docker daemon during build
Includes Dockerfile and all referenced files
Use .dockerignore to exclude files
# Build context is current directory
docker build -t myapp .
# Exclude files in .dockerignore
cat .dockerignore
node_modules
.git
Entrypoint vs CMD
Entrypoint/CMD
CMD: Default command to run (can be overridden)
ENTRYPOINT: Always runs, even with arguments
Combine for flexible containers
# Example
ENTRYPOINT ["python"]
CMD ["app.py"]
# docker run myimage script.py (runs: python script.py)
Dockerfile Best Practices
Best Practices
Use official base images
Minimize layers and image size
Leverage .dockerignore
Use multi-stage builds for production
Pin versions for reproducibility
# Multi-stage build example
FROM node:18-alpine as build
WORKDIR /app
COPY . .
RUN npm install && npm run build
FROM nginx:alpine
COPY --from=build /app/dist /usr/share/nginx/html
Docker Swarm (Intro)
Swarm
Native clustering and orchestration for Docker
Manages multiple Docker hosts as a single cluster
Services, scaling, rolling updates
# Initialize swarm
docker swarm init
# Deploy a service
docker service create --name web -p 80:80 nginx
# List nodes
docker node ls
Kubernetes vs Docker Swarm
K8s vs Swarm
Kubernetes: Advanced, feature-rich, large ecosystem
Swarm: Simpler, built into Docker, easier to start
Both provide container orchestration, scaling, self-healing
Feature
Kubernetes
Swarm
Setup
Complex
Simple
Scaling
Advanced
Basic
Community
Large
Smaller
Security Best Practices
Security
Use minimal base images
Run as non-root user
Keep images up to date
Scan images for vulnerabilities
Limit container capabilities
# Scan image for vulnerabilities
docker scan myimage
# Run as non-root user in Dockerfile
USER node