Docker Installation
Learn to containerize your applications for AWS deployment
💵 Cost: Docker Desktop is FREE for personal use and small businesses (<250 employees, <$10M revenue). Larger organizations require a paid subscription. Docker itself has no AWS costs until you deploy.
What is Docker?
Docker packages your application and all its dependencies into a container - a standardized unit that runs the same everywhere. Think of it as a lightweight, portable virtual machine.
Why Containers?
"It works on my machine" becomes "It works everywhere" because the container includes everything needed to run your app.
Installation
Download Docker Desktop for Mac:
Double-click the .dmg file and drag Docker to Applications. Launch Docker from your Applications folder.
Verify Installation
Docker version 24.0.7, build afdd53b
Hello from Docker! This message shows that your installation appears to be working correctly.
Dockerfile for Next.js
Choose between a simple Dockerfile for learning or a production-ready version for real deployments:
A minimal Dockerfile to understand the basics. Good for learning, not for production.
# Build stage
FROM node:20-alpine AS builder
WORKDIR /app
# Install dependencies
COPY package*.json ./
RUN npm ci
# Build the application
COPY . .
RUN npm run build
# Production stage
FROM node:20-alpine AS runner
WORKDIR /app
ENV NODE_ENV=production
# Create non-root user for security
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
# Copy built application
COPY --from=builder /app/public ./public
COPY --from=builder /app/.next/standalone ./
COPY --from=builder /app/.next/static ./.next/static
USER nextjs
EXPOSE 3000
ENV PORT=3000
ENV HOSTNAME="0.0.0.0"
CMD ["node", "server.js"]Missing for Production
- No health check for container orchestration
- No Alpine compatibility fix (
libc6-compat) - Single-stage install (less cache optimization)
- No npm cache cleanup (larger image)
Standalone Output Required
For both Dockerfiles above to work, add this to your next.config.ts:
const nextConfig = {
output: 'standalone',
}
export default nextConfigBuild and Test Locally
Visit http://localhost:3000 to see your containerized app running!
Key Docker Commands
| Command | Purpose |
|---|---|
docker build -t name . | Build an image from a Dockerfile |
docker run -p 3000:3000 name | Run a container with port mapping |
docker images | List all images |
docker ps | List running containers |
docker stop [id] | Stop a running container |