Skip to content

Commit

Permalink
add docker image optimization (#3727)
Browse files Browse the repository at this point in the history
* add docker image optimization
* flip copy for even more caching
* fix pyproject install bug
  • Loading branch information
fliepeltje committed Jul 12, 2024
1 parent 6a6b554 commit 7c8638f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 23 deletions.
1 change: 1 addition & 0 deletions scanner/templates/python-docker/.dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ fly.toml
.git/
__pycache__/
.envrc
.venv/
35 changes: 12 additions & 23 deletions scanner/templates/python-docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,45 +1,34 @@
FROM python:{{ .pyVersion }} AS builder
FROM python:{{ .pyVersion }}-bookworm AS builder

ENV PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1

WORKDIR /app
COPY . .

{{ if .pipenv -}}

ENV PIPENV_VENV_IN_PROJECT=1 \
PIPENV_CUSTOM_VENV_NAME=.venv
RUN pip install pipenv
COPY Pipfile Pipfile.lock ./
RUN pipenv install

{{ else if .poetry -}}

RUN pip install poetry
RUN poetry config virtualenvs.in-project true
COPY pyproject.toml poetry.lock ./
RUN poetry install

{{ else if .pep621 -}}

RUN python -m venv .venv && \
. .venv/bin/activate && \
pip install --upgrade pip && \
pip install .

RUN python -m venv .venv
COPY pyproject.toml ./
RUN .venv/bin/pip install .
{{ else if .pip }}

RUN python -m venv .venv && \
. .venv/bin/activate && \
pip install --upgrade pip && \
pip install -r requirements.txt

RUN python -m venv .venv
COPY requirements.txt ./
RUN .venv/bin/pip install -r requirements.txt
{{ end -}}

FROM python:{{ .pyVersion }}-slim

FROM python:{{ .pyVersion }}-slim-bookworm
WORKDIR /app
COPY --from=builder /app .

COPY --from=builder /app/.venv .venv/
COPY . .
{{ if .flask -}}
CMD ["/app/.venv/bin/flask", "run", "--host=0.0.0.0", "--port=8080"]
{{ else if .fastapi -}}
Expand Down

0 comments on commit 7c8638f

Please sign in to comment.