-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dockerfile
50 lines (36 loc) · 1.18 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
# Start from a small, secure base image
FROM golang:1.22-alpine AS builder
ARG VER=dev
# Set the working directory inside the container
WORKDIR /app
# Copy the Go module files
COPY go.mod go.sum ./
# Download the Go module dependencies
RUN go mod download
# Copy the source code into the container
COPY . .
RUN apk add --update make git gcc libc-dev curl file
# Build the Go binary
RUN --mount=type=cache,target=/root/.cache/go-build \
export CGO_ENABLED=1 &&\
export LDFLAGS="-X main.version=$VER -linkmode external -extldflags \"-static\"" &&\
go build -v -ldflags "${LDFLAGS}" -o billy ./cmd/billy/main.go &&\
file billy
# Create a minimal production image
FROM alpine:latest
# It's essential to regularly update the packages within the image to include security patches
# Reduce image size
RUN apk update && \
apk upgrade && \
rm -rf /var/cache/apk/* && \
rm -rf /tmp/*
# Avoid running code as a root user
RUN adduser -D billy
USER billy
# Set the working directory inside the container
WORKDIR /app
RUN mkdir /app/data
# Copy only the necessary files from the builder stage
COPY --from=builder /app/billy .
# Run the binary when the container starts
CMD ["./billy"]