Development vs production Dockerfiles - Performance Comparison
We want to understand how building Docker images changes as the project size grows.
How does the time to build differ between development and production Dockerfiles?
Analyze the time complexity of these two Dockerfile snippets.
# Development Dockerfile
FROM node:18
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
CMD ["npm", "run", "dev"]
# Production Dockerfile
FROM node:18
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
RUN npm run build
CMD ["node", "dist/index.js"]
These Dockerfiles set up development and production environments differently.
Look for steps that repeat or take time depending on input size.
- Primary operation: Copying files and running npm install or build commands.
- How many times: Each command runs once per build, but their cost depends on project size.
As the project files increase, copying and installing take longer.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 files | Fast copy and install, quick build |
| 100 files | Copy and install take noticeably longer |
| 1000 files | Copying and building take much longer |
Pattern observation: Time grows roughly in proportion to the number of files and dependencies.
Time Complexity: O(n)
This means build time grows roughly in a straight line as project size increases.
[X] Wrong: "Development and production Dockerfiles take the same time to build regardless of project size."
[OK] Correct: Production builds often run extra steps like building code, which add time as the project grows.
Understanding how build time scales helps you write efficient Dockerfiles and manage development workflows smoothly.
What if we added caching layers to the Dockerfiles? How would the time complexity change?
Practice
Solution
Step 1: Understand development Dockerfile purpose
Development Dockerfiles include debugging tools and full dependencies to help developers test and debug easily.Step 2: Understand production Dockerfile purpose
Production Dockerfiles are optimized for smaller size, speed, and security, removing unnecessary tools.Final Answer:
Because development needs debugging tools and production needs optimized images -> Option AQuick Check:
Separate Dockerfiles fit different environment needs [OK]
- Thinking production needs more debugging tools
- Assuming development images must be smaller
- Believing production always includes all source code
Solution
Step 1: Identify correct Dockerfile syntax for base image
The correct keyword to specify a base image is 'FROM', followed by the image name and tag.Step 2: Choose a lightweight base image for production
For production, 'node:alpine' is a lightweight image suitable for smaller, optimized containers.Final Answer:
FROM node:alpine -> Option DQuick Check:
Use 'FROM' and lightweight image for production [OK]
- Using 'BASE' instead of 'FROM'
- Incorrect image name order like 'alpine:node'
- Using 'latest' tag for production images
FROM node:alpine WORKDIR /app COPY package.json ./ RUN npm install --production COPY . . CMD ["node", "server.js"]
What is the main effect of the
npm install --production command?Solution
Step 1: Understand npm install --production flag
The '--production' flag tells npm to install only dependencies needed for running the app, skipping devDependencies used for development.Step 2: Relate to production Dockerfile purpose
Skipping devDependencies reduces image size and improves security for production environments.Final Answer:
Installs only production dependencies, skipping devDependencies -> Option CQuick Check:
npm install --production = only production deps [OK]
- Assuming it installs all dependencies
- Thinking it installs no dependencies
- Confusing devDependencies with production dependencies
Solution
Step 1: Identify cause of large production image
If the production image is large, it may include unnecessary development tools and dependencies.Step 2: Check which Dockerfile was used for production build
Using the development Dockerfile for production will include extra tools, increasing image size.Final Answer:
You used the development Dockerfile to build the production image -> Option AQuick Check:
Wrong Dockerfile causes large production image [OK]
- Assuming missing source code causes large image
- Thinking smaller base image increases size
- Confusing npm install flags in development
Solution
Step 1: Identify multi-stage build structure
The first stage uses a full Node.js image (node:latest) for building with all tools, suitable for development.Step 2: Use lightweight Alpine image for final production stage
The second stage uses node:alpine to copy only necessary files from build stage, creating a smaller production image.Step 3: Verify correct order and commands
FROM node:latest AS build WORKDIR /app COPY . . RUN npm install FROM node:alpine COPY --from=build /app . CMD ["node", "server.js"] correctly uses 'AS build' for first stage and copies from it to the lightweight image.Final Answer:
FROM node:latest AS build WORKDIR /app COPY . . RUN npm install FROM node:alpine COPY --from=build /app . CMD ["node", "server.js"] -> Option BQuick Check:
Multi-stage: full image build + alpine production [OK]
- Using alpine for build stage losing dev tools
- Using latest image for final production stage
- Not using multi-stage COPY syntax
Ready to prove it?
Pass the test and this pattern moves unknown
