From bb12f0baf4efba155513ee195dae1a92c705d9b2 Mon Sep 17 00:00:00 2001 From: John Cairns Date: Mon, 16 Sep 2024 11:51:11 -0500 Subject: [PATCH] chore: reduce build artifact to 117Mb --- .devcontainer/Dockerfile | 37 ++++++++++++++++++++++++++++++++ .devcontainer/devcontainer.json | 2 +- Dockerfile | 38 ++++++++++++++------------------- Makefile | 1 + 4 files changed, 55 insertions(+), 23 deletions(-) create mode 100644 .devcontainer/Dockerfile diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile new file mode 100644 index 00000000..a7973454 --- /dev/null +++ b/.devcontainer/Dockerfile @@ -0,0 +1,37 @@ +FROM golang:1.22-bullseye + +ARG TARGETARCH + +RUN export DEBIAN_FRONTEND=noninteractive && \ + apt update && \ + apt install -y -q --no-install-recommends \ + build-essential gcc musl-dev linux-headers-${TARGETARCH} && \ + apt clean && \ + rm -rf /var/lib/apt/lists/* + +RUN useradd --create-home -s /bin/bash gobuild +RUN usermod -a -G sudo gobuild +RUN echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers + +ARG PROJECT=go-sidecar +RUN mkdir -p /workspaces/${PROJECT} +WORKDIR /workspaces/${PROJECT} +COPY --chown=gobuild:gobuild . . + +# system and linux dependencies +RUN make deps-linux +RUN chown -R gobuild:gobuild /go + +# local dependencies +ENV USER=gobuild +ENV GOBIN=/go/bin +ENV PATH=$PATH:${GOBIN} +USER gobuild + +RUN git config --global --add safe.directory /workspaces/${PROJECT} + +RUN make yamlfmt +RUN make fmtcheck +RUN make vet +RUN make lint +RUN make test diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 33bba3f6..d7924b36 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -6,7 +6,7 @@ // Sets the run context to one level up instead of the .devcontainer folder. "context": "..", // Update the 'dockerFile' property if you aren't using the standard 'Dockerfile' filename. - "dockerfile": "../Dockerfile" + "dockerfile": "./Dockerfile" }, "customizations": { "vscode": { diff --git a/Dockerfile b/Dockerfile index 50ddd736..f916e727 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,31 +1,25 @@ -FROM golang:1.22-bullseye AS build +FROM golang:1.22-bullseye AS builder -RUN apt-get update -RUN apt-get install -y make +ARG TARGETARCH -RUN useradd --create-home -s /bin/bash gobuild -RUN usermod -a -G sudo gobuild -RUN echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers +RUN export DEBIAN_FRONTEND=noninteractive && \ + apt update && \ + apt install -y -q --no-install-recommends \ + build-essential gcc musl-dev linux-headers-${TARGETARCH} && \ + apt clean && \ + rm -rf /var/lib/apt/lists/* -ARG PROJECT=go-sidecar -RUN mkdir -p /workspaces/${PROJECT} -WORKDIR /workspaces/${PROJECT} -COPY --chown=gobuild:gobuild . . +WORKDIR /build + +COPY . . # system and linux dependencies RUN make deps-linux -RUN chown -R gobuild:gobuild /go -# local dependencies -ENV USER=gobuild -ENV GOBIN=/go/bin -ENV PATH=$PATH:${GOBIN} -USER gobuild +RUN make build + +FROM debian:stable-slim -RUN git config --global --add safe.directory /workspaces/${PROJECT} +COPY --from=builder /build/bin/cmd/sidecar /usr/local/bin/sidecar -RUN make yamlfmt -RUN make fmtcheck -RUN make vet -RUN make lint -RUN make test +ENTRYPOINT ["/usr/local/bin/sidecar"] diff --git a/Makefile b/Makefile index 5f8486ca..692ad277 100644 --- a/Makefile +++ b/Makefile @@ -43,6 +43,7 @@ clean: .PHONY: build/cmd/sidecar build/cmd/sidecar: go build -ldflags="-s -w" -o bin/cmd/sidecar cmd/sidecar/main.go + strip bin/cmd/sidecar .PHONY: build build: build/cmd/sidecar