Module 8 Lesson 5: Dev vs. Prod Dockerfiles
·DevOps

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.

GoalDevelopmentProduction
SpeedInstant startup (Hot reload)Faster downloads (Small size)
VisibilityVerbose logs & DebuggersSilent & Secure
Permissions"Root" is fine for speedNon-Root only
ToolsIncludes Compilers & LintersMinimal app only

1. Approach A: Two Separate Files

Perfect for simpler projects.

  • Dockerfile.dev: Starts with a full base image, uses npm start or pip watch.
  • Dockerfile: Uses multi-stage builds and a slim base 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

FeatureDev ConfigProd Config
Source CodeBind MountedCOPY instruction
Secrets.env fileDocker Secrets
Base ImageRegular (e.g. node:18)Alpine/Slim (e.g. node:18-slim)
CachingAggressiveSecurity-focused
UserRoot (usually)Non-root

Exercise: The Environmental Audit

  1. Look at your project's current Dockerfile. Which goal is it optimized for: Dev or Prod?
  2. Identify three things in that file that are "Unsafe" for production (e.g., source code inside, running as root).
  3. Rewrite it using the Multi-Stage Targets approach described above.
  4. Why is it better to use "Targets" in one file rather than having two separate files? (Hint: Think about maintaining the shared RUN npm install instructions).

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.

Subscribe to our newsletter

Get the latest posts delivered right to your inbox.

Subscribe on LinkedIn