generated from br3ndonland/template-python
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #54 from br3ndonland/buildkit
Use BuildKit for Docker builds
- Loading branch information
Showing
4 changed files
with
110 additions
and
19 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
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,5 +1,6 @@ | ||
# syntax=docker/dockerfile:1 | ||
ARG PYTHON_VERSION=3.10 LINUX_VERSION= | ||
FROM python:${PYTHON_VERSION}${LINUX_VERSION:+-$LINUX_VERSION} AS base | ||
FROM python:${PYTHON_VERSION}${LINUX_VERSION:+-$LINUX_VERSION} AS builder | ||
LABEL org.opencontainers.image.authors="Brendon Smith <[email protected]>" | ||
LABEL org.opencontainers.image.description="Docker images and utilities to power your Python APIs and help you ship faster." | ||
LABEL org.opencontainers.image.licenses="MIT" | ||
|
@@ -8,21 +9,59 @@ LABEL org.opencontainers.image.title="inboard" | |
LABEL org.opencontainers.image.url="https://github.com/br3ndonland/inboard/pkgs/container/inboard" | ||
ARG LINUX_VERSION PIPX_VERSION=1.1.0 POETRY_VERSION=1.1.11 | ||
ENV APP_MODULE=inboard.app.main_base:app LINUX_VERSION=$LINUX_VERSION PATH=/opt/pipx/bin:/app/.venv/bin:$PATH PIPX_BIN_DIR=/opt/pipx/bin PIPX_HOME=/opt/pipx/home PIPX_VERSION=$PIPX_VERSION POETRY_VERSION=$POETRY_VERSION PYTHONPATH=/app | ||
COPY poetry.lock poetry.toml pyproject.toml /app/ | ||
COPY --link poetry.lock poetry.toml pyproject.toml /app/ | ||
WORKDIR /app | ||
RUN sh -c 'if [ "$LINUX_VERSION" = "slim" ]; then apt-get update -qy && apt-get install -qy --no-install-recommends gcc libc-dev make wget; fi' && \ | ||
sh -c '. /etc/os-release; if [ "$ID" = "alpine" ]; then apk add --no-cache --virtual .build-deps gcc libc-dev libffi-dev make openssl-dev; fi' && \ | ||
python -m pip install --no-cache-dir --upgrade pip "pipx==$PIPX_VERSION" && pipx install "poetry==$POETRY_VERSION" && poetry install --no-dev --no-interaction --no-root && \ | ||
sh -c 'if [ "$LINUX_VERSION" = "slim" ]; then apt-get purge --auto-remove -qy gcc libc-dev make wget; fi' && \ | ||
sh -c '. /etc/os-release; if [ "$ID" = "alpine" ]; then apk del .build-deps; fi' | ||
COPY inboard /app/inboard | ||
RUN <<HEREDOC | ||
. /etc/os-release | ||
if [ "$ID" = "alpine" ]; then | ||
apk add --no-cache --virtual .build-deps \ | ||
gcc libc-dev libffi-dev make openssl-dev | ||
elif [ "$LINUX_VERSION" = "slim" ]; then | ||
apt-get update -qy | ||
apt-get install -qy --no-install-recommends \ | ||
gcc libc-dev make wget | ||
fi | ||
python -m pip install --no-cache-dir --upgrade pip "pipx==$PIPX_VERSION" | ||
pipx install "poetry==$POETRY_VERSION" | ||
poetry install --no-dev --no-interaction --no-root | ||
HEREDOC | ||
COPY --link inboard /app/inboard | ||
ENTRYPOINT ["python"] | ||
CMD ["-m", "inboard.start"] | ||
|
||
FROM base AS fastapi | ||
FROM builder as base | ||
RUN <<HEREDOC | ||
. /etc/os-release | ||
if [ "$ID" = "alpine" ]; then | ||
apk del .build-deps | ||
elif [ "$LINUX_VERSION" = "slim" ]; then | ||
apt-get purge --auto-remove -qy \ | ||
gcc libc-dev make wget | ||
fi | ||
HEREDOC | ||
|
||
FROM builder AS fastapi | ||
ENV APP_MODULE=inboard.app.main_fastapi:app | ||
RUN poetry install --no-dev --no-interaction --no-root -E fastapi | ||
RUN <<HEREDOC | ||
poetry install --no-dev --no-interaction --no-root -E fastapi | ||
. /etc/os-release | ||
if [ "$ID" = "alpine" ]; then | ||
apk del .build-deps | ||
elif [ "$LINUX_VERSION" = "slim" ]; then | ||
apt-get purge --auto-remove -qy \ | ||
gcc libc-dev make wget | ||
fi | ||
HEREDOC | ||
|
||
FROM base AS starlette | ||
FROM builder AS starlette | ||
ENV APP_MODULE=inboard.app.main_starlette:app | ||
RUN poetry install --no-dev --no-interaction --no-root -E starlette | ||
RUN <<HEREDOC | ||
poetry install --no-dev --no-interaction --no-root -E starlette | ||
. /etc/os-release | ||
if [ "$ID" = "alpine" ]; then | ||
apk del .build-deps | ||
elif [ "$LINUX_VERSION" = "slim" ]; then | ||
apt-get purge --auto-remove -qy \ | ||
gcc libc-dev make wget | ||
fi | ||
HEREDOC |
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 |
---|---|---|
|
@@ -167,13 +167,17 @@ docker cp [container_name]:/path/to/file destination.file | |
|
||
### Building development images | ||
|
||
Note that Docker builds use BuildKit. See the [BuildKit docs](https://github.com/moby/buildkit/blob/HEAD/frontend/dockerfile/docs/syntax.md) and [Docker docs](https://docs.docker.com/develop/develop-images/build_enhancements/). | ||
|
||
To build the Docker images for each stage: | ||
|
||
```sh | ||
git clone [email protected]:br3ndonland/inboard.git | ||
cd inboard | ||
export DOCKER_BUILDKIT=1 | ||
docker build . --rm --target base -t localhost/br3ndonland/inboard:base && \ | ||
docker build . --rm --target fastapi -t localhost/br3ndonland/inboard:fastapi && \ | ||
docker build . --rm --target starlette -t localhost/br3ndonland/inboard:starlette | ||
|
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