0
0
Dockerdevops~10 mins

Multi-stage for different environments in Docker - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Multi-stage for different environments
Start: Base Stage
Build Stage: Compile/Build
Test Stage: Run Tests
Production Stage: Final Image
Select Stage Based on Environment
Dev Image
Prod Image
Test Image
End
The Dockerfile builds multiple stages for dev, test, and prod environments, then selects the final image based on the environment.
Execution Sample
Docker
FROM node:18 AS base
WORKDIR /app
COPY package.json ./
RUN npm install

FROM base AS build
COPY . ./
RUN npm run build

FROM base AS test
COPY . ./
RUN npm test

FROM base AS prod
COPY --from=build /app/dist ./dist
CMD ["node", "dist/server.js"]
This Dockerfile creates separate stages for building, testing, and production, reusing the base stage.
Process Table
StepStageActionFiles/StateResult
1baseSet working directory and copy package.json/app/package.jsonpackage.json copied
2baseRun npm install/app/node_modulesDependencies installed
3buildCopy all source files/app/src + othersSource files copied
4buildRun npm run build/app/distBuild output created
5testCopy all source files/app/src + othersSource files copied
6testRun npm testTest resultsTests passed
7prodCopy build output from build stage/app/distProduction files ready
8prodSet CMD to start serverCMD setImage ready for production
💡 All stages completed; final image chosen based on environment variable.
Status Tracker
Variablebasebuildtestprod
Working Directory/app/app/app/app
package.jsoncopiedinheritedinheritedinherited
node_modulesinstalledinheritedinheritedinherited
Source Filesnonecopiedcopiednone
Build Output (dist)nonecreatednonecopied from build
Test Resultsnonenonecreatednone
CMDnonenonenoneset to start server
Key Moments - 3 Insights
Why do we copy source files again in build and test stages if base already has dependencies?
Base stage only installs dependencies; source files must be copied separately in build and test stages to run build and tests. See execution_table rows 3 and 5.
How does the prod stage get the build output without rebuilding?
Prod stage copies the build output from the build stage using 'COPY --from=build'. This avoids rebuilding. See execution_table row 7.
What decides which stage becomes the final image?
The final image is selected by specifying the target stage during docker build (e.g., --target prod). This is why all stages are built but only one is used.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step are the tests run?
AStep 6
BStep 4
CStep 7
DStep 2
💡 Hint
Check the 'Action' column for 'Run npm test' in the execution_table.
According to variable_tracker, which stage has the 'CMD' set to start the server?
Atest
Bbuild
Cprod
Dbase
💡 Hint
Look at the 'CMD' row in variable_tracker for the 'prod' column.
If we skip copying source files in the build stage, what will happen?
ABuild output will be created normally
BBuild stage will fail or produce empty output
CTests will pass anyway
DProd stage will copy source files automatically
💡 Hint
Refer to execution_table rows 3 and 4 where source files are copied before build.
Concept Snapshot
Multi-stage Dockerfiles let you create separate build steps for dev, test, and prod.
Use 'FROM ... AS stage' to name stages.
Copy files and run commands per stage.
Use 'COPY --from=stage' to reuse outputs.
Select final image with 'docker build --target'.
This keeps images small and environment-specific.
Full Transcript
This visual execution shows how a multi-stage Dockerfile builds different environments: base, build, test, and prod. The base stage sets up dependencies. Build and test stages copy source files and run build or tests. The prod stage copies the build output from build and sets the command to start the server. Variables like working directory, files, and commands change per stage. The final image is chosen by specifying the target stage during build. This approach keeps images efficient and environment-specific.