Apps are not easy forever. More features mean more dependent, slow construction, and heavy docar images. This is where things begin to hurt.
The Dokar helps, but without the right setup, your construction flourishes rapidly.
Multi -stage bloods make their pictures smooth, clean, clean and prepared for production. In this guide, how would you use them to spare your Dokar workflow.
Let’s enter it.
Provisions
You should be following this guide, you should be:
Doer is installed and running
The basic understanding of the Doker
The knowledge of some Azigar (or any language, really)
Familiar with the terminal
Here’s what we will cover is:
What are Dokar Images?
Before we seek correction, let’s quickly become clear what the Dokar images are actually.
A Dokar image is a lightweight, standstone package that requires everything to run your app – code, dependence, environmental variables, and files. Think of it as a snapshot of your app, ready to rotate anywhere.
When you run an image, the Dokar turns it into a container: a self -made environment that treats the same on your machine, staging or production. It is a huge win for consistency and deployment.
Now that we have the basics, let’s talk about making these images smaller and faster.
How to enforce multi -phase construction
Let’s get your hands by creating a basic flask app and using a multi -stage blood to keep your Dokar image thin.
Step 1: Create app.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return "Hello, Docker Multi-stage Builds! 🐳"
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
Step 2: Install and save dependent
Install Flask and Gnicorn using PIP:
pip install flask gunicorn
Then freeze your environment a requirements.txt
File:
pip freeze > requirements.txt
This file is what the Doker will use to install dependence within your container.
Step 3: Make a multi -stage Dockerfile
FROM python:3.9-slim AS builder
WORKDIR /app
COPY requirements.txt .
RUN python -m venv /opt/venv && \\
. /opt/venv/bin/activate && \\
pip install --no-cache-dir -r requirements.txt
FROM python:3.9-slim
COPY --from=builder /opt/venv /opt/venv
WORKDIR /app
COPY . .
ENV PATH="/opt/venv/bin/:$PATH"
EXPOSE 5000
CMD ("gunicorn", "--bind", "0.0.0.0:5000", "app:app")
In the aforementioned postal post, we have explained both the development and production phase for our application. The first step, Phase to makeUses python:3.9-slim
Base image, sets a working directory, adds all the necessary files, and creates virtual environment. All dependence within this virtual environment is installed.
I Production phaseWe start again python:3.9-slim
But this time we copy the virtual environment from the blood stage with the application code only. We then create the environment to use this virtual environment and run the app using Gankorin.
Now, in multi-stage blood, you may experience using different versions in different stages-but here’s why I didn’t go on this path:
In some packages, depending on the version of Azigar, different depends.
My
requirements.txt
The file depends on the version, so staying on the same version at the same stages helps avoid compatibility issues.
Once the multi -stage doker file is ready, go ahead and make pictures. You will clearly see the size difference.
Step 4: Make your icon and run
To make and run your image container, use the following command:
docker build -t my-python-app .
docker run -p 5000:5000 my-python-app
If everything works correctly, your flask app should be alive now In your browser
When the Dokar is completed without mistakes and starts a container, you will know that your construction has succeeded. You should look at the terminal logs from Gancorn, which shows that the app is ready and running.
Chunky Single Stage Blood
Let’s compare with a traditional One Stage Dokar Blood that includes everything in one goes:
FROM python:3.9-slim
WORKDIR /app
RUN apt-get update && apt-get install -y \\
build-essential \\
python3-dev \\
gcc \\
&& rm -rf /var/lib/apt/lists/*
COPY requirements.txt .
RUN python -m venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
EXPOSE 5000
CMD ("gunicorn", "--bind", "0.0.0.0:5000", "app:app")
The aforementioned dok file uses a straightforward process: it starts with python:3.9-slim
The image determines a working directory, installs dependence on the system, produces virtual environment, installs packages, exposes copies on the app code, exposes port 5000, and runs the app using Gankorine. This type of doer file is common and it works well, but it can produce unnecessarily large and fola images.
Let’s prepare your icon to compare the size of the multi -stage blood:
docker build -t my-chunky-app .
You will see that it takes longer to build this Dokar file than the previous one, which was very fast.
Before we continue, confirm that your Dokar image was successfully created.
Now, let’s compare the Blood Size:
docker images | grep 'my-'
If you are wondering why we used “mine” to find the pictures, the reason is that we have named our Dokar Images my-python-app
And my-chunky-app
Using “mine” as a key word makes it easier to filter them.
The aforementioned photo compares the blood size of our single stage and multi -stage Doker Images. As you can see, my-python-app
-Military stage is blood-small and lightweight, while my-chunky-app
Significantly bigger. If you dig a little deep, you will see that the multi -stage image is made in just 1.2 seconds, while the only phase I completed in 1 minute and 21 seconds. Very impressive difference, okay?
In my opinion, these are the solid reasons for using a multi-phase construction-but it is not always necessary. There are cases where the construction of the same phase has more meaning. Let’s take a look at them.
When use multi -stage bloods
Use multi -stage bloods if:
Your app needs to make tools (eg, compiler, giant dependent)
You want a small, sharp doker photos
You care about the image security and performance
Use single phase construction if:
You are just testing or typing prototy
Your app is small and does not need external tools
You are still learning the basics
Choose the scale and complexity of your project.
Conclusion
Multi -stage bloods are an easy win. They help keep your Dokar images clean, fast and safe – especially as your app grows.
Every project does not need them, but when you do, they make a lot of difference. So the next time you are decorating something serious, reach the multi -stage. Your future will thank you yourself.