From d143b31734405a0b267057a738cddc5e1baa2ef1 Mon Sep 17 00:00:00 2001 From: Piotr Biernat Date: Wed, 30 Nov 2022 02:08:36 +0100 Subject: [PATCH] Refactored docker image builder script --- Dockerfile.builder | 14 ++++++++++++ Dockerfile.server | 30 -------------------------- Dockerfile.target | 22 +++++++++++++++++++ Dockerfile.worker | 30 -------------------------- deploy/image-build.sh | 50 +++++++++++++++++++++++-------------------- 5 files changed, 63 insertions(+), 83 deletions(-) create mode 100644 Dockerfile.builder delete mode 100644 Dockerfile.server create mode 100644 Dockerfile.target delete mode 100644 Dockerfile.worker diff --git a/Dockerfile.builder b/Dockerfile.builder new file mode 100644 index 0000000..9a0110d --- /dev/null +++ b/Dockerfile.builder @@ -0,0 +1,14 @@ +# Builder +FROM golang:alpine + +ARG BIN_OUTPUT=/go/bin +ARG GO_SERVER=cmd/server/main.go +ARG GO_WORKER=cmd/worker/main.go + +WORKDIR /go/src/app +COPY src ./ + +RUN go mod download && \ + export CGO_ENABLED=0 ; export GOOS=linux ; export GOARCH=amd64 && \ + go build -ldflags="-w -s" -o "$BIN_OUTPUT/server" $GO_SERVER && \ + go build -ldflags="-w -s" -o "$BIN_OUTPUT/worker" $GO_WORKER diff --git a/Dockerfile.server b/Dockerfile.server deleted file mode 100644 index 2010485..0000000 --- a/Dockerfile.server +++ /dev/null @@ -1,30 +0,0 @@ -# Builder -FROM golang:alpine AS builder - -ARG BIN_OUTPUT=/go/bin/server -ARG GO_MAIN=cmd/server/main.go - -WORKDIR /go/src/app -COPY src ./ - -RUN go mod download && \ - export CGO_ENABLED=0 ; export GOOS=linux ; export GOARCH=amd64 && \ - go build -ldflags="-w -s" -o $BIN_OUTPUT $GO_MAIN - - -# Destination image - server -FROM gcr.io/distroless/base-debian10 - -ARG BIN_OUTPUT=/go/bin/server - -LABEL author="Piotr Biernat" -LABEL service="catalog-svc" -LABEL vendor="Egommerce" -LABEL version="1.0" - -WORKDIR / -COPY --from=builder $BIN_OUTPUT / -COPY .env.dist /.env - -EXPOSE 80 -ENTRYPOINT ["/server"] diff --git a/Dockerfile.target b/Dockerfile.target new file mode 100644 index 0000000..687004f --- /dev/null +++ b/Dockerfile.target @@ -0,0 +1,22 @@ +# Builder +ARG BUILDER_IMAGE="git.pbiernat.dev/egommerce/catalog-builder:latest" +FROM ${BUILDER_IMAGE} AS builder + +# Destination image - server +FROM gcr.io/distroless/base-debian10 + +ARG BIN_OUTPUT +ARG SVC_NAME +ARG SVC_VER + +LABEL author="Piotr Biernat" +LABEL vendor="egommerce" +LABEL service=${SVC_NAME} +LABEL version=${SVC_VER} + +WORKDIR / +COPY --from=builder $BIN_OUTPUT /app +COPY .env.dist /.env + +EXPOSE 80 +ENTRYPOINT ["/app"] diff --git a/Dockerfile.worker b/Dockerfile.worker deleted file mode 100644 index 14d3173..0000000 --- a/Dockerfile.worker +++ /dev/null @@ -1,30 +0,0 @@ -# Builder -FROM golang:alpine AS builder - -ARG BIN_OUTPUT=/go/bin/worker -ARG GO_WORKER=cmd/worker/main.go - -WORKDIR /go/src/app -COPY src ./ - -RUN go mod download && \ - export CGO_ENABLED=0 ; export GOOS=linux ; export GOARCH=amd64 && \ - go build -ldflags="-w -s" -o $BIN_OUTPUT $GO_WORKER - - -# Destination image - worker -FROM gcr.io/distroless/base-debian10 - -ARG BIN_OUTPUT=/go/bin/worker - -LABEL author="Piotr Biernat" -LABEL service="catalog-worker" -LABEL vendor="Egommerce" -LABEL version="1.0" - -WORKDIR / -COPY --from=builder $BIN_OUTPUT / -COPY .env.dist /.env - -EXPOSE 80 -ENTRYPOINT ["/worker"] diff --git a/deploy/image-build.sh b/deploy/image-build.sh index 905f77e..6a1c23b 100755 --- a/deploy/image-build.sh +++ b/deploy/image-build.sh @@ -1,33 +1,37 @@ #!/bin/sh # RUN IN REPO ROOT DIR !! -# export DOCKER_BUILDKIT=1 - -export IMAGE_BASE="git.pbiernat.dev/egommerce/catalog" -export SERVER_IMAGE="$IMAGE_BASE-svc" -export WORKER_IMAGE="$IMAGE_BASE-worker" +export IMAGE_PREFIX="git.pbiernat.dev/egommerce/catalog" +export BUILDER_IMAGE="$IMAGE_PREFIX-builder:tmp" +export SERVER_IMAGE="$IMAGE_PREFIX-svc" +export WORKER_IMAGE="$IMAGE_PREFIX-worker" TARGET=${1:-latest} -KIND=${2:-all} -if [ $KIND = "svc" ] || [ $KIND = "all" ]; then - echo "Building: $SERVER_IMAGE:$TARGET" - if [ $TARGET = "dev" ] - then - docker build --rm --no-cache -t "$SERVER_IMAGE:$TARGET" -f Dockerfile.server . # >/dev/null 2>&1 - else - docker build --rm --cache-from "$SERVER_IMAGE:$TARGET" -t "$SERVER_IMAGE:$TARGET" -f Dockerfile.server . >/dev/null 2>&1 - fi -fi +docker build --no-cache -t "$BUILDER_IMAGE" -f Dockerfile.builder . >/dev/null && echo "Successfully tagged $BUILDER_IMAGE" -if [ $KIND = "worker" ] || [ $KIND = "all" ]; then - echo "Building: $WORKER_IMAGE:$TARGET" - if [ $TARGET = "dev" ] - then - docker build --rm --no-cache -t "$WORKER_IMAGE:$TARGET" -f Dockerfile.worker . # >/dev/null 2>&1 - else - docker build --rm --cache-from "$WORKER_IMAGE:$TARGET" -t "$WORKER_IMAGE:$TARGET" -f Dockerfile.worker . >/dev/null 2>&1 - fi +echo "Building target $IMAGE_PREFIX images..." +if [ $TARGET = "latest" ] +then + # SERVER + docker build --build-arg SVC_NAME=catalog-svc --build-arg SVC_VER="1.0" --build-arg BIN_OUTPUT=/go/bin/server \ + --rm --build-arg BUILDER_IMAGE --cache-from "$SERVER_IMAGE:$TARGET" \ + -t "$SERVER_IMAGE:$TARGET" -f Dockerfile.target . >/dev/null 2>&1 + + # WORKER + docker build --build-arg SVC_NAME=catalog-worker --build-arg SVC_VER="1.0" --build-arg BIN_OUTPUT=/go/bin/worker \ + --rm --build-arg BUILDER_IMAGE --cache-from "$WORKER_IMAGE:$TARGET" \ + -t "$WORKER_IMAGE:$TARGET" -f Dockerfile.target . >/dev/null 2>&1 +else + # SERVER + docker build --build-arg SVC_NAME=catalog-svc --build-arg SVC_VER="dev" --build-arg BIN_OUTPUT=/go/bin/server \ + --rm --build-arg BUILDER_IMAGE --no-cache -t "$SERVER_IMAGE:$TARGET" -f Dockerfile.target . \ + >/dev/null && echo "Successfully tagged $SERVER_IMAGE:$TARGET" + + # WORKER + docker build --build-arg SVC_NAME=catalog-worker --build-arg SVC_VER="dev" --build-arg BIN_OUTPUT=/go/bin/worker \ + --rm --build-arg BUILDER_IMAGE --no-cache -t "$WORKER_IMAGE:$TARGET" -f Dockerfile.target . \ + >/dev/null && echo "Successfully tagged $WORKER_IMAGE:$TARGET" fi echo "Done."