From ae8c6c88a964cd2eddd6d23827ea0172300e68c1 Mon Sep 17 00:00:00 2001 From: Alicia Sykes Date: Mon, 15 Sep 2025 19:02:26 +0100 Subject: [PATCH] Dockerized app --- .github/workflows/docker.yml | 98 ++++++++++++++++++++++++++++++++ Dockerfile | 41 +++++++++++++ docker-compose.yml | 18 ++++++ src/routes/dns/zone/+page.svelte | 4 +- src/routes/health/+server.ts | 8 +++ 5 files changed, 167 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/docker.yml create mode 100644 Dockerfile create mode 100644 docker-compose.yml create mode 100644 src/routes/health/+server.ts diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml new file mode 100644 index 0000000..1242416 --- /dev/null +++ b/.github/workflows/docker.yml @@ -0,0 +1,98 @@ +name: 🐳 Build & Push Docker Image + +on: + push: + branches: [main] + tags: ['v*'] + workflow_dispatch: + inputs: + tag: + description: 'Custom tag to publish' + required: false + type: string + dry_run: + description: 'Build without pushing' + required: false + default: false + type: boolean + +env: + REPO_NAME: ${{ vars.REPO_NAME || github.repository }} + +jobs: + build: + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + attestations: write + id-token: write + + steps: + - name: 🛎️ Checkout + uses: actions/checkout@v4 + + - name: 🔌 Set up QEMU + uses: docker/setup-qemu-action@v3 + + - name: 🔌 Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: 🏷️ Determine tags + id: tags + run: | + TAGS="" + + if [[ "${{ github.event_name }}" == "workflow_dispatch" && -n "${{ inputs.tag }}" ]]; then + # Manual dispatch with custom tag + TAG="${{ inputs.tag }}" + TAGS="docker.io/${{ env.REPO_NAME }}:${TAG},ghcr.io/${{ env.REPO_NAME }}:${TAG}" + elif [[ "${{ github.ref_type }}" == "tag" ]]; then + # Git tag push + TAG="${{ github.ref_name }}" + TAG="${TAG#v}" # Remove v prefix + TAGS="docker.io/${{ env.REPO_NAME }}:${TAG},ghcr.io/${{ env.REPO_NAME }}:${TAG}" + elif [[ "${{ github.ref }}" == "refs/heads/main" ]]; then + # Main branch push + TAGS="docker.io/${{ env.REPO_NAME }}:latest,ghcr.io/${{ env.REPO_NAME }}:latest" + fi + + echo "tags=${TAGS}" >> $GITHUB_OUTPUT + echo "Publishing tags: ${TAGS}" + + - name: 🔑 Login to Docker Hub + if: steps.tags.outputs.tags != '' && vars.DOCKER_USERNAME != '' + uses: docker/login-action@v3 + with: + username: ${{ vars.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_TOKEN }} + continue-on-error: true + + - name: 🔑 Login to GitHub Container Registry + if: steps.tags.outputs.tags != '' + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: 🔨 Build and push + if: steps.tags.outputs.tags != '' + uses: docker/build-push-action@v6 + with: + context: . + platforms: linux/amd64,linux/arm64 + push: ${{ !inputs.dry_run }} + tags: ${{ steps.tags.outputs.tags }} + cache-from: type=gha + cache-to: type=gha,mode=max + labels: | + org.opencontainers.image.source=${{ github.server_url }}/${{ github.repository }} + org.opencontainers.image.revision=${{ github.sha }} + org.opencontainers.image.created=${{ github.event.head_commit.timestamp }} + + - name: ⚠️ Warning if skipped + if: steps.tags.outputs.tags == '' + run: | + echo "::warning::No tags determined - skipping Docker build" + echo "This happens when the trigger doesn't match main branch, git tag, or manual dispatch with tag" diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..23e6a99 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,41 @@ +# syntax=docker/dockerfile:1.7 + +FROM node:22-alpine AS deps +WORKDIR /app +COPY package*.json ./ +RUN --mount=type=cache,target=/root/.npm \ + npm ci --omit=dev + +FROM node:22-alpine AS builder +WORKDIR /app +COPY package*.json ./ +RUN --mount=type=cache,target=/root/.npm \ + npm ci +# Copy only what we need; rely on .dockerignore for the rest +COPY . . +ENV DEPLOY_ENV=docker NODE_ENV=production +RUN npm run build + +FROM node:22-alpine AS runner +# tini for proper init (optional; remove if you use --init at runtime) +RUN apk add --no-cache tini +WORKDIR /app +ENV NODE_ENV=production PORT=3000 HOST=0.0.0.0 + +# Non-root user +RUN addgroup -g 1001 -S nodejs && adduser -S sveltekit -u 1001 + +# Copy runtime artifacts +COPY --from=builder --chown=sveltekit:nodejs /app/build ./build +COPY --from=builder --chown=sveltekit:nodejs /app/package.json ./ +COPY --from=deps --chown=sveltekit:nodejs /app/node_modules ./node_modules + +USER sveltekit +EXPOSE 3000 + +# Optional: add a lightweight health endpoint in your app (e.g., /health) +HEALTHCHECK --interval=30s --timeout=3s --start-period=10s --retries=3 \ + CMD wget -qO- http://127.0.0.1:3000/health || exit 1 + +ENTRYPOINT ["/sbin/tini","--"] +CMD ["node","build"] diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..f6fcf08 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,18 @@ +version: '3.8' + +services: + app: + image: lissy93/networking-toolbox:latest + ports: + - "3000:3000" + environment: + - NODE_ENV=production + - PORT=3000 + - HOST=0.0.0.0 + restart: unless-stopped + healthcheck: + test: ["CMD", "wget", "-qO-", "http://localhost:3000/health"] + interval: 30s + timeout: 10s + retries: 3 + start_period: 40s \ No newline at end of file diff --git a/src/routes/dns/zone/+page.svelte b/src/routes/dns/zone/+page.svelte index b53cb96..f17aea5 100644 --- a/src/routes/dns/zone/+page.svelte +++ b/src/routes/dns/zone/+page.svelte @@ -4,7 +4,7 @@ const zoneTools = (SUB_NAV['/dns']?.find(group => typeof group === 'object' && 'title' in group && group.title === 'Zone File Tools' - ) as any)?.items || []; + ))?.items || []; @@ -382,4 +382,4 @@ grid-template-columns: 1fr; } } - \ No newline at end of file + diff --git a/src/routes/health/+server.ts b/src/routes/health/+server.ts new file mode 100644 index 0000000..30534ec --- /dev/null +++ b/src/routes/health/+server.ts @@ -0,0 +1,8 @@ +import { json } from '@sveltejs/kit'; + +export async function GET() { + return json({ + status: 'healthy', + timestamp: new Date().toISOString() + }); +} \ No newline at end of file