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.
Release files for hubtel-ai 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| hubtel_ai-0.1.0.tar.gz | 9.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| hubtel_ai-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 24.4 kB
Release files / hubtel_ai-0.1.0.tar.gz
| Download URL | hubtel_ai-0.1.0.tar.gz |
|---|---|
| Size | 9.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
acfd34f38217a7bad8040760b0d390eb1f653165592ca6fcb93125d75b6125bd
|
|
BLAKE2b-256 checksum How to use checksums |
d25309400476e720c36ea7c857cd43b9ae850ee0e45ed024bc4ff810040ee079
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.7.1 CPython/3.11.6 Linux/5.10.102.1-microsoft-standard-WSL2
|
Release files / hubtel_ai-0.1.0-py3-none-any.whl
| Download URL | hubtel_ai-0.1.0-py3-none-any.whl |
|---|---|
| Size | 15.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b2c0ce962413daf0eab7c92fc1fa96c26122b92fcf0210138614a3a2f0dc441d
|
|
BLAKE2b-256 checksum How to use checksums |
f697f29bf5c299a2c19f5fa9a52c990a79336c9bc803dcada9e5ce45dce0666c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.7.1 CPython/3.11.6 Linux/5.10.102.1-microsoft-standard-WSL2
|