Common Mistakes Beginners Make
17. Common Mistakes Beginners Make
Section titled “17. Common Mistakes Beginners Make”❌ Mistake 1: Not Naming Containers
Section titled “❌ Mistake 1: Not Naming Containers”# Bad: random names like "nervous_darwin"docker run -d nginx
# Good: meaningful namesdocker run -d --name web-server nginx❌ Mistake 2: Using latest in Production
Section titled “❌ Mistake 2: Using latest in Production”# Bad: unpredictable updatesdocker run node:latest
# Good: pin to specific versiondocker run node:18.17.0-alpine3.18❌ Mistake 3: Storing Data Inside Containers
Section titled “❌ Mistake 3: Storing Data Inside Containers”# Bad: data lost when container is removed!docker run -d mongo
# Good: use a volumedocker run -d -v mongo-data:/data/db mongo❌ Mistake 4: Port Conflicts
Section titled “❌ Mistake 4: Port Conflicts”# Error: port 8080 already in usedocker run -p 8080:80 nginx
# Fix: check what's using the portlsof -i :8080 # macOS/Linuxnetstat -ano | findstr :8080 # Windows
# Or use a different host portdocker run -p 8081:80 nginx❌ Mistake 5: Massive Image Sizes
Section titled “❌ Mistake 5: Massive Image Sizes”# Bad: full OS image (~1GB)FROM ubuntu:22.04RUN apt-get install nodejs
# Good: use official slim/alpine images (~50MB)FROM node:18-alpine❌ Mistake 6: Forgetting .dockerignore
Section titled “❌ Mistake 6: Forgetting .dockerignore”Without .dockerignore, your node_modules (often 500MB+) gets copied into the build context.
# Create .dockerignorecat > .dockerignore << 'EOF'node_modules.git.env*.logdistbuild.DS_StoreEOF❌ Mistake 7: Not Cleaning Up
Section titled “❌ Mistake 7: Not Cleaning Up”# Accumulated stopped containers, images, volumes eat disk space!# Check disk usage:docker system df
# Full cleanup (use carefully!):docker system prune -a --volumes
# Safer: clean specific resourcesdocker container prunedocker image prunedocker volume prunedocker network prune❌ Mistake 8: Running as Root Inside Containers
Section titled “❌ Mistake 8: Running as Root Inside Containers”# Bad: container runs as root by default (security risk)FROM node:18-alpineCMD ["node", "app.js"]
# Good: create and use a non-root userFROM node:18-alpineRUN addgroup -S appgroup && adduser -S appuser -G appgroupUSER appuserCMD ["node", "app.js"]