0
0
Dockerdevops~30 mins

Multi-stage for different environments in Docker - Mini Project: Build & Apply

Choose your learning style9 modes available
Multi-stage Dockerfile for Different Environments
📖 Scenario: You are working on a simple web application that needs to be built and deployed in two different environments: development and production. Each environment requires a different setup inside the Docker image.In development, you want to include debugging tools and keep the source code inside the image. In production, you want a smaller image with only the built application and no extra tools.
🎯 Goal: Create a multi-stage Dockerfile that builds the application in a builder stage, then creates two separate final images: one for development with debugging tools and source code, and one for production with only the built app.
📋 What You'll Learn
Create a builder stage that copies source code and builds the app
Create a development stage that copies from builder and adds debugging tools
Create a production stage that copies only the built app from builder
Use multi-stage syntax with FROM and AS keywords
Print the final Dockerfile content
💡 Why This Matters
🌍 Real World
Multi-stage Dockerfiles are used in real projects to create optimized images for different environments, saving space and improving security.
💼 Career
Understanding multi-stage builds is important for DevOps roles to manage container images efficiently and support continuous integration and deployment pipelines.
Progress0 / 4 steps
1
Create the builder stage
Write the first stage of the Dockerfile named builder that uses the python:3.12-slim image. Copy the source code folder src/ into /app inside the container and set the working directory to /app. Then run the command pip install -r requirements.txt to install dependencies.
Docker
Need a hint?

Use FROM python:3.12-slim AS builder to start the builder stage.

Remember to copy the src/ folder and set the working directory.

2
Add the development stage
Add a new stage named development that starts from python:3.12-slim. Copy everything from the builder stage's /app directory into /app in this stage. Then install the debugging tool ipdb by running pip install ipdb. Set the working directory to /app.
Docker
Need a hint?

Use COPY --from=builder /app /app to copy files from the builder stage.

Install ipdb with pip install ipdb.

3
Add the production stage
Add a new stage named production that starts from python:3.12-slim. Copy only the built application files from the builder stage's /app directory into /app. Set the working directory to /app. Do not install debugging tools here.
Docker
Need a hint?

Start the production stage with FROM python:3.12-slim AS production.

Copy files from the builder stage but do not install extra packages.

4
Print the complete Dockerfile
Print the entire Dockerfile content as a single string exactly as written in the previous steps.
Docker
Need a hint?

Use a print statement to output the entire Dockerfile content as a string.