|
| 1 | +<div align="center"> |
| 2 | + |
| 3 | +# 🐳 Introduction to Docker |
| 4 | +</div> |
| 5 | + |
| 6 | +Welcome! Docker helps you package and run applications in containers—think of them as lightweight, portable boxes that contain everything your app needs to run. |
| 7 | + |
| 8 | +## What is Docker? |
| 9 | + |
| 10 | +Docker is a platform that lets you build, ship, and run applications inside containers. Instead of worrying about "it works on my machine" problems, Docker ensures your app runs the same way everywhere. |
| 11 | + |
| 12 | +**Simple analogy:** Just like shipping containers standardized global trade, Docker containers standardize software deployment. Your application + its dependencies = one portable package. |
| 13 | + |
| 14 | +## Why Use Docker? |
| 15 | + |
| 16 | +- **Consistency** - Same behavior on your laptop, your teammate's computer, and production servers |
| 17 | +- **Fast setup** - New developers can start working in minutes instead of days |
| 18 | +- **Isolation** - Each app runs in its own environment without conflicts |
| 19 | +- **Efficiency** - Containers are lightweight and start in seconds |
| 20 | +- **Portability** - Build once, run anywhere |
| 21 | + |
| 22 | +## Core Concepts |
| 23 | + |
| 24 | +### Image |
| 25 | +A blueprint for your application. Contains your code, runtime, libraries, and configuration. Images are built from a **Dockerfile** and never change once created. |
| 26 | + |
| 27 | +### Container |
| 28 | +A running instance of an image. Lightweight, isolated, and disposable. You can run multiple containers from the same image. |
| 29 | + |
| 30 | +### Dockerfile |
| 31 | +A simple text file with instructions to build an image: |
| 32 | +```dockerfile |
| 33 | +FROM node:18-alpine |
| 34 | +WORKDIR /app |
| 35 | +COPY package*.json ./ |
| 36 | +RUN npm install |
| 37 | +COPY . . |
| 38 | +CMD ["node", "app.js"] |
| 39 | +``` |
| 40 | + |
| 41 | +### Registry |
| 42 | +A storage service for Docker images. **Docker Hub** is the most popular—like GitHub for Docker images. |
| 43 | + |
| 44 | +### Volume |
| 45 | +Persistent storage that survives when containers are deleted. Use for databases, logs, and user files. |
| 46 | + |
| 47 | +### Network |
| 48 | +Allows containers to communicate with each other securely. |
| 49 | + |
| 50 | +## Quick Start Workflow |
| 51 | + |
| 52 | +**1. Create a Dockerfile** |
| 53 | +```dockerfile |
| 54 | +FROM python:3.11-slim |
| 55 | +WORKDIR /app |
| 56 | +COPY requirements.txt . |
| 57 | +RUN pip install -r requirements.txt |
| 58 | +COPY . . |
| 59 | +CMD ["python", "app.py"] |
| 60 | +``` |
| 61 | + |
| 62 | +**2. Build your image** |
| 63 | +```bash |
| 64 | +docker build -t my-app:1.0 . |
| 65 | +``` |
| 66 | + |
| 67 | +**3. Run a container** |
| 68 | +```bash |
| 69 | +docker run -d --name my-app -p 8000:8000 my-app:1.0 |
| 70 | +``` |
| 71 | + |
| 72 | +**4. Check it's running** |
| 73 | +```bash |
| 74 | +docker ps |
| 75 | +docker logs my-app |
| 76 | +``` |
| 77 | + |
| 78 | +That's it! Your app is now running in a container. |
| 79 | + |
| 80 | +## Essential Commands |
| 81 | + |
| 82 | +```bash |
| 83 | +# Build an image |
| 84 | +docker build -t myapp:1.0 . |
| 85 | + |
| 86 | +# Run a container |
| 87 | +docker run -d --name myapp -p 8080:80 myapp:1.0 |
| 88 | + |
| 89 | +# List running containers |
| 90 | +docker ps |
| 91 | + |
| 92 | +# View logs |
| 93 | +docker logs myapp |
| 94 | + |
| 95 | +# Stop a container |
| 96 | +docker stop myapp |
| 97 | + |
| 98 | +# Remove a container |
| 99 | +docker rm myapp |
| 100 | + |
| 101 | +# List images |
| 102 | +docker images |
| 103 | + |
| 104 | +# Remove an image |
| 105 | +docker rmi myapp:1.0 |
| 106 | +``` |
| 107 | + |
| 108 | + |
| 109 | +## Best Practices |
| 110 | + |
| 111 | +✅ **Use official base images** - `node:18-alpine`, `python:3.11-slim` |
| 112 | +✅ **Use specific tags** - Avoid `:latest` in production |
| 113 | +✅ **Keep images small** - Use Alpine or slim variants |
| 114 | +✅ **Add .dockerignore** - Exclude `node_modules`, `.git`, logs |
| 115 | +✅ **Don't run as root** - Create a non-privileged user |
| 116 | +✅ **Use volumes for data** - Never store important data in containers |
| 117 | +✅ **One process per container** - Keep it simple and focused |
| 118 | + |
| 119 | + |
| 120 | +## Next Steps |
| 121 | + |
| 122 | +1. **Install Docker** - Get Docker Desktop (Mac/Windows) or Docker Engine (Linux) |
| 123 | +2. **Try the examples** - Build and run the sample Dockerfiles above |
| 124 | +3. **Learn Docker Compose** - Manage multi-container apps easily |
| 125 | +4. **Explore Docker Hub** - Find pre-built images for databases, web servers, etc. |
| 126 | +5. **Read the docs** - https://docs.docker.com/ |
| 127 | + |
| 128 | +## Key Takeaways |
| 129 | + |
| 130 | +- **Containers** package your app with everything it needs |
| 131 | +- **Images** are blueprints, containers are running instances |
| 132 | +- **Dockerfiles** define how to build images |
| 133 | +- **Docker Compose** manages multiple containers together |
| 134 | +- Docker makes development, testing, and deployment much easier |
| 135 | + |
| 136 | +Ready to containerize your first app? Start with a simple Dockerfile and experiment! 🚀 |
0 commit comments