From 1d50efe3c6c465cf3e42afebc82517377bd1aa21 Mon Sep 17 00:00:00 2001 From: Kato Twofold Date: Wed, 1 Jun 2022 21:24:03 +0300 Subject: [PATCH 1/2] Dockerfile Build Improvements --- Dockerfile | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index ef5f4d3..5fc605b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,9 +1,25 @@ +FROM golang:1.18 as builder + +# Go into the app dir +WORKDIR /app + +# Copy all of the source to /app +COPY . ./ + +# Download dependencies +RUN go mod download + +# Build +RUN CGO_ENABLED=0 go build -a -installsuffix cgo -o freepad . + FROM alpine LABEL version="1.4.0" -# Copy the distribution files -COPY ./dist /app +# Copy the files from the builder to the new image +COPY --from=builder /app/freepad /app/freepad +COPY --from=builder /app/templates /app/templates +COPY --from=builder /app/static /app/static # Make /app the work directory WORKDIR /app From 1585d3b158cecde803a5b8a54b613d0bce7645ee Mon Sep 17 00:00:00 2001 From: Kato Twofold Date: Wed, 1 Jun 2022 21:28:57 +0300 Subject: [PATCH 2/2] * Replaced alpine with Scratch * Changed comments * Used /src instead of /app twice --- Dockerfile | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/Dockerfile b/Dockerfile index 5fc605b..3917d1c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,25 +1,27 @@ +# Importing golang 1.18 to use as a builder for our source FROM golang:1.18 as builder -# Go into the app dir -WORKDIR /app +# Use the /src directory as a workdir +WORKDIR /src -# Copy all of the source to /app +# Copy the src to /src COPY . ./ # Download dependencies RUN go mod download -# Build +# Build the executable RUN CGO_ENABLED=0 go build -a -installsuffix cgo -o freepad . -FROM alpine +# Import alpine linux as a base +FROM scratch LABEL version="1.4.0" # Copy the files from the builder to the new image -COPY --from=builder /app/freepad /app/freepad -COPY --from=builder /app/templates /app/templates -COPY --from=builder /app/static /app/static +COPY --from=builder /src/freepad /app/freepad +COPY --from=builder /src/templates /app/templates +COPY --from=builder /src/static /app/static # Make /app the work directory WORKDIR /app