From 755c213b91228aaae1b1e8ee584560cdd20a4a2f Mon Sep 17 00:00:00 2001 From: Nick Dumas Date: Sun, 18 Jun 2023 15:04:36 -0400 Subject: [PATCH] Alpine build works, without version injection --- Dockerfile | 43 ++++++++++++++++++++++++++++++++++++------- Makefile | 6 +++--- 2 files changed, 39 insertions(+), 10 deletions(-) diff --git a/Dockerfile b/Dockerfile index 193c4af..64941d6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,10 +1,39 @@ -FROM golang:latest AS builder -ADD . /opt/obp -WORKDIR /opt/obp +# syntax=docker/dockerfile:1 + +# Build the application from source +FROM golang:latest AS build-stage + +WORKDIR /app + +COPY go.mod go.sum ./ +RUN go mod download + +COPY . ./ + +# This definitely works +# RUN CGO_ENABLED=0 GOOS=linux go build -o obp ./cmd/obp/ RUN make build-alpine -FROM alpine:latest -RUN apk --no-cache add ca-certificates -ARG VERSION=* -COPY --from=builder /opt/obp/dist/obp-$VERSION-alpine_amd64/obp /bin/obp + +# Deploy the application binary into a lean image +FROM alpine:latest AS build-release-stage + +WORKDIR / + +ARG VERSION=version +COPY --from=build-stage /app/dist/*-alpine/obp /bin/obp RUN chmod +x /bin/obp + +ARG USER=default +ENV HOME /home/$USER + +# install sudo as root +RUN apk add --update sudo + +# add new user +RUN adduser -D $USER \ + && echo "$USER ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/$USER \ + && chmod 0440 /etc/sudoers.d/$USER + +USER $USER +WORKDIR $HOME diff --git a/Makefile b/Makefile index bee3d51..6a5cad5 100644 --- a/Makefile +++ b/Makefile @@ -13,9 +13,9 @@ docker-push: docker-image: $(DOCKER_CMD) build --build-arg VERSION=$(VERSION) -t code.ndumas.com/ndumas/obsidian-pipeline:$(VERSION) . -.PHONE: build-alpine +.PHONY: build-alpine build-alpine: - CGO_ENABLED=0 go build -a -installsuffix cgo -o $(DISTDIR)/$(NAME)-$(VERSION)-alpine_amd64/obp cmd/obp/cmd/*.go + CGO_ENABLED=0 GOOS=linux go build -ldflags="buildmode=exe $(LDFLAGS) -linkmode external -w -extldflags '-static'" -o $(DISTDIR)/$(NAME)-$(VERSION)-alpine/obp cmd/obp/*.go # This file is intended as a starting point for a customized makefile for a Go project. # @@ -92,7 +92,7 @@ GOCMD = go ARCHES ?= amd64 386 OSES ?= windows linux darwin OUTTPL = $(DISTDIR)/$(NAME)-$(VERSION)-{{.OS}}_{{.Arch}}/{{.Dir}} -LDFLAGS = -X $(PKG).Version=$(VERSION) -X $(PKG).Build=$(COMMIT_ID) +LDFLAGS = -X '$(PKG).Version=$(VERSION)' -X '$(PKG).Build=$(COMMIT_ID)' GOBUILD = gox -osarch="!darwin/386" -rebuild -gocmd="$(GOCMD)" -arch="$(ARCHES)" -os="$(OSES)" -output="$(OUTTPL)" -tags "$(BUILD_TAGS)" -ldflags "$(LDFLAGS)" GOCLEAN = $(GOCMD) clean GOINSTALL = $(GOCMD) install -a -tags "$(BUILD_TAGS)" -ldflags "$(LDFLAGS)"