Build multi-platform images with Docker Buildx

This commit is contained in:
Frédéric Guillot 2020-10-18 15:00:10 -07:00 committed by Frédéric Guillot
parent cc8eb89ce0
commit 6e20d37dda
5 changed files with 41 additions and 83 deletions

View file

@ -1,19 +0,0 @@
ARG BASE_IMAGE_ARCH="amd64"
ARG ALPINE_LINUX_VERSION="3.12"
FROM golang:1-alpine${ALPINE_LINUX_VERSION} as build
ARG APP_VERSION
ARG APP_ARCH="amd64"
WORKDIR /go/src/app
RUN apk add --no-cache --update build-base git
COPY . .
RUN make linux-${APP_ARCH} VERSION=${APP_VERSION}
RUN cp /go/src/app/miniflux-linux-${APP_ARCH} /go/src/app/miniflux
FROM ${BASE_IMAGE_ARCH}/alpine:${ALPINE_LINUX_VERSION}
EXPOSE 8080
ENV LISTEN_ADDR 0.0.0.0:8080
RUN apk --no-cache add ca-certificates tzdata
COPY --from=build /go/src/app/miniflux /usr/bin/miniflux
USER nobody
CMD ["/usr/bin/miniflux"]

View file

@ -1,18 +1,18 @@
APP := miniflux
APP := miniflux
DOCKER_IMAGE := miniflux/miniflux
VERSION := $(shell git rev-parse --short HEAD)
BUILD_DATE := `date +%FT%T%z`
LD_FLAGS := "-s -w -X 'miniflux.app/version.Version=$(VERSION)' -X 'miniflux.app/version.BuildDate=$(BUILD_DATE)'"
PKG_LIST := $(shell go list ./... | grep -v /vendor/)
DB_URL := postgres://postgres:postgres@localhost/miniflux_test?sslmode=disable
VERSION := $(shell git describe --abbrev=0)
COMMIT := $(shell git rev-parse --short HEAD)
BUILD_DATE := `date +%FT%T%z`
LD_FLAGS := "-s -w -X 'miniflux.app/version.Version=$(VERSION)' -X 'miniflux.app/version.Commit=$(COMMIT)' -X 'miniflux.app/version.BuildDate=$(BUILD_DATE)'"
PKG_LIST := $(shell go list ./... | grep -v /vendor/)
DB_URL := postgres://postgres:postgres@localhost/miniflux_test?sslmode=disable
export PGPASSWORD := postgres
export GO111MODULE=on
.PHONY: generate \
miniflux \
linux-amd64 \
linux-armv8 \
linux-arm64 \
linux-armv7 \
linux-armv6 \
linux-armv5 \
@ -38,7 +38,7 @@ export GO111MODULE=on
docker-manifest
generate:
@ go generate -mod=vendor
@ go generate
miniflux: generate
@ go build -ldflags=$(LD_FLAGS) -o $(APP) main.go
@ -46,8 +46,8 @@ miniflux: generate
linux-amd64: generate
@ GOOS=linux GOARCH=amd64 go build -ldflags=$(LD_FLAGS) -o $(APP)-linux-amd64 main.go
linux-armv8: generate
@ GOOS=linux GOARCH=arm64 go build -ldflags=$(LD_FLAGS) -o $(APP)-linux-armv8 main.go
linux-arm64: generate
@ GOOS=linux GOARCH=arm64 go build -ldflags=$(LD_FLAGS) -o $(APP)-linux-arm64 main.go
linux-armv7: generate
@ GOOS=linux GOARCH=arm GOARM=7 go build -ldflags=$(LD_FLAGS) -o $(APP)-linux-armv7 main.go
@ -70,7 +70,7 @@ openbsd-amd64: generate
windows-amd64: generate
@ GOOS=windows GOARCH=amd64 go build -ldflags=$(LD_FLAGS) -o $(APP)-windows-amd64 main.go
build: linux-amd64 linux-armv8 linux-armv7 linux-armv6 linux-armv5 darwin-amd64 freebsd-amd64 openbsd-amd64 windows-amd64
build: linux-amd64 linux-arm64 linux-armv7 linux-armv6 linux-armv5 darwin-amd64 freebsd-amd64 openbsd-amd64 windows-amd64
# NOTE: unsupported targets
netbsd-amd64: generate
@ -120,52 +120,11 @@ clean-integration-test:
@ psql -U postgres -c 'drop database if exists miniflux_test;'
docker-image:
docker build -t $(DOCKER_IMAGE):$(VERSION) \
--build-arg APP_VERSION=$(VERSION) \
--build-arg APP_ARCH=amd64 \
--build-arg BASE_IMAGE_ARCH=amd64 .
docker build -t $(DOCKER_IMAGE):$(VERSION) -f packaging/docker/Dockerfile .
docker-images:
docker build -t $(DOCKER_IMAGE):amd64-$(VERSION) \
--build-arg APP_VERSION=$(VERSION) \
--build-arg APP_ARCH=amd64 \
--build-arg BASE_IMAGE_ARCH=amd64 .
docker tag $(DOCKER_IMAGE):amd64-$(VERSION) $(DOCKER_IMAGE):amd64-latest
docker build -t $(DOCKER_IMAGE):arm32v6-$(VERSION) \
--build-arg APP_VERSION=$(VERSION) \
--build-arg APP_ARCH=armv6 \
--build-arg BASE_IMAGE_ARCH=arm32v6 .
docker tag $(DOCKER_IMAGE):arm32v6-$(VERSION) $(DOCKER_IMAGE):arm32v6-latest
docker build -t $(DOCKER_IMAGE):arm32v7-$(VERSION) \
--build-arg APP_VERSION=$(VERSION) \
--build-arg APP_ARCH=armv7 \
--build-arg BASE_IMAGE_ARCH=arm32v7 .
docker tag $(DOCKER_IMAGE):arm32v7-$(VERSION) $(DOCKER_IMAGE):arm32v7-latest
docker build -t $(DOCKER_IMAGE):arm64v8-$(VERSION) \
--build-arg APP_VERSION=$(VERSION) \
--build-arg APP_ARCH=armv8 \
--build-arg BASE_IMAGE_ARCH=arm64v8 .
docker tag $(DOCKER_IMAGE):arm64v8-$(VERSION) $(DOCKER_IMAGE):arm64v8-latest
docker-manifest:
for version in $(VERSION) latest; do \
docker push $(DOCKER_IMAGE):amd64-$${version} && \
docker push $(DOCKER_IMAGE):arm32v6-$${version} && \
docker push $(DOCKER_IMAGE):arm32v7-$${version} && \
docker push $(DOCKER_IMAGE):arm64v8-$${version} && \
docker manifest create --amend $(DOCKER_IMAGE):$${version} \
$(DOCKER_IMAGE):amd64-$${version} \
$(DOCKER_IMAGE):arm32v6-$${version} \
$(DOCKER_IMAGE):arm32v7-$${version} \
$(DOCKER_IMAGE):arm64v8-$${version} && \
docker manifest annotate $(DOCKER_IMAGE):$${version} \
$(DOCKER_IMAGE):arm32v6-$${version} --os linux --arch arm --variant v6 && \
docker manifest annotate $(DOCKER_IMAGE):$${version} \
$(DOCKER_IMAGE):arm32v7-$${version} --os linux --arch arm --variant v7 && \
docker manifest annotate $(DOCKER_IMAGE):$${version} \
$(DOCKER_IMAGE):arm64v8-$${version} --os linux --arch arm64 --variant v8 && \
docker manifest push --purge $(DOCKER_IMAGE):$${version} ;\
done
docker buildx build \
--platform linux/amd64,linux/arm64,linux/arm/v7,linux/arm/v6 \
--file packaging/docker/Dockerfile \
--tag $(DOCKER_IMAGE):$(VERSION) \
--push .

