Skip to content
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

Merged
merged 2 commits into from
Dec 12, 2024

Conversation

nwangfw
Copy link
Collaborator

@nwangfw nwangfw commented Dec 10, 2024

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

  • PR title includes appropriate prefix(es)
  • Changes are clearly explained in the PR description
  • New and existing tests pass successfully
  • Code adheres to project style and best practices
  • Documentation updated to reflect changes (if applicable)
  • Thorough testing completed, no regressions introduced

By submitting this PR, you confirm that you've read these guidelines and your changes align with the project's contribution standards.

@nwangfw nwangfw force-pushed the ning/issue-496-reduce-runtime-image-size branch from 46caae4 to ed71aa0 Compare December 10, 2024 21:20
@nwangfw nwangfw changed the title Reduced runtime's container image size [Misc] Reduced runtime's container image size Dec 10, 2024
@nwangfw nwangfw force-pushed the ning/issue-496-reduce-runtime-image-size branch 3 times, most recently from e64bb94 to 992107c Compare December 10, 2024 22:37
@nwangfw nwangfw force-pushed the ning/issue-496-reduce-runtime-image-size branch from 992107c to df3d1c2 Compare December 10, 2024 22:45
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/
Copy link
Collaborator

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

Copy link
Collaborator Author

@nwangfw nwangfw Dec 11, 2024

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

Copy link
Collaborator

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

Copy link
Collaborator

@Jeffwan Jeffwan Dec 12, 2024

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.

image

I combine commands into 1 and get results below
image

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)

Copy link
Collaborator Author

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!

@nwangfw nwangfw force-pushed the ning/issue-496-reduce-runtime-image-size branch from a22fef9 to 76e67eb Compare December 12, 2024 16:17
Copy link
Collaborator

@Jeffwan Jeffwan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm

@Jeffwan Jeffwan merged commit 264f507 into main Dec 12, 2024
7 checks passed
@Jeffwan Jeffwan deleted the ning/issue-496-reduce-runtime-image-size branch December 12, 2024 17:23
gangmuk pushed a commit that referenced this pull request Jan 25, 2025
* multi-stage builds

* merge install wheel and delete wheel into one step
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Reduce runtime's container image size
2 participants