Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FAB-9629] support for building multi-arch docker images #1086

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 28 additions & 15 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ GO_TAGS ?=

RELEASE_EXES = orderer $(TOOLS_EXES)
RELEASE_IMAGES = baseos ccenv orderer peer tools
RELEASE_PLATFORMS = darwin-amd64 linux-amd64 linux-ppc64le linux-s390x windows-amd64
RELEASE_PLATFORMS = darwin-amd64 linux-amd64 linux-arm64 linux-ppc64le linux-s390x windows-amd64
DOCKER_ARCHES = arm64 ppc64le s390x amd64
TOOLS_EXES = configtxgen configtxlator cryptogen discover idemixgen peer

pkgmap.configtxgen := $(PKGNAME)/cmd/configtxgen
Expand Down Expand Up @@ -207,25 +208,35 @@ $(BUILD_DIR)/bin/%:
.PHONY: docker
docker: $(RELEASE_IMAGES:%=%-docker)

ALL_IMAGES=$(foreach image,$(RELEASE_IMAGES),$(foreach platform,$(DOCKER_ARCHES),$(image)-$(platform)))
.PHONY: $(ALL_IMAGES:%=%-docker)
$(ALL_IMAGES:%=%-docker): %-docker: $(BUILD_DIR)/images/%/$(DUMMY)

.PHONY: $(RELEASE_IMAGES:%=%-docker)
$(RELEASE_IMAGES:%=%-docker): %-docker: $(BUILD_DIR)/images/%/$(DUMMY)
$(RELEASE_IMAGES:%=%-docker): %-docker: $(foreach platform,$(DOCKER_ARCHES),%-$(platform)-docker)

$(BUILD_DIR)/images/ccenv/$(DUMMY): BUILD_CONTEXT=images/ccenv
$(BUILD_DIR)/images/baseos/$(DUMMY): BUILD_CONTEXT=images/baseos
$(BUILD_DIR)/images/peer/$(DUMMY): BUILD_ARGS=--build-arg GO_TAGS=${GO_TAGS}
$(BUILD_DIR)/images/orderer/$(DUMMY): BUILD_ARGS=--build-arg GO_TAGS=${GO_TAGS}
$(BUILD_DIR)/images/ccenv-%/$(DUMMY): BUILD_CONTEXT=images/ccenv
$(BUILD_DIR)/images/baseos-%/$(DUMMY): BUILD_CONTEXT=images/baseos
$(BUILD_DIR)/images/peer-%/$(DUMMY): BUILD_ARGS=--build-arg GO_TAGS=${GO_TAGS}
$(BUILD_DIR)/images/orderer-%/$(DUMMY): BUILD_ARGS=--build-arg GO_TAGS=${GO_TAGS}

$(BUILD_DIR)/images/%/$(DUMMY):
@echo "Building Docker image $(DOCKER_NS)/fabric-$*"
$(eval exe_variant = $(subst -, ,$*))
$(eval exe = $(word 1, $(exe_variant)))
$(eval arch = $(word 2, $(exe_variant)))

@echo "Building Docker image $(DOCKER_NS)/fabric-$(exe):$(arch)"
@mkdir -p $(@D)
$(DBUILD) -f images/$*/Dockerfile \
@docker version
$(DBUILD) -f images/$(exe)/Dockerfile \
--platform linux/$(arch) \
--build-arg GO_VER=$(GO_VER) \
--build-arg ALPINE_VER=$(ALPINE_VER) \
$(BUILD_ARGS) \
-t $(DOCKER_NS)/fabric-$* ./$(BUILD_CONTEXT)
docker tag $(DOCKER_NS)/fabric-$* $(DOCKER_NS)/fabric-$*:$(BASE_VERSION)
docker tag $(DOCKER_NS)/fabric-$* $(DOCKER_NS)/fabric-$*:$(TWO_DIGIT_VERSION)
docker tag $(DOCKER_NS)/fabric-$* $(DOCKER_NS)/fabric-$*:$(DOCKER_TAG)
-t $(DOCKER_NS)/fabric-$(exe) ./$(BUILD_CONTEXT)
docker tag $(DOCKER_NS)/fabric-$(exe) $(DOCKER_NS)/fabric-$(exe):$(BASE_VERSION)
docker tag $(DOCKER_NS)/fabric-$(exe) $(DOCKER_NS)/fabric-$(exe):$(TWO_DIGIT_VERSION)
docker tag $(DOCKER_NS)/fabric-$(exe) $(DOCKER_NS)/fabric-$(exe):$(arch)-$(PROJECT_VERSION)
@touch $@