View file

@ -13,6 +13,7 @@ import (
func info() {
fmt.Println("Version:", version.Version)
fmt.Println("Commit:", version.Commit)
fmt.Println("Build Date:", version.BuildDate)
fmt.Println("Go Version:", runtime.Version())
fmt.Println("Compiler:", runtime.Compiler)

View file

@ -0,0 +1,16 @@
FROM golang:alpine AS build
RUN apk add --no-cache --update git
ADD . /go/src/app
WORKDIR /go/src/app
RUN go generate
RUN go build \
-o miniflux \
-ldflags="-s -w -X 'miniflux.app/version.Version=`git describe --abbrev=0`' -X 'miniflux.app/version.Commit=`git rev-parse --short HEAD`' -X 'miniflux.app/version.BuildDate=`date +%FT%T%z`'" \
main.go
FROM alpine:latest
ENV LISTEN_ADDR 0.0.0.0:8080
RUN apk --no-cache add ca-certificates tzdata
COPY --from=build /go/src/app/miniflux /usr/bin/miniflux
USER nobody
CMD ["/usr/bin/miniflux"]

View file

@ -4,8 +4,9 @@
package version // import "miniflux.app/version"
// Version of the application (generated with "make build")
var Version = "2.0.x-dev"
// BuildDate is generated with "make build"
var BuildDate = "undefined"
// Variables populated at build time.
var (
Version = "dev"
Commit = "HEAD"
BuildDate = "undefined"
)