-
Notifications
You must be signed in to change notification settings - Fork 5
/
Dockerfile
55 lines (38 loc) · 1.66 KB
/
Dockerfile
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# Use a specific version of the Golang image for the build stage
FROM golang:1.22.2-alpine AS builder
# Install git, necessary for fetching the Go modules and source code
RUN apk --no-cache add git
# Set the working directory inside the container
WORKDIR /app
# Copy your local source code into the image
COPY . .
# Adjust permissions to ensure all files in /app are accessible by 'nobody'
# Set ownership of all files in the /app directory to 'nobody'
RUN chown -R nobody:nobody /app
# Create a directory for the build cache that the 'nobody' user can access
RUN mkdir /.cache && chown nobody:nobody /.cache
# Add a non-root user and switch to it
USER nobody
# Set environment variable for Go cache directory
ENV GOCACHE=/.cache/go-build
# Run go mod tidy to ensure all dependencies are correct and go.sum is updated based on your go.mod
RUN go mod tidy
# Download all dependencies. Dependencies will be cached if the go.mod and go.sum files are not changed
RUN go mod download
# Build the application. Consider simplifying build flags if debug information is not an issue or necessary for troubleshooting
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o electronwall .
# Switch to a new stage using Alpine to keep the final image small
FROM alpine:latest
# Install Bash in the final image
RUN apk add --no-cache bash
# Create a user 'appuser' and switch to it
RUN adduser -D appuser
USER appuser
# Set work directory to /app
WORKDIR /app
# Copy the binary from the builder stage
COPY --from=builder /app/electronwall .
# Copy the rules directory from the builder stage
COPY --from=builder /app/rules /app/rules
# Specify the container's executable
CMD ["./electronwall"]