# builds release packages for the host platform
Expand Down Expand Up @@ -267,10 +278,10 @@ docker-list: $(RELEASE_IMAGES:%=%-docker-list)
.PHONY: docker-clean
docker-clean: $(RELEASE_IMAGES:%=%-docker-clean)
%-docker-clean:
-@for image in "$$(docker images --quiet --filter=reference='$(DOCKER_NS)/fabric-$*:$(DOCKER_TAG)')"; do \
-@for image in "$$(docker images --quiet --filter=reference='$(DOCKER_NS)/fabric-$*')"; do \
[ -z "$$image" ] || docker rmi -f $$image; \
done
-@rm -rf $(BUILD_DIR)/images/$* || true
-@rm -rf $(BUILD_DIR)/images/$** || true

.PHONY: docker-tag-latest
docker-tag-latest: $(RELEASE_IMAGES:%=%-docker-tag-latest)
Expand All @@ -286,7 +297,9 @@ docker-tag-stable: $(RELEASE_IMAGES:%=%-docker-tag-stable)
publish-images: $(RELEASE_IMAGES:%=%-publish-images)
%-publish-images:
@docker login $(DOCKER_HUB_USERNAME) $(DOCKER_HUB_PASSWORD)
@docker push $(DOCKER_NS)/fabric-$*:$(PROJECT_VERSION)
@echo $(foreach platform,$(DOCKER_ARCHES),$(DOCKER_NS)/fabric-$*:$(platform)-$(PROJECT_VERSION)) | xargs -n 1 docker push
@echo $(foreach platform,$(DOCKER_ARCHES),$(DOCKER_NS)/fabric-$*:$(platform)-$(PROJECT_VERSION)) | xargs docker manifest create $(DOCKER_NS)/fabric-$*:$(PROJECT_VERSION)
docker manifest push $(DOCKER_NS)/fabric-$*:$(PROJECT_VERSION)

.PHONY: clean
clean: docker-clean unit-test-clean release-clean
Expand Down
4 changes: 2 additions & 2 deletions docker-env.mk
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ ifneq ($(NO_PROXY),)
DOCKER_BUILD_FLAGS+=--build-arg 'NO_PROXY=$(NO_PROXY)'
endif

DBUILD = docker build --force-rm $(DOCKER_BUILD_FLAGS)
DBUILD = docker buildx build --force-rm --output type=docker $(DOCKER_BUILD_FLAGS)

DOCKER_NS ?= hyperledger
DOCKER_TAG=$(ARCH)-$(PROJECT_VERSION)
Expand Down Expand Up @@ -53,4 +53,4 @@ BASE_DOCKER_LABEL=org.hyperledger.fabric
# As an aside, also note that we incorporate the version number in the .dummy
# file to differentiate different tags to fix FAB-1145
#
DUMMY = .dummy-$(DOCKER_TAG)
DUMMY = .dummy-$(PROJECT_VERSION)
9 changes: 6 additions & 3 deletions images/orderer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ ARG ALPINE_VER
FROM alpine:${ALPINE_VER} as base
RUN apk add --no-cache tzdata

FROM golang:${GO_VER}-alpine${ALPINE_VER} as golang
FROM --platform=$BUILDPLATFORM golang:${GO_VER}-alpine${ALPINE_VER} as golang
RUN apk add --no-cache \
gcc \
musl-dev \
Expand All @@ -18,14 +18,17 @@ ADD . $GOPATH/src/github.com/hyperledger/fabric
WORKDIR $GOPATH/src/github.com/hyperledger/fabric

FROM golang as orderer
ARG BUILDPLATFORM
ARG TARGETPLATFORM
ARG GO_TAGS
RUN make orderer GO_TAGS=${GO_TAGS}
RUN export MAKETARGET=$(echo $TARGETPLATFORM | sed -e 's|/|-|g') && \
make release/${MAKETARGET}/bin/orderer GO_TAGS=${GO_TAGS}

FROM base
ENV FABRIC_CFG_PATH /etc/hyperledger/fabric
VOLUME /etc/hyperledger/fabric
VOLUME /var/hyperledger
COPY --from=orderer /go/src/github.com/hyperledger/fabric/build/bin /usr/local/bin
COPY --from=orderer /go/src/github.com/hyperledger/fabric/release/*/bin /usr/local/bin
COPY --from=orderer /go/src/github.com/hyperledger/fabric/sampleconfig/msp ${FABRIC_CFG_PATH}/msp
COPY --from=orderer /go/src/github.com/hyperledger/fabric/sampleconfig/orderer.yaml ${FABRIC_CFG_PATH}
COPY --from=orderer /go/src/github.com/hyperledger/fabric/sampleconfig/configtx.yaml ${FABRIC_CFG_PATH}
Expand Down
9 changes: 6 additions & 3 deletions images/peer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ ARG ALPINE_VER
FROM alpine:${ALPINE_VER} as peer-base
RUN apk add --no-cache tzdata

FROM golang:${GO_VER}-alpine${ALPINE_VER} as golang
FROM --platform=$BUILDPLATFORM golang:${GO_VER}-alpine${ALPINE_VER} as golang
RUN apk add --no-cache \
bash \
gcc \
Expand All @@ -19,14 +19,17 @@ ADD . $GOPATH/src/github.com/hyperledger/fabric
WORKDIR $GOPATH/src/github.com/hyperledger/fabric

FROM golang as peer
ARG BUILDPLATFORM
ARG TARGETPLATFORM
ARG GO_TAGS
RUN make peer GO_TAGS=${GO_TAGS}
RUN export MAKETARGET=$(echo $TARGETPLATFORM | sed -e 's|/|-|g') && \
make release/${MAKETARGET}/bin/peer GO_TAGS=${GO_TAGS}

FROM peer-base
ENV FABRIC_CFG_PATH /etc/hyperledger/fabric
VOLUME /etc/hyperledger/fabric
VOLUME /var/hyperledger
COPY --from=peer /go/src/github.com/hyperledger/fabric/build/bin /usr/local/bin
COPY --from=peer /go/src/github.com/hyperledger/fabric/release/*/bin /usr/local/bin
COPY --from=peer /go/src/github.com/hyperledger/fabric/sampleconfig/msp ${FABRIC_CFG_PATH}/msp
COPY --from=peer /go/src/github.com/hyperledger/fabric/sampleconfig/core.yaml ${FABRIC_CFG_PATH}
EXPOSE 7051
Expand Down
14 changes: 11 additions & 3 deletions images/tools/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

ARG GO_VER
ARG ALPINE_VER
FROM golang:${GO_VER}-alpine${ALPINE_VER} as golang
FROM --platform=$BUILDPLATFORM golang:${GO_VER}-alpine${ALPINE_VER} as golang

RUN apk add --no-cache \
bash \
Expand All @@ -17,7 +17,15 @@ ADD . $GOPATH/src/github.com/hyperledger/fabric
WORKDIR $GOPATH/src/github.com/hyperledger/fabric

FROM golang as tools
RUN make configtxgen configtxlator cryptogen peer discover idemixgen
ARG BUILDPLATFORM
ARG TARGETPLATFORM
RUN export MAKETARGET=$(echo $TARGETPLATFORM | sed -e 's|/|-|g') && \
make release/${MAKETARGET}/bin/configtxgen \
release/${MAKETARGET}/bin/configtxlator \
release/${MAKETARGET}/bin/cryptogen \
release/${MAKETARGET}/bin/peer \
release/${MAKETARGET}/bin/discover \
release/${MAKETARGET}/bin/idemixgen

FROM golang:${GO_VER}-alpine
# git is required to support `go list -m`
Expand All @@ -28,5 +36,5 @@ RUN apk add --no-cache \
tzdata;
ENV FABRIC_CFG_PATH /etc/hyperledger/fabric
VOLUME /etc/hyperledger/fabric
COPY --from=tools /go/src/github.com/hyperledger/fabric/build/bin /usr/local/bin
COPY --from=tools /go/src/github.com/hyperledger/fabric/release/*/bin /usr/local/bin
COPY --from=tools /go/src/github.com/hyperledger/fabric/sampleconfig ${FABRIC_CFG_PATH}