Refactored docker image builder script
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Piotr Biernat 2022-11-30 02:08:59 +01:00
parent 642a94741f
commit f7802a1a51
5 changed files with 65 additions and 83 deletions

14
Dockerfile.builder Normal file
View File

@ -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

View File

@ -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="order-svc"
LABEL vendor="Egommerce"
LABEL version="1.0"
WORKDIR /
COPY --from=builder $BIN_OUTPUT /
COPY .env.dist /.env
EXPOSE 80
ENTRYPOINT ["/server"]

22
Dockerfile.target Normal file
View File

@ -0,0 +1,22 @@
# Builder
ARG BUILDER_IMAGE="git.pbiernat.dev/egommerce/order-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"]

View File

@ -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="order-worker"
LABEL vendor="Egommerce"
LABEL version="1.0"
WORKDIR /
COPY --from=builder $BIN_OUTPUT /
COPY .env.dist /.env
EXPOSE 80
ENTRYPOINT ["/worker"]

View File

@ -1,31 +1,37 @@
#!/bin/sh
# RUN IN REPO ROOT DIR !!
export IMAGE_BASE="git.pbiernat.dev/egommerce/order"
export SERVER_IMAGE="$IMAGE_BASE-svc"
export WORKER_IMAGE="$IMAGE_BASE-worker"
export IMAGE_PREFIX="git.pbiernat.dev/egommerce/order"
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" ]
docker build --no-cache -t "$BUILDER_IMAGE" -f Dockerfile.builder . >/dev/null && echo "Successfully tagged $BUILDER_IMAGE"
echo "Building target $IMAGE_PREFIX images..."
if [ $TARGET = "latest" ]
then
docker build --rm --no-cache -t "$SERVER_IMAGE:$TARGET" -f Dockerfile.server . # >/dev/null 2>&1
# SERVER
docker build --build-arg SVC_NAME=basket-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=basket-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
docker build --rm --cache-from "$SERVER_IMAGE:$TARGET" -t "$SERVER_IMAGE:$TARGET" -f Dockerfile.server . >/dev/null 2>&1
fi
# SERVER
docker build --build-arg SVC_NAME=basket-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=basket-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
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
fi
echo "Done."