-
Notifications
You must be signed in to change notification settings - Fork 267
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Misc] Reduced runtime's container image size #518
Conversation
46caae4
to
ed71aa0
Compare
e64bb94
to
992107c
Compare
992107c
to
df3d1c2
Compare
build/container/Dockerfile.runtime
Outdated
WORKDIR /app | ||
|
||
# Copy only the Python packages from builder | ||
COPY --from=builder /usr/local/lib/python${PYTHON_VERSION}/site-packages/ /usr/local/lib/python${PYTHON_VERSION}/site-packages/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel this is kind of hacky. Can we build the aibrix pythong package and copy that one and install? Instead of copying all the python path, I feel that way is much more clean and reliable
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great Suggesetions, Thanks! The new image built in the below way is a little bit larger. Let me know if you have any way to further optimize the below Dockerfile.
ARG PYTHON_VERSION=3.11
ARG BASE_IMAGE=python:${PYTHON_VERSION}-slim-bookworm
# Builder stage
FROM ${BASE_IMAGE} AS builder
WORKDIR /app
ARG POETRY_VERSION=1.8.3
# Install dependencies
RUN apt-get update \
&& apt-get install -y python3-dev build-essential \
&& apt-get clean && \
rm -rf /var/lib/apt/lists/*
# Install Poetry
RUN python3 -m pip install poetry==${POETRY_VERSION}
# Copy the runtime source
COPY python/aibrix/poetry.lock python/aibrix/pyproject.toml python/aibrix/ /app/
# Install dependencies and build package
RUN poetry config virtualenvs.create false \
&& poetry install --no-interaction --no-dev \
&& poetry build -f wheel
# Final stage
FROM ${BASE_IMAGE}
WORKDIR /app
# Install build dependencies temporarily
RUN apt-get update \
&& apt-get install -y --no-install-recommends gcc python3-dev \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
# Copy only the built wheel and install with dependencies
COPY --from=builder /app/dist/*.whl ./
RUN pip install --no-cache-dir ./*.whl \
&& rm -f ./*.whl \
&& apt-get purge -y gcc python3-dev \
&& apt-get autoremove -y
# Set entrypoint for Runtime
ENTRYPOINT ["aibrix_runtime"]
Here is a comparison of three different approaches:
Current image size (2.53 GB), image size built by this new method (1.74 GB), image size built by the hacky method (1.13 GB)
Which way do you think is the best now? @Jeffwan
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's have a short discussion in office @nwangfw
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The tricky part is the docker images is build layer by layer. you have three parts separately. Even you install & remove the gcc, python-dev later but it still take one layer instead. We either need to use 3rd party tools to squash the layers or put them in the same layer.
I combine commands into 1 and get results below
Even it's still a little bit larger than PYTHONPATH version, I think it's acceptable. (Technically, we can make it close by removing all cache files but that's change Dockerfile a lot which is not necessary at this moment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for helping me figure it out! The modified Dockerfile looks very close to PYTHONPATH version!
a22fef9
to
76e67eb
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/lgtm
* multi-stage builds * merge install wheel and delete wheel into one step
Pull Request Description
Enabled multi-stage docker builds and the runtime image is reduced to 1.13 GB.
Github Action Installation-test is also updated to cover the Dockerfile changes.
Related Issues
Resolves: #496
Important: Before submitting, please complete the description above and review the checklist below.
Contribution Guidelines (Expand for Details)
We appreciate your contribution to aibrix! To ensure a smooth review process and maintain high code quality, please adhere to the following guidelines:
Pull Request Title Format
Your PR title should start with one of these prefixes to indicate the nature of the change:
[Bug]
: Corrections to existing functionality[CI]
: Changes to build process or CI pipeline[Docs]
: Updates or additions to documentation[API]
: Modifications to aibrix's API or interface[CLI]
: Changes or additions to the Command Line Interface[Misc]
: For changes not covered above (use sparingly)Note: For changes spanning multiple categories, use multiple prefixes in order of importance.
Submission Checklist
By submitting this PR, you confirm that you've read these guidelines and your changes align with the project's contribution standards.