Containerize applications efficiently with Docker, multi-stage builds, and security best practices.
# Docker Containerization Best Practices
Master Docker containerization with Google Antigravity IDE. This comprehensive guide covers multi-stage builds, security hardening, and production optimization for containerized applications.
## Why Docker?
Docker provides consistent development and deployment environments. Google Antigravity IDE's Gemini 3 engine offers intelligent Dockerfile optimization and security recommendations.
## Multi-Stage Dockerfile
```dockerfile
# Stage 1: Dependencies
FROM node:20-alpine AS deps
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production && npm cache clean --force
# Stage 2: Build
FROM node:20-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
# Stage 3: Production
FROM node:20-alpine AS runner
WORKDIR /app
ENV NODE_ENV=production
ENV PORT=3000
# Security: Run as non-root user
RUN addgroup --system --gid 1001 nodejs && \
adduser --system --uid 1001 nextjs
# Copy only necessary files
COPY --from=deps /app/node_modules ./node_modules
COPY --from=builder /app/.next/standalone ./
COPY --from=builder /app/.next/static ./.next/static
COPY --from=builder /app/public ./public
# Set correct permissions
RUN chown -R nextjs:nodejs /app
USER nextjs
EXPOSE 3000
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s \
CMD wget --no-verbose --tries=1 --spider http://localhost:3000/api/health || exit 1
CMD ["node", "server.js"]
```
## Docker Compose Configuration
```yaml
# docker-compose.yml
version: "3.9"
services:
app:
build:
context: .
dockerfile: Dockerfile
target: runner
ports:
- "3000:3000"
environment:
- DATABASE_URL=postgresql://postgres:password@db:5432/app
- REDIS_URL=redis://redis:6379
depends_on:
db:
condition: service_healthy
redis:
condition: service_started
restart: unless-stopped
networks:
- app-network
db:
image: postgres:16-alpine
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: password
POSTGRES_DB: app
volumes:
- postgres_data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 5s
timeout: 5s
retries: 5
networks:
- app-network
redis:
image: redis:7-alpine
command: redis-server --appendonly yes
volumes:
- redis_data:/data
networks:
- app-network
volumes:
postgres_data:
redis_data:
networks:
app-network:
driver: bridge
```
## Security Hardening
```dockerfile
# .dockerignore
node_modules
.git
.env*
*.md
Dockerfile*
docker-compose*
.dockerignore
coverage
.nyc_output
```
```dockerfile
# Security-focused Dockerfile additions
FROM node:20-alpine AS runner
# Install security updates
RUN apk update && apk upgrade && \
apk add --no-cache dumb-init && \
rm -rf /var/cache/apk/*
# Prevent privilege escalation
RUN chmod u-s /usr/bin/* && \
chmod g-s /usr/bin/*
# Use dumb-init for proper signal handling
ENTRYPOINT ["/usr/bin/dumb-init", "--"]
CMD ["node", "server.js"]
```
## Build Optimization
```bash
# Build with BuildKit for better caching
DOCKER_BUILDKIT=1 docker build \
--cache-from myapp:latest \
--build-arg BUILDKIT_INLINE_CACHE=1 \
-t myapp:latest .
# Multi-platform builds
docker buildx build \
--platform linux/amd64,linux/arm64 \
-t myapp:latest \
--push .
```
## Health Checks
```typescript
// src/api/health/route.ts
export async function GET() {
const checks = {
database: await checkDatabase(),
redis: await checkRedis(),
memory: process.memoryUsage(),
};
const healthy = checks.database && checks.redis;
return Response.json(
{ status: healthy ? "healthy" : "unhealthy", checks },
{ status: healthy ? 200 : 503 }
);
}
```
## Best Practices
- Use multi-stage builds to minimize image size
- Run containers as non-root users
- Implement proper health checks
- Use .dockerignore to exclude unnecessary files
- Pin base image versions for reproducibility
- Scan images for vulnerabilities with Trivy
Google Antigravity IDE provides Docker configuration templates and automatically detects security issues in your containerization setup.This Docker prompt is ideal for developers working on:
By using this prompt, you can save hours of manual coding and ensure best practices are followed from the start. It's particularly valuable for teams looking to maintain consistency across their docker implementations.
Yes! All prompts on Antigravity AI Directory are free to use for both personal and commercial projects. No attribution required, though it's always appreciated.
This prompt works excellently with Claude, ChatGPT, Cursor, GitHub Copilot, and other modern AI coding assistants. For best results, use models with large context windows.
You can modify the prompt by adding specific requirements, constraints, or preferences. For Docker projects, consider mentioning your framework version, coding style, and any specific libraries you're using.