From a95989b929946560e701ffff4282ac87307cc2e1 Mon Sep 17 00:00:00 2001 From: Dinika Saxena Date: Sun, 28 Apr 2024 21:31:58 +0200 Subject: [PATCH] Build image for raspberry pis --- Dockerfile.prod | 37 +++++++++++++++++++++++++++++++++++++ build.sh | 24 +++++++++++++++++++----- 2 files changed, 56 insertions(+), 5 deletions(-) create mode 100644 Dockerfile.prod diff --git a/Dockerfile.prod b/Dockerfile.prod new file mode 100644 index 0000000..f09fbc6 --- /dev/null +++ b/Dockerfile.prod @@ -0,0 +1,37 @@ +FROM docker.io/arm64v8/python:3-slim as builder + +RUN pip install poetry + +ENV POETRY_NO_INTERACTION=1 \ + POETRY_VIRTUALENVS_IN_PROJECT=1 \ + POETRY_VIRTUALENVS_CREATE=1 \ + POETRY_CACHE_DIR=/tmp/expenses-cache + +WORKDIR /app + +COPY src/pyproject.toml src/poetry.lock ./ + +RUN --mount=type=cache,target=$POETRY_CACHE_DIR poetry install --without dev --no-root + +FROM docker.io/arm64v8/python:3-slim as runtime + +ENV VIRTUAL_ENV=/app/.venv \ + PATH="/app/.venv/bin:$PATH" + + +COPY --from=builder ${VIRTUAL_ENV} ${VIRTUAL_ENV} +WORKDIR /app + + +COPY ./src/expenses_server /app/expenses_server +COPY ./src/alembic /app/alembic +COPY ./src/alembic.ini /app/ +COPY ./src/static /app/static + +COPY ./docker-entrypoint.sh /app/ + +ENV FRONTEND_PATH=/app/static + +EXPOSE 8000 + +ENTRYPOINT ["./docker-entrypoint.sh"] diff --git a/build.sh b/build.sh index 166393b..476c641 100755 --- a/build.sh +++ b/build.sh @@ -27,7 +27,11 @@ function help() { Path to the directory containing the backend server code. Example: - ./build.sh ../expenses-react ../expenses_server_fastapi x86 + # For personal laptops (usually x86 architecture) + ./build.sh ../expenses-react ../expenses_server_fastapi x86_64 + + # or the following to build the image for the raspberry pi + ./build.sh ../expenses-react ../expenses_server_fastapi arm64v8 EOH } @@ -52,7 +56,14 @@ function main() { fi local fe_path="${1:?No frontend path passed}" local be_path="${2:?No backend path passed}" - local arch="${3:-}" + local env="${3:-}" + if [[ "$env" == "prod" ]]; then + arch="arm64v8" + dockerfile="./Dockerfile.prod" + else + arch="x86_64" + dockerfile="./Dockerfile" + fi if $do_build; then cleanup @@ -64,16 +75,19 @@ function main() { copyFrontend "$fe_path" "src/static" fi - buildContainer "$arch" + buildContainer "$arch" "$dockerfile" } function buildContainer() { local arch="${1:-}" - docker build \ + local dockerfile="${2:-}" + + podman build \ --tag "expenses-app:dev-latest" \ + --arch "${arch}" \ + -f "${dockerfile}" \ . - # ${arch:+--arch=$arch} \ } function cleanup() {