Dockerfile.server 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # Wails Server Mode Dockerfile
  2. # Multi-stage build for a minimal image.
  3. #
  4. # Defaults produce a pure-Go, fully static binary on a distroless/static base:
  5. # the smallest, most secure image, and correct for the default (CGO-free) app.
  6. #
  7. # If your app needs CGO, override the build args, e.g.:
  8. # docker build --build-arg CGO_ENABLED=1 \
  9. # --build-arg GO_IMAGE=golang:bookworm \
  10. # --build-arg RUNTIME_IMAGE=gcr.io/distroless/base-debian12 \
  11. # -f build/docker/Dockerfile.server .
  12. # CGO needs a C toolchain in the builder and a libc in the runtime image; keep
  13. # the builder and runtime libc compatible (a glibc builder -> a glibc runtime).
  14. # The Taskfile passes these through: task build:docker CGO_ENABLED=1 GO_IMAGE=... RUNTIME_IMAGE=...
  15. ARG GO_IMAGE=golang:alpine
  16. ARG RUNTIME_IMAGE=gcr.io/distroless/static-debian12
  17. ARG CGO_ENABLED=0
  18. # Build stage
  19. FROM ${GO_IMAGE} AS builder
  20. ARG CGO_ENABLED
  21. WORKDIR /app
  22. # Install build dependencies: git always; a C compiler only when CGO is enabled.
  23. # Supports both apk (alpine) and apt (debian/bookworm) base images.
  24. RUN if command -v apk >/dev/null 2>&1; then \
  25. apk add --no-cache git $([ "$CGO_ENABLED" = "1" ] && echo gcc musl-dev); \
  26. else \
  27. apt-get update && apt-get install -y --no-install-recommends git $([ "$CGO_ENABLED" = "1" ] && echo gcc libc6-dev) && rm -rf /var/lib/apt/lists/*; \
  28. fi
  29. # Copy source code
  30. COPY . .
  31. # Remove local replace directive if present (for production builds)
  32. RUN sed -i '/^replace/d' go.mod || true
  33. # Download dependencies
  34. RUN go mod tidy
  35. # Build the production server binary.
  36. # - server,production tags: HTTP server mode with production code paths (no dev
  37. # logger/asset middleware), matching the desktop production build.
  38. # - CGO_ENABLED defaults to 0 for a fully static binary (distroless/static);
  39. # override with --build-arg CGO_ENABLED=1 (see header) for CGO apps.
  40. RUN CGO_ENABLED=${CGO_ENABLED} go build -tags server,production -trimpath -buildvcs=false -ldflags="-s -w" -o server .
  41. # Runtime stage - minimal image
  42. FROM ${RUNTIME_IMAGE}
  43. # Copy the binary
  44. COPY --from=builder /app/server /server
  45. # Copy frontend assets
  46. COPY --from=builder /app/frontend/dist /frontend/dist
  47. # Expose the default port
  48. EXPOSE 8080
  49. # Bind to all interfaces (required for Docker)
  50. # Can be overridden at runtime with -e WAILS_SERVER_HOST=...
  51. ENV WAILS_SERVER_HOST=0.0.0.0
  52. # Run the server
  53. ENTRYPOINT ["/server"]