From b4746cf18f4ec43e8a8f2faa0fa522aef44e2518 Mon Sep 17 00:00:00 2001 From: Nikolai Papin Date: Tue, 17 Jun 2025 21:34:55 +0300 Subject: [PATCH] feat: health check for backend in docker-compose; feat: go dockerfile preinstalls curl; refactor: reduced go image size by using a 2 stage dockerfile; refactor: switch frontend dockerfile --- backend/Dockerfile | 26 ++++++++++++++--- backend/internal/controllers/service.go | 2 +- docker-compose.yml | 7 +++++ frontend/Dockerfile | 37 +++++++++++++++---------- 4 files changed, 53 insertions(+), 19 deletions(-) diff --git a/backend/Dockerfile b/backend/Dockerfile index 89ef4e6..ae371c1 100644 --- a/backend/Dockerfile +++ b/backend/Dockerfile @@ -1,12 +1,30 @@ -FROM golang:1.24 +# Stage 1: Build the binary +FROM golang:1.24 AS builder -WORKDIR /usr/src/app +WORKDIR /app +# Copy dependencies and download COPY go.mod go.sum ./ RUN go mod download +# Copy source and build static binary COPY . . -RUN go build -v -o /usr/local/bin/app ./... +RUN CGO_ENABLED=0 go build -ldflags="-s -w" -o /main ./cmd/main.go -CMD ["app"] +# Stage 2: Lightweight runtime with tools +FROM debian:bookworm-slim +# Install curl, shell (sh), and SSL certificates +RUN apt-get update && \ + apt-get install -y --no-install-recommends curl ca-certificates && \ + rm -rf /var/lib/apt/lists/* + +# Create non-root user and set ownership +RUN groupadd -r appuser && useradd -r -g appuser appuser +USER appuser + +# Copy compiled binary +COPY --from=builder /main /main + +# Execute the binary +CMD ["/main"] diff --git a/backend/internal/controllers/service.go b/backend/internal/controllers/service.go index d5382ca..b74227c 100644 --- a/backend/internal/controllers/service.go +++ b/backend/internal/controllers/service.go @@ -7,5 +7,5 @@ import ( ) func HealthCheck(c *gin.Context) { - c.JSON(http.StatusOK, gin.H{"status": "ok"}) + c.JSON(http.StatusOK, gin.H{"healthy": true}) } diff --git a/docker-compose.yml b/docker-compose.yml index 5c5e9ed..3e526ed 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,6 +3,13 @@ services: backend: build: ./backend restart: unless-stopped + healthcheck: + test: ["CMD", "/usr/bin/curl", "-f", "http://localhost:8080/service/health"] + interval: 30s + timeout: 10s + retries: 3 + ports: + - "8080:8080" networks: - easywish-network diff --git a/frontend/Dockerfile b/frontend/Dockerfile index 66ec030..ffa22d6 100644 --- a/frontend/Dockerfile +++ b/frontend/Dockerfile @@ -1,16 +1,25 @@ -FROM node:18-alpine AS builder -WORKDIR /app -COPY package*.json . -RUN npm ci -COPY . . -RUN npm run build -RUN npm prune --production +#Dockerfile -FROM node:18-alpine +# Use this image as the platform to build the app +FROM node:20.11-alpine AS external-website + +# The WORKDIR instruction sets the working directory for everything that will happen next WORKDIR /app -COPY --from=builder /app/build build/ -COPY --from=builder /app/node_modules node_modules/ -COPY package.json . -EXPOSE 3000 -ENV NODE_ENV=production -CMD [ "node", "build" ] + +# Copy all local files into the image +COPY . . + +# Clean install all node modules +RUN npm ci + +# Build SvelteKit app +RUN npm run build + +# Delete source code files that were used to build the app that are no longer needed +RUN rm -rf src/ + +# The USER instruction sets the user name to use as the default user for the remainder of the current stage +USER node:node + +# This is the command that will be run inside the image when you tell Docker to start the container +CMD ["node","build/index.js"]