Introduction to Docker
Docker is a powerful platform that enables developers to build, deploy, and run applications in lightweight, portable containers. Unlike traditional virtual machines, Docker containers share the host OS kernel, making them faster and more efficient.
Why Use Docker?
Consistency: Works the same across different environments (development, testing, production).
Isolation: Applications run in separate containers, avoiding dependency conflicts.
Portability: Easily move containers between systems.
Scalability: Quickly scale applications using container orchestration tools like Kubernetes.
Docker Basics for Beginners
1. Key Docker Concepts
Container: A lightweight, standalone executable package that includes everything needed to run an application.
Image: A read-only template used to create containers (e.g., nginx, ubuntu).
Dockerfile: A script that defines how to build a Docker image.
Docker Hub: A public registry to share and pull Docker images.
2. Installing Docker
Linux:
sudo apt-get update && sudo apt-get install docker.io
Windows/macOS: Download Docker Desktop from Docker’s official website.
3. Basic Docker Commands
# Pull an image from Docker Hub
docker pull nginx
# Run a container
docker run -d -p 8080:80 --name my-nginx nginx
# List running containers
docker ps
# Stop a container
docker stop my-nginx
# Remove a container
docker rm my-nginx
Intermediate Docker: Custom Images & Docker Compose
1. Creating a Custom Docker Image
Example Dockerfile:
FROM python:3.8-slim
WORKDIR /app
COPY . .
RUN pip install -r requirements.txt
CMD ["python", "app.py"]
Build and run:
docker build -t my-python-app .
docker run -d -p 5000:5000 my-python-app
2. Managing Multiple Containers with Docker Compose
Example docker-compose.yml:
version: '3'
services:
web:
image: nginx
ports:
- "8080:80"
db:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: example
Run with:
docker-compose up -d
Advanced Docker for Professionals
1. Docker Networking
Bridge Network (Default): Isolated containers on a single host.
Host Network: Shares the host’s network stack.
Overlay Network: Connects containers across multiple hosts (used in Swarm/Kubernetes).
2. Docker Security Best Practices
Use minimal base images (e.g., alpine instead of ubuntu).
Run containers as non-root users:
USER nobody
Scan images for vulnerabilities:
docker scan <image-name>
3. Docker in CI/CD Pipelines
Example GitHub Actions workflow:
jobs:
build-and-push:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: docker build -t my-app .
- run: docker login -u ${{ secrets.DOCKER_USER }} -p ${{ secrets.DOCKER_PASS }}
- run: docker push my-app
4. Kubernetes & Docker Swarm for Orchestration
Kubernetes: Manages large-scale container deployments.
Docker Swarm: Simpler alternative for smaller clusters.
Real-World Docker Use Cases
1. Microservices Architecture
Each service runs in its own container, enabling independent scaling.
2. DevOps & Continuous Deployment
Docker ensures consistency between development and production.
3. Legacy Application Modernization
Run old applications in containers without full VM overhead.
4. Data Science & Machine Learning
Reproducible environments for training models.