Module 8 Lesson 5: Dev vs. Prod Dockerfiles
The right tool for the right job. Learn how to maintain separate Docker configurations for development (speed and debugging) and production (security and size).
Module 8 Lesson 5: Dev vs. Prod Dockerfiles
You shouldn't use the exact same Docker setup for Development and Production. Their goals are different.
| Goal | Development | Production |
|---|---|---|
| Speed | Instant startup (Hot reload) | Faster downloads (Small size) |
| Visibility | Verbose logs & Debuggers | Silent & Secure |
| Permissions | "Root" is fine for speed | Non-Root only |
| Tools | Includes Compilers & Linters | Minimal app only |
1. Approach A: Two Separate Files
Perfect for simpler projects.
Dockerfile.dev: Starts with a full base image, usesnpm startorpip watch.Dockerfile: Uses multi-stage builds and aslimbase for production.
Usage:
# To run locally
docker build -f Dockerfile.dev .
2. Approach B: Multi-Stage "Targets" (The Pro Way)
You can use a single Dockerfile with different "End States" (Targets).
# 1. Base Layer
FROM node:18-alpine AS base
WORKDIR /app
COPY package*.json ./
RUN npm install
# 2. Development Stage
FROM base AS development
COPY . .
CMD ["npm", "run", "dev"]
# 3. Production Stage
FROM base AS production
COPY . .
RUN npm run build
RUN rm -rf src/ # Cleanup
USER node
CMD ["npm", "start"]
Usage:
# Build only up to the 'development' stage
docker build --target development -t app:dev .
# Build everything up to the 'production' stage
docker build --target production -t app:prod .
3. Best Practices Table
| Feature | Dev Config | Prod Config |
|---|---|---|
| Source Code | Bind Mounted | COPY instruction |
| Secrets | .env file | Docker Secrets |
| Base Image | Regular (e.g. node:18) | Alpine/Slim (e.g. node:18-slim) |
| Caching | Aggressive | Security-focused |
| User | Root (usually) | Non-root |
Exercise: The Environmental Audit
- Look at your project's current Dockerfile. Which goal is it optimized for: Dev or Prod?
- Identify three things in that file that are "Unsafe" for production (e.g., source code inside, running as root).
- Rewrite it using the Multi-Stage Targets approach described above.
- Why is it better to use "Targets" in one file rather than having two separate files? (Hint: Think about maintaining the shared
RUN npm installinstructions).
Conclusion of Module 8
You have completed the Development Deep Dive. You now know how to build a local environment that is fast, consistent, and easily bridgeable to production.
Next Module: The advanced storage layer: Module 9: Docker Volumes in Depth.