File size: 1,131 Bytes
96477a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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"]