From 113ca74436fa3f78d45918f4746ac132b458663e Mon Sep 17 00:00:00 2001 From: Leon Latsch Date: Mon, 1 Apr 2024 23:52:26 +0200 Subject: [PATCH] finish docker file for building prod --- Dockerfile | 27 ++++++++++++++++++++++----- build.sh | 18 +++++++++--------- 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/Dockerfile b/Dockerfile index 1b2740a..254e6c8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,24 @@ -FROM alpine:latest +FROM golang:latest as builder +# Define build env +ENV GOOS linux +ENV CGO_ENABLED 0 +# Add a work directory +WORKDIR /build +# Cache and install dependencies +COPY go.mod go.sum ./ +RUN go mod download +# Copy app files +COPY . . +# Build app +RUN go build -o app -WORKDIR /app +FROM alpine:latest as production +# Add certificates +RUN apk add --no-cache ca-certificates +# Copy built binary from builder +COPY --from=builder build/app . +# Expose port +EXPOSE 4000 +# Exec built binary +CMD ./app -COPY build/go-resolve /app/go-resolve - -ENTRYPOINT [ "/app/go-resolve" ] diff --git a/build.sh b/build.sh index ce28c47..8015590 100755 --- a/build.sh +++ b/build.sh @@ -1,13 +1,13 @@ #!/bin/bash -echo "Cleaning up" -rm -rf build/ -mkdir build/ - -echo "Building executable" -go build -o build/go-resolve - -if [[ "$1" == "--docker" ]]; then +if [[ "$1" == "docker" ]]; then echo "Building docker image" - docker build -t ghcr.io/leonlatsch/go-resolve . + docker build -t ghcr.io/leonlatsch/go-resolve . --target production +else + echo "Cleaning up" + rm -rf build/ + mkdir build/ + + echo "Building executable" + go build -o build/go-resolve fi