From d710f958b37f34a2844b0a1ee5b1037b8f9fffd9 Mon Sep 17 00:00:00 2001 From: Valentin Heiserer Date: Sat, 30 Aug 2025 14:25:26 +0200 Subject: [PATCH] feat: update Docker configuration for combined frontend and backend build process --- .dockerignore | 6 +-- .github/workflows/BuildAndPushContainer.yml | 10 +++-- Dockerfile | 46 +++++++++++++++++++++ frontend/docker-compose.yml | 2 +- 4 files changed, 57 insertions(+), 7 deletions(-) create mode 100644 Dockerfile diff --git a/.dockerignore b/.dockerignore index 00f3b21..a8de1bd 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,12 +1,12 @@ +yarn-error.log node_modules **/node_modules npm-debug.log yarn-error.log -dist tmp temp .git .gitignore -Dockerfile* -frontend/dist backend/coverage +# Keep built frontend out of context cache noise but allow source +frontend/dist diff --git a/.github/workflows/BuildAndPushContainer.yml b/.github/workflows/BuildAndPushContainer.yml index e3ab808..6f65e7d 100644 --- a/.github/workflows/BuildAndPushContainer.yml +++ b/.github/workflows/BuildAndPushContainer.yml @@ -8,7 +8,7 @@ on: env: REGISTRY: ghcr.io - IMAGE_NAME: ${{ github.repository }}/backend + IMAGE_NAME: ${{ github.repository }}/app jobs: build-and-push: @@ -47,12 +47,16 @@ jobs: - name: Build and push Docker image (multi-arch) uses: docker/build-push-action@v6 with: - context: ./frontend - file: ./frontend/Dockerfile + context: . + file: ./Dockerfile push: true platforms: linux/amd64,linux/arm64 tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} + build-args: | + NODE_VERSION=20-bookworm-slim + cache-from: type=gha + cache-to: type=gha,mode=max - name: Notify updater (GET, bearer) if: ${{ success() && github.ref == 'refs/heads/main' }} diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..1326629 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,46 @@ +######## Root Combined Frontend + Backend Build ######## +## Build args (can be overridden) +ARG NODE_VERSION=20-bookworm-slim + +######################## +# 1) Frontend build # +######################## +FROM node:${NODE_VERSION} AS frontend-build +WORKDIR /frontend +COPY frontend/package*.json ./ +RUN npm install --no-audit --no-fund +COPY frontend/ . +RUN npm run build + +######################## +# 2) Backend build # +######################## +FROM node:${NODE_VERSION} AS backend-build +WORKDIR /app +COPY backend/package*.json ./ +RUN npm install --no-audit --no-fund +COPY backend/ . +# Copy built frontend assets +COPY --from=frontend-build /frontend/dist ./public +RUN npm run build + +######################## +# 3) Production image # +######################## +FROM node:${NODE_VERSION} AS runner +ENV NODE_ENV=production +WORKDIR /app + +# Install production deps +COPY backend/package*.json ./ +RUN npm install --omit=dev --no-audit --no-fund + +# Copy backend dist + public +COPY --from=backend-build /app/dist ./dist +COPY --from=backend-build /app/public ./public + +ENV PORT=3000 \ + SERIAL_BAUD=115200 + +EXPOSE 3000 +CMD ["node", "dist/main.js"] \ No newline at end of file diff --git a/frontend/docker-compose.yml b/frontend/docker-compose.yml index aa1a928..ca62b1c 100644 --- a/frontend/docker-compose.yml +++ b/frontend/docker-compose.yml @@ -2,7 +2,7 @@ services: app: build: context: .. - dockerfile: frontend/Dockerfile + dockerfile: Dockerfile container_name: schafkop-app restart: unless-stopped environment: