|
1 | 1 | <div align="center"> |
2 | 2 |
|
3 | | -# Introduction to Docker |
| 3 | +# 🐳 Introduction to Docker |
4 | 4 | </div> |
5 | 5 |
|
6 | | -Docker is a platform for building, shipping, and running applications in lightweight, portable containers. Containers package an application and its dependencies together, ensuring consistent behavior across development, CI, and production environments. |
7 | | - |
8 | | -## Why use Docker |
9 | | -- Reproducible runtime environments across machines and teams. |
10 | | -- Faster developer workflows (build once, run anywhere). |
11 | | -- Resource-efficient compared to full virtual machines. |
12 | | -- Simplifies dependency management and deployment pipelines. |
13 | | - |
14 | | -## Core concepts |
15 | | -- Image: Immutable, read-only snapshot that contains application code, runtime, libraries, and metadata. Built from a Dockerfile. |
16 | | -- Container: A running instance of an image; isolated filesystem, network namespace, and process tree. |
17 | | -- Dockerfile: Declarative text file that defines how to build an image. |
18 | | -- Registry: A service to store and distribute images (Docker Hub, private registries). |
19 | | -- Volume: Persistent storage that lives outside container lifecycle. |
20 | | -- Network: Isolation and connectivity between containers (bridge, host, overlay). |
21 | | -- Tag: Named pointer to an image version (e.g., myapp:1.0, nginx:latest). |
22 | | - |
23 | | -## Key components |
24 | | -- Docker Engine: The runtime that builds and runs containers. |
25 | | -- Docker CLI: Command-line interface (docker) to interact with the Engine. |
26 | | -- Docker Compose: Tool to define and run multi-container applications via YAML. |
27 | | -- Docker Desktop: Desktop application for macOS/Windows that bundles Engine, CLI, and tools. |
28 | | -- Registry/Hub: Public or private storage for images. |
29 | | - |
30 | | -## Typical workflow |
31 | | -1. Write a Dockerfile that describes the application image. |
32 | | -2. Build the image: `docker build -t myapp:latest .` |
33 | | -3. Run a container: `docker run -d --name myapp -p 8080:80 myapp:latest` |
34 | | -4. Test and iterate locally. |
35 | | -5. Push image to a registry: `docker push myregistry/myapp:latest` |
36 | | -6. Deploy by pulling the image to target hosts or orchestrator. |
37 | | - |
38 | | -## Minimal examples |
39 | | - |
40 | | -Dockerfile (simple Node.js app): |
| 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: |
41 | 32 | ```dockerfile |
42 | 33 | FROM node:18-alpine |
43 | 34 | WORKDIR /app |
44 | 35 | COPY package*.json ./ |
45 | | -RUN npm ci --production |
| 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 |
46 | 58 | COPY . . |
47 | | -CMD ["node", "server.js"] |
| 59 | +CMD ["python", "app.py"] |
48 | 60 | ``` |
49 | 61 |
|
50 | | -Run an nginx container: |
| 62 | +**2. Build your image** |
51 | 63 | ```bash |
52 | | -docker run -d --name web -p 80:80 nginx:stable |
| 64 | +docker build -t my-app:1.0 . |
53 | 65 | ``` |
54 | 66 |
|
55 | | -docker-compose.yml (web + redis): |
56 | | -```yaml |
57 | | -version: "3.8" |
58 | | -services: |
59 | | - web: |
60 | | - build: . |
61 | | - ports: |
62 | | - - "8080:80" |
63 | | - depends_on: |
64 | | - - redis |
65 | | - redis: |
66 | | - image: redis:7-alpine |
| 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 |
67 | 76 | ``` |
68 | | -Start: `docker compose up -d` |
69 | | - |
70 | | -## Useful commands |
71 | | -- Build: `docker build -t myapp:tag .` |
72 | | -- Run: `docker run -d -p 80:80 --name app myapp:tag` |
73 | | -- List running containers: `docker ps` |
74 | | -- List images: `docker images` |
75 | | -- Exec into container: `docker exec -it app /bin/sh` |
76 | | -- Logs: `docker logs -f app` |
77 | | -- Remove container/image: `docker rm app`, `docker rmi myapp:tag` |
78 | | -- Compose: `docker compose up -d`, `docker compose logs -f` |
79 | | - |
80 | | -## Best practices |
81 | | -- Use small base images (alpine, distroless) when possible. |
82 | | -- Follow multi-stage builds to reduce final image size. |
83 | | -- Do not store secrets in images; use environment variables, secrets managers, or Docker Secrets. |
84 | | -- Keep images immutable and versioned with tags. |
85 | | -- Add a .dockerignore file to speed builds and avoid leaking files. |
86 | | -- Run processes as non-root inside containers when feasible. |
87 | | -- Add HEALTHCHECK to images for orchestration health reporting. |
88 | | - |
89 | | -## Security considerations |
90 | | -- Scan images for vulnerabilities regularly. |
91 | | -- Minimize installed packages and attack surface. |
92 | | -- Use official or trusted base images. |
93 | | -- Pin image digests or tags for reproducible deployments. |
94 | | -- Limit container capabilities and use user namespaces or seccomp profiles. |
95 | | - |
96 | | -## Troubleshooting tips |
97 | | -- If container fails to start: check `docker logs <container>` and `docker inspect <container>`. |
98 | | -- Network issues: inspect networks (`docker network ls`, `docker network inspect`). |
99 | | -- Build issues: add `--progress=plain` and check Dockerfile layers; use `docker build --no-cache` to force rebuild. |
100 | | - |
101 | | -## Next steps / learning path |
102 | | -- Install Docker Desktop (Windows/macOS) or Docker Engine (Linux). |
103 | | -- Learn Dockerfile best practices and multi-stage builds. |
104 | | -- Learn Docker Compose for multi-container dev environments. |
105 | | -- Explore orchestration: Kubernetes or Docker Swarm for production-scale deployments. |
106 | | -- Read the official docs: https://docs.docker.com/ |
107 | | - |
108 | | -This file should serve as a concise reference for getting started with Docker and as a base to link to more detailed guides (Dockerfile patterns, Compose, security hardening, CI/CD integrations). |
| 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