🐳 Docker Complete Guide

Essential commands for Docker development & maintenance

Core Operations

▢️
Start the Project
Essential

Build and start all services defined in docker-compose.yml

docker compose up --build
⏹️
Stop All Containers
Essential

Stop and remove all containers, networks created by up

docker compose down
πŸ”„
Restart After Changes
Common

Full restart with rebuild (use after code/config changes)

docker compose down docker compose up --build
πŸ”¨
Rebuild Specific Services
Efficient

Rebuild only the services you changed (faster than full rebuild)

# Rebuild backend only docker compose build backend # Rebuild frontend only docker compose build frontend # Then restart with: docker compose up -d
πŸ“‹
List Running Containers
Status Check

View all running containers in the project

docker compose ps

Monitoring & Debugging

πŸ“œ
View Service Logs
Debug

Stream logs from specific services (-f follows logs in real-time)

# Backend logs docker compose logs -f backend # Frontend logs docker compose logs -f frontend # MongoDB logs docker compose logs -f mongo # Phoenix observability logs docker compose logs -f phoenix # All logs docker compose logs -f
πŸ—„οΈ
Check MongoDB Status
Health Check

Verify MongoDB is running and check uptime

docker compose exec mongo mongosh --eval 'db.serverStatus().uptime'
πŸ”Œ
Check Backend API Health
Health Check

Test backend API and view automatic documentation

curl http://localhost:8003/docs

Or open in browser:

open http://localhost:8003/docs # Windows: start http://localhost:8003/docs # Linux: xdg-open http://localhost:8003/docs
πŸ–₯️
Check Frontend UI
Health Check

Open frontend application in browser

open http://localhost:3000 # Windows: start http://localhost:3000 # Linux: xdg-open http://localhost:3000
πŸ“Š
Check Phoenix Dashboard
Observability

Access Phoenix observability dashboard for monitoring

open http://localhost:6006 # Windows: start http://localhost:6006 # Linux: xdg-open http://localhost:6006
πŸ’‘ Monitoring Tips
  • Use docker compose logs -f service_name to debug startup issues
  • Check logs when containers exit unexpectedly
  • Monitor resource usage with docker stats
  • Keep Phoenix dashboard open during development for real-time insights

Cleanup & Maintenance

πŸ’Ύ Potential Savings: ~30-35 GB
1
Stop Containers
Required First

Stop all running containers before cleanup

docker compose down
2
Clean Build Cache
Biggest Win

Remove all build cache (typically frees ~15+ GB)

docker builder prune -af
3
Remove Unused Images
High Impact

Clean up dangling and unused images (keeps active ones)

# Remove dangling image layers docker image prune -f # Remove all unused images docker image prune -a -f
4
Clean Containers & Networks
Optional

Remove stopped containers and unused networks

docker container prune -f docker network prune -f
5
Prune Volumes
⚠️ Data Loss

Only if you can afford to lose persistent data

⚠️
This will delete all data in volumes (MongoDB, PostgreSQL, uploaded files, etc.)
# List volumes first to see what will be deleted docker volume ls # Prune unused volumes docker volume prune -f
πŸ’£
Nuclear Option: Clean Everything
Use Carefully

Remove all unused containers, networks, images (dangling and unreferenced), and build cache

docker system prune -af
βœ… Expected Results
After cleanup steps 1-4: Images drop from 24+ GB β†’ single digits | Build Cache β†’ 0 GB
πŸ—‘οΈ Cleanup Tips
  • Run docker system df to check disk usage before and after cleanup
  • Schedule weekly cleanups to prevent disk bloat
  • Always stop containers before pruning
  • Be careful with volume pruningβ€”you'll lose all database data
  • Use docker image ls to see what images you have before pruning

Best Practices

⚑
Enable BuildKit
Performance

Enable BuildKit for faster, more efficient builds with better caching

# Add to your ~/.bashrc or ~/.zshrc export DOCKER_BUILDKIT=1 export COMPOSE_DOCKER_CLI_BUILD=1 # Or run before each build: DOCKER_BUILDKIT=1 docker compose build
πŸ“
Create .dockerignore
Essential

Prevent unnecessary files from being sent to Docker daemon

# Create .dockerignore file in project root: .git .venv venv node_modules .next dist build storage __pycache__ *.pyc *.log *.md .DS_Store .env.local .idea .vscode coverage .pytest_cache
πŸ”
Monitor Disk Usage
Maintenance

Regularly check Docker disk usage to prevent bloat

# Check overall Docker disk usage docker system df # Detailed breakdown with verbose output docker system df -v
πŸ—οΈ
Multi-Stage Builds
Image Size

Use multi-stage builds to reduce final image size

# Example Dockerfile with multi-stage build FROM node:18 AS builder WORKDIR /app COPY package*.json ./ RUN npm ci COPY . . RUN npm run build FROM node:18-alpine WORKDIR /app COPY --from=builder /app/dist ./dist COPY --from=builder /app/node_modules ./node_modules CMD ["node", "dist/index.js"]
πŸš€
Hybrid Development
Recommended

Run services locally when possible, use Docker only for infrastructure

# Example: Run frontend + backend locally # Keep only databases/services in Docker # 1. Start only infrastructure docker compose up mongo phoenix # 2. Run backend locally cd backend && python main.py # 3. Run frontend locally cd frontend && npm run dev
πŸ”„
Smart Rebuilds
Efficient

Rebuild only what changed instead of everything

# Instead of full rebuild: docker compose build # Only rebuild specific service: docker compose build frontend docker compose build backend # Use --no-cache when needed: docker compose build --no-cache backend
πŸ’‘ Pro Tips
  • Layer Dockerfile commands wiselyβ€”put things that change less frequently first
  • Use .dockerignore to speed up builds and reduce image size
  • Pin specific versions in Dockerfile (node:18.19.0 instead of node:latest)
  • Use docker compose up -d to run in detached mode
  • Create shell aliases for common commands: alias dc='docker compose'
  • Schedule automated cleanups with cron or systemd timers
  • Keep development and production Dockerfiles separate
  • Use health checks in docker-compose.yml for better reliability
🎯 Development Workflow
Daily: Use docker compose logs -f for debugging
Weekly: Run docker system prune -f to clean up
Monthly: Full cleanup with docker system prune -af