-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(docker): reduce image size (#363)
- Loading branch information
Showing
3 changed files
with
62 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Ignore git related files | ||
.github/ | ||
.gitignore | ||
|
||
# Ignore folders not necessary for the docker image | ||
docs/ | ||
tests/ | ||
|
||
# Ignore venv if any | ||
venv/ | ||
|
||
# Ignore files not necessary for the docker image | ||
CONTRIBUTING.md | ||
*.png | ||
*.json | ||
LICENSE | ||
Makefile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,51 @@ | ||
ARG PY_VERSION=3.12 | ||
|
||
#--------------------------------------------------------------------------------------- | ||
# Stage 1 → Builder image | ||
#--------------------------------------------------------------------------------------- | ||
FROM python:$PY_VERSION-slim AS build-env | ||
|
||
ARG VERSION | ||
|
||
WORKDIR /app | ||
|
||
# Install python deps | ||
RUN python -m pip install --upgrade poetry wheel twine | ||
RUN apt-get update && apt-get install -y --no-install-recommends build-essential \ | ||
&& rm -rf /var/lib/apt/lists/* | ||
RUN python -m pip install --no-cache-dir --upgrade poetry wheel twine | ||
|
||
# Install project deps | ||
COPY pyproject.toml . | ||
RUN poetry install --with dev | ||
COPY pyproject.toml poetry.lock ./ | ||
RUN poetry install --with dev --no-root | ||
|
||
# Copy code *after* installing deps to avoid unnecessarily invalidating cache | ||
COPY . . | ||
|
||
# Build the project | ||
RUN poetry build | ||
RUN PROJECT_VERSION=$(poetry version -s) && cp /app/dist/boaviztapi-$PROJECT_VERSION.tar.gz ./boaviztapi-$VERSION.tar.gz | ||
RUN pip install boaviztapi-$VERSION.tar.gz && cp $(which uvicorn) /app | ||
RUN PROJECT_VERSION=$(poetry version -s) && \ | ||
cp /app/dist/boaviztapi-$PROJECT_VERSION.tar.gz ./boaviztapi-$VERSION.tar.gz | ||
|
||
#--------------------------------------------------------------------------------------- | ||
# Stage 2 → Runtime image | ||
#--------------------------------------------------------------------------------------- | ||
FROM python:$PY_VERSION-slim AS run-env | ||
# Python 3 surrogate unicode handling | ||
# @see https://click.palletsprojects.com/en/7.x/python3/ | ||
ENV LC_ALL=C.UTF-8 | ||
ENV LANG=C.UTF-8 | ||
|
||
COPY --from=build-env /app /app | ||
COPY --from=build-env /usr/local/lib/python3.12/site-packages /usr/local/lib/python3.12/site-packages | ||
ENV PYTHONPATH=/usr/local/lib/python3.12/site-packages | ||
ENV LC_ALL=C.UTF-8 \ | ||
LANG=C.UTF-8 | ||
|
||
ARG VERSION | ||
WORKDIR /app | ||
|
||
# Copy executable and dependencies | ||
COPY --from=build-env /app/boaviztapi-$VERSION.tar.gz /app/ | ||
RUN pip install --no-cache-dir /app/boaviztapi-$VERSION.tar.gz | ||
|
||
# Required in main.py | ||
COPY --from=build-env /app/pyproject.toml /usr/local/lib/python3.12/site-packages/boaviztapi/ | ||
|
||
# Copy uvicorn executable | ||
RUN pip install --no-cache-dir uvicorn | ||
|
||
EXPOSE 5000 | ||
CMD ["./uvicorn", "boaviztapi.main:app", "--host", "0.0.0.0", "--port", "5000"] | ||
CMD ["uvicorn", "boaviztapi.main:app", "--host", "0.0.0.0", "--port", "5000"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters