FROM golang:1.23-alpine # 1. Install git and create the user 1000 RUN apk add --no-cache git && \ adduser -D -u 1000 user # 2. FIX PERMISSIONS: Give user 1000 ownership of the Go Cache # (This was the missing piece causing the crash) RUN mkdir -p /go && \ chown -R user:user /go # 3. Setup workspace with permissions WORKDIR /app RUN chown user:user /app # 4. Copy your main.go COPY --chown=user main.go ./ # 5. Create the magic script RUN echo '#!/bin/sh' > /run_script.sh && \ echo 'set -e' >> /run_script.sh && \ # Create module if missing echo 'if [ ! -f go.mod ]; then' >> /run_script.sh && \ echo ' echo "Initializing module..."' >> /run_script.sh && \ echo ' go mod init app' >> /run_script.sh && \ echo ' echo "Downloading dependencies..."' >> /run_script.sh && \ echo ' go mod tidy' >> /run_script.sh && \ echo 'fi' >> /run_script.sh && \ # Run the app echo 'echo "Running app..."' >> /run_script.sh && \ echo 'exec go run .' >> /run_script.sh && \ chmod +x /run_script.sh # 6. Switch to non-root user USER user # 7. Start ENTRYPOINT ["/run_script.sh"]