chore: update Docker setup

This commit is contained in:
Matthew Lopez 2024-06-24 21:12:22 -04:00
parent ee46ec47e5
commit 531d9f8ed5
No known key found for this signature in database
GPG Key ID: 302A6EE3D63B7E0E
2 changed files with 33 additions and 17 deletions

View File

@ -3,5 +3,4 @@
build
log
go.work
*.test
go.work.sum
go.work.sum

View File

@ -1,19 +1,36 @@
# --- builder ---
FROM golang:1.20.6-alpine3.17 as builder
LABEL stage=builder
RUN apk add git
WORKDIR /build
# syntax=docker/dockerfile:1
COPY go.* ./
RUN go mod download
ARG app_dir="/home/go/app"
COPY . ./
ARG BUILD_STRING=pretendo.friends.docker
RUN go build -ldflags "-X 'main.serverBuildString=${BUILD_STRING}'" -v -o server
# --- runner ---
FROM alpine:3.17 as runner
WORKDIR /build
# * Building the application
FROM golang:1.22-alpine3.20 AS build
ARG app_dir build_string=pretendo.friends.docker
COPY --from=builder /build/server /build/
CMD ["/build/server"]
WORKDIR ${app_dir}
RUN --mount=type=cache,target=/go/pkg/mod/ \
--mount=type=bind,source=go.sum,target=go.sum \
--mount=type=bind,source=go.mod,target=go.mod \
go mod download -x
COPY . .
RUN --mount=type=cache,target=/go/pkg/mod/ \
CGO_ENABLED=0 go build -v -o ${app_dir}/build/server -ldflags "-X 'main.serverBuildString=${build_string}'"
# * Running the final application
FROM alpine:3.20 AS final
ARG app_dir
WORKDIR ${app_dir}
RUN addgroup go && adduser -D -G go go
RUN mkdir -p ${app_dir}/log
RUN chown go:go ${app_dir}/log
USER go
COPY --from=build ${app_dir}/build/server ${app_dir}/server
CMD [ "./server" ]