From e5155c8a42775a695d0b9f791bbadde11e460964 Mon Sep 17 00:00:00 2001 From: x0gp Date: Mon, 6 Jul 2026 22:04:28 +0400 Subject: [PATCH] feat(server): add Dockerfile.fast and scripts for fast host-compiled builds - Add Dockerfile.fast which packages a pre-built binary instead of compiling inside the container. - Add scripts/build-fast.ps1 (PowerShell) and scripts/build-fast.sh (Bash) to automate local cross-compilation (CGO_ENABLED=0 GOOS=linux) and Docker packaging. - Set executable bit for build-fast.sh. --- server/Dockerfile.fast | 19 ++++++++++++++ server/scripts/build-fast.ps1 | 49 +++++++++++++++++++++++++++++++++++ server/scripts/build-fast.sh | 27 +++++++++++++++++++ 3 files changed, 95 insertions(+) create mode 100644 server/Dockerfile.fast create mode 100644 server/scripts/build-fast.ps1 create mode 100755 server/scripts/build-fast.sh diff --git a/server/Dockerfile.fast b/server/Dockerfile.fast new file mode 100644 index 0000000..9b0d65c --- /dev/null +++ b/server/Dockerfile.fast @@ -0,0 +1,19 @@ +# Dockerfile.fast — Fast packaging of a pre-compiled local binary +FROM alpine:3.19 + +# Add CA certificates and timezone data +RUN apk --no-cache add ca-certificates tzdata + +WORKDIR /app + +# Copy the pre-compiled binary from the host +ARG BINARY_PATH +COPY ${BINARY_PATH} /app/poc-server + +# Set execution permissions +RUN chmod +x /app/poc-server + +# Expose server ports +EXPOSE 8080 + +ENTRYPOINT ["/app/poc-server"] diff --git a/server/scripts/build-fast.ps1 b/server/scripts/build-fast.ps1 new file mode 100644 index 0000000..bec2a23 --- /dev/null +++ b/server/scripts/build-fast.ps1 @@ -0,0 +1,49 @@ +# PowerShell script for fast Docker build using host compilation +param ( + [string]$Arch = "arm64", + [string]$Tag = "x0gp-server:latest" +) + +$ErrorActionPreference = "Stop" + +# Navigate to the server root directory if not already there +$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path +$ServerDir = Split-Path -Parent $ScriptDir +Push-Location $ServerDir + +try { + Write-Host "Building Go binary for OS=linux, ARCH=$Arch..." -ForegroundColor Cyan + + # Setup temporary binary path + $BinPath = "bin/poc-server-linux-$Arch" + if (!(Test-Path "bin")) { + New-Item -ItemType Directory -Path "bin" | Out-Null + } + + # Configure cross-compilation environment variables + $env:CGO_ENABLED = "0" + $env:GOOS = "linux" + $env:GOARCH = $Arch + + # Compile + go build -ldflags="-s -w" -o $BinPath ./cmd/poc-server + + Write-Host "Go binary successfully compiled: $BinPath" -ForegroundColor Green + Write-Host "Building Docker image '$Tag'..." -ForegroundColor Cyan + + # Run docker build + docker build --build-arg BINARY_PATH=$BinPath -t $Tag -f Dockerfile.fast . + + Write-Host "`nDocker image '$Tag' built successfully!" -ForegroundColor Green +} +catch { + Write-Host "Error occurred: $_" -ForegroundColor Red + exit 1 +} +finally { + # Restore environment variables + Remove-Item env:CGO_ENABLED -ErrorAction SilentlyContinue + Remove-Item env:GOOS -ErrorAction SilentlyContinue + Remove-Item env:GOARCH -ErrorAction SilentlyContinue + Pop-Location +} diff --git a/server/scripts/build-fast.sh b/server/scripts/build-fast.sh new file mode 100755 index 0000000..b51641c --- /dev/null +++ b/server/scripts/build-fast.sh @@ -0,0 +1,27 @@ +#!/usr/bin/env bash +# Bash script for fast Docker build using host compilation + +set -e + +ARCH=${1:-"arm64"} +TAG=${2:-"x0gp-server:latest"} + +# Navigate to the server root directory +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +SERVER_DIR="$(dirname "$SCRIPT_DIR")" +cd "$SERVER_DIR" + +echo "Building Go binary for OS=linux, ARCH=${ARCH}..." +mkdir -p bin +BIN_PATH="bin/poc-server-linux-${ARCH}" + +# Compile locally +CGO_ENABLED=0 GOOS=linux GOARCH=${ARCH} go build -ldflags="-s -w" -o "${BIN_PATH}" ./cmd/poc-server + +echo "Go binary successfully compiled: ${BIN_PATH}" +echo "Building Docker image '${TAG}'..." + +# Build docker image +docker build --build-arg BINARY_PATH="${BIN_PATH}" -t "${TAG}" -f Dockerfile.fast . + +echo -e "\nDocker image '${TAG}' built successfully!"