Skip to content

Commit

Permalink
Update dockerfile and makefile for multiarch support
Browse files Browse the repository at this point in the history
**What**
- Create multiarch Docker images and update Makefile
  to standardize the process
Signed-off-by: Lucas Roesler <[email protected]>
  • Loading branch information
LucasRoesler committed Jun 19, 2020
1 parent a1f6f27 commit 7235cab
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 11 deletions.
24 changes: 20 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# syntax=docker/dockerfile:experimental
FROM golang:1.14 as builder
FROM golang:1.14-alpine as builder

WORKDIR /app
COPY go.mod go.sum ./
RUN --mount=type=cache,target=/go/pkg/mod go mod download

ENV GO111MODULE=on
ENV CGO_ENABLED=0
ARG go_opts
ARG GOOS=linux
ARG GIT_COMMIT=0
ARG GIT_VERSION=dev
Expand All @@ -17,16 +18,31 @@ COPY cmd ./cmd
COPY pkg ./pkg
COPY main.go .

RUN --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/go/pkg/mod go build -o /bin/openfaas-loki \
RUN --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/go/pkg/mod \
env $go_opts go build -o /bin/openfaas-loki \
-v -ldflags "\
-X $REPO/pkg.GitCommit=$GIT_COMMIT -X $REPO/pkg.Version=$GIT_VERSION \
-extldflags \"-static\"" \
.

FROM alpine:3.11 as image
# we can't add user in next stage because it's from scratch
# ca-certificates and tmp folder are also missing in scratch
# so we add all of it here and copy files in next stage
RUN addgroup -S app \
&& adduser -S -g app app \
&& mkdir /scratch-tmp

RUN apk --no-cache --update add ca-certificates
FROM scratch

EXPOSE 9191

ENV http_proxy ""
ENV https_proxy ""
USER app

COPY --from=builder /etc/passwd /etc/group /etc/
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=builder --chown=app:app /scratch-tmp /tmp
COPY --from=builder /bin/openfaas-loki /bin/openfaas-loki

ENTRYPOINT ["openfaas-loki"]
40 changes: 33 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ ifneq ($(.GIT_UNTRACKEDCHANGES),)
.GIT_COMMIT := $(.GIT_COMMIT)-dirty
endif

ARCHS=amd64 arm64 armhf ppc64le
BUILD_ARGS=--build-arg GIT_COMMIT=$(.GIT_COMMIT) --build-arg GIT_VERSION=$(.GIT_VERSION)g

################################
################################
.PHONY: help
Expand Down Expand Up @@ -54,17 +57,40 @@ install: $(shell find ./pkg ./cmd) ## Build the project and store the binaries i
"-X ${.PKG}/pkg.GitCommit=${.GIT_COMMIT} -X ${.PKG}/pkg.Version=${.GIT_VERSION}" \
.


.PHONY: build
build: ## Create a docker image using the binary from make build
DOCKER_BUILDKIT=1 docker build \
-t ${.IMAGE_PREFIX}:latest \
-t ${.IMAGE_PREFIX}:dev \
--build-arg GIT_COMMIT=${.GIT_COMMIT} \
--build-arg GIT_VERSION=${.GIT_VERSION} \
build: $(addprefix build-,$(ARCHS)) ## Build Docker images for all architectures

.PHONY: build-%
build-%:
DOCKER_BUILDKIT=1 docker build $(BUILD_ARGS) --build-arg go_opts="GOARCH=$*" \
-t ${.IMAGE_PREFIX}:latest-$* \
-f ./Dockerfile .

build-armhf:
DOCKER_BUILDKIT=1 docker build $(BUILD_ARGS) --build-arg go_opts="GOARCH=arm GOARM=6" \
-t ${.IMAGE_PREFIX}:latest-armhf \
-f ./Dockerfile .

.PHONY: push
push: $(addprefix push-,$(ARCHS)) ## Push Docker images for all architectures

.PHONY: push-%
push-%:
docker push ${.IMAGE_PREFIX}:latest-$*

.PHONY: manifest
manifest: ## Create and push Docker manifest to combine all architectures in multi-arch Docker image
docker manifest create --amend ${.IMAGE_PREFIX}:latest $(addprefix ${.IMAGE_PREFIX}:latest-,$(ARCHS))
$(MAKE) $(addprefix manifest-annotate-,$(ARCHS))
docker manifest push -p ${.IMAGE_PREFIX}:latest

.PHONY: manifest-annotate-%
manifest-annotate-%:
docker manifest annotate ${.IMAGE_PREFIX}:latest ${.IMAGE_PREFIX}:latest-$* --os linux --arch $*

.PHONY: manifest-annotate-armhf
manifest-annotate-armhf:
docker manifest annotate ${.IMAGE_PREFIX}:latest ${.IMAGE_PREFIX}:latest-armhf --os linux --arch arm --variant v6

.PHONY: package
package: ## Package the helm chart
Expand Down

0 comments on commit 7235cab

Please sign in to comment.