Docker 101: Pack Your App Like Moving

服务器基础 Feb 25, 2021

"It works on MY machine!" - The Classic Dev Excuse

Every dev knows this pain. Code works locally, tests pass, perfect. Deploy to server - CRASH. Why? Your machine vs server: different OS, different Node version, different Python, different everything. Environment inconsistency. Enter Docker.

Docker in One Sentence

Docker is a shipping container. Pack your app + its entire environment (OS, dependencies, config) into a box, run it anywhere.

Think moving house: Traditional = disassemble all furniture, move, reassemble (parts get lost). Docker = pack each room into containers, use them as-is at new place.

Philosophy: Build once, run everywhere.

Core Concepts

1. Image = Blueprint / Recipe. Describes environment (Ubuntu + Python 3.11 + Flask).

FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD ["python", "app.py"]

2. Container = Instance from image. Same recipe makes same dish every time.

3. Registry = Image storage. Docker Hub = GitHub for Docker images.

Quick Commands

docker pull nginx:latest
docker run -d -p 8080:80 nginx
docker ps
docker stop CONTAINER_ID
docker build -t my-app:latest .

Docker Compose: One Command to Rule Them All

docker-compose up -d  # start all services
docker-compose down   # stop all

What Docker Solves

Three things: Environment consistency, fast deployment, resource isolation. Stop saying "it works on my machine" and learn Docker. Your colleagues will thank you.

Tags