Hubtel AI is a CLI tool for managing your Hubtel AI deployment projects.
Project description
Introduction
AWS Serverless Application Model (SAM)
Docker
Let's dive deeper into the concept of multi-stage builds in Docker, especially focusing on how it's beneficial for creating optimized Docker images, such as for AWS Lambda functions.
The Basics of Multi-Stage Builds
Multi-stage builds in Docker are designed to optimize the process of building Docker images, especially for applications that require a compilation or build step. Here's a step-by-step explanation of how it works:
-
Multiple FROM Statements: A typical Dockerfile starts with a
FROMinstruction to specify the base image. In multi-stage builds, you can use multipleFROMinstructions. EachFROMinstruction begins a new stage of the build. -
Build Stage: In the initial stages, you might use a larger image with all the necessary tools and dependencies to compile or build your application. This stage is where you perform actions like downloading dependencies, compiling code, running tests, etc.
-
Runtime Stage: After the build stage, you start a new stage with a lighter base image, often just containing the runtime environment necessary to run your application (like a slimmed-down version of a Python or Node.js image).
-
Copying Artifacts: You then copy only the necessary artifacts (like compiled binaries, specific libraries, or other required files) from the build stage to the runtime stage. This step is crucial as it allows you to include only what your application needs to run, excluding all the build-time tools and dependencies.
-
Resulting Image: The final image that comes out of the last stage is much smaller than it would be if it contained all the build tools and intermediate artifacts. This image is what you deploy.
Example with More Details
Let’s expand on the previous Python Lambda function example with more details:
# Stage 1: Build stage
FROM python:3.11 AS build-stage
WORKDIR /build
COPY requirements.txt .
RUN pip install --target=/build/python/lib/python3.11/site-packages -r requirements.txt
# Stage 2: Runtime stage
FROM public.ecr.aws/lambda/python:3.11 AS runtime-stage
COPY --from=build-stage /build/python /var/runtime
COPY src/ /var/task/
CMD ["app.handler"]
Explanation
-
Build Stage:
- We start with a full Python image (
python:3.11) because it contains all the necessary tools for compiling Python packages. - The dependencies are defined in
requirements.txtand are installed in a custom directory (/build/python/lib/python3.11/site-packages).
- We start with a full Python image (
-
Runtime Stage:
- We then switch to a lighter base image specifically provided by AWS for Python Lambda (
public.ecr.aws/lambda/python:3.11). - We copy only the installed packages from the build stage. No build tools or source code are copied, just the compiled packages.
- The application source code is located in the
src/directory and is copied directly into the runtime stage.
- We then switch to a lighter base image specifically provided by AWS for Python Lambda (
Benefits in a Lambda Context
In the context of AWS Lambda:
- Reduced Size: The final Docker image is smaller, which translates to quicker upload times, faster Lambda deployment, and potentially reduced cold start times.
- Security: Minimizing the contents of the image reduces the potential attack surface as there are fewer components that could contain vulnerabilities.
- Clean Environment: The runtime environment contains only what is necessary for running the application, making it easier to manage and understand.
Conclusion
Multi-stage builds are particularly useful when you need to compile or process code and then run it in a different, usually leaner, environment. This technique aligns well with best practices in Docker image creation, ensuring efficient, secure, and maintainable container images.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file hubtel_ai-0.1.0.tar.gz.
File metadata
- Download URL: hubtel_ai-0.1.0.tar.gz
- Upload date:
- Size: 9.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.11.6 Linux/5.10.102.1-microsoft-standard-WSL2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
acfd34f38217a7bad8040760b0d390eb1f653165592ca6fcb93125d75b6125bd
|
|
| MD5 |
1735573f6a5ec584c1139b69364789a6
|
|
| BLAKE2b-256 |
d25309400476e720c36ea7c857cd43b9ae850ee0e45ed024bc4ff810040ee079
|
File details
Details for the file hubtel_ai-0.1.0-py3-none-any.whl.
File metadata
- Download URL: hubtel_ai-0.1.0-py3-none-any.whl
- Upload date:
- Size: 15.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.11.6 Linux/5.10.102.1-microsoft-standard-WSL2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b2c0ce962413daf0eab7c92fc1fa96c26122b92fcf0210138614a3a2f0dc441d
|
|
| MD5 |
8eb26870a19a761af214d6e4bf72be8e
|
|
| BLAKE2b-256 |
f697f29bf5c299a2c19f5fa9a52c990a79336c9bc803dcada9e5ce45dce0666c
|