Docker and Containerization for Agentic Dev
Use Docker to create reproducible development environments for AI-assisted coding.
What you'll accomplish
Set up a containerized development environment that works consistently for AI tools.
Prerequisites
- Docker installed
- Basic Docker knowledge
- Completed lesson 1
Why Containerize?
Containers solve critical problems for AI-assisted development:
- Reproducibility - AI generates code for your environment, not theirs
- Isolation - Mistakes can't break your system
- Consistency - Same environment across team members
- Portability - Move between machines seamlessly
Note
When AI generates code that "works on my machine," containers ensure it actually works.
Docker Installation Check
Development Dockerfile Pattern
For Node.js projects, use this development Dockerfile:
dockerfile# Dockerfile.dev FROM node:22-alpine WORKDIR /app # Install dependencies first (better caching) COPY package*.json ./ RUN npm ci # Copy source code COPY . . # Development port EXPOSE 3000 # Enable hot reloading ENV WATCHPACK_POLLING=true CMD ["npm", "run", "dev"]
Docker Compose for Development
yaml# docker-compose.yml services: web: build: context: . dockerfile: Dockerfile.dev ports: - "3000:3000" volumes: # Mount source for hot reloading - .:/app # Don't override node_modules - /app/node_modules environment: - NODE_ENV=development
Production Dockerfile Pattern
Multi-stage build for optimized production images:
dockerfile# Dockerfile.prod # Stage 1: Dependencies FROM node:22-alpine AS deps WORKDIR /app COPY package*.json ./ RUN npm ci --only=production # Stage 2: Build FROM node:22-alpine AS builder WORKDIR /app COPY --from=deps /app/node_modules ./node_modules COPY . . RUN npm run build # Stage 3: Run FROM node:22-alpine AS runner WORKDIR /app ENV NODE_ENV=production # Security: non-root user RUN addgroup --system --gid 1001 nodejs RUN adduser --system --uid 1001 nextjs USER nextjs COPY --from=builder /app/public ./public COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./ COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static EXPOSE 3000 CMD ["node", "server.js"]
Workflow Commands
Prompting AI for Container-Aware Code
When asking AI to generate code, include container context:
Container-Aware Request
Include container context in AI requests
I'm running in a Docker container (node:22-alpine).
I need to implement {{feature}}.
Constraints:
- Use only Alpine-compatible packages
- Assume the app runs as non-root user
- Environment variables come from docker-compose
- File paths should be container-relative (/app/...)
Please account for these in your implementation.Common Docker Issues
| Issue | Cause | Fix |
|---|---|---|
| Hot reload not working | Volume not mounted | Check docker-compose volumes |
| Permission denied | Root/non-root mismatch | Use same UID in container |
| Module not found | node_modules not in volume | Add /app/node_modules exclude |
| Build cache stale | Dockerfile layer caching | Run --no-cache |
Container Security for AI Work
Danger
Never give AI tools direct access to Docker daemon or privileged containers. AI-generated code runs inside containers, not alongside them.
Safety checklist:
- Non-root user in containers
- No --privileged flag
- Minimal base images (alpine)
- No secrets in Dockerfile
- Use .dockerignore
.dockerignore Best Practices
# .dockerignore
node_modules
.next
.git
.env
.env.local
*.md
Dockerfile*
docker-compose*
Verify
Your containerized setup is correct if:
-
docker-compose upstarts the dev server - Code changes trigger hot reload
-
docker-compose downcleans up completely - Production image is under 200MB
- Container runs as non-root
Next: Integrate AI tools with your IDE for seamless development.