Skip to content

Commit f270b72

Browse files
committed
Improve docker image creation time
Avoid repeatedly sending the entire source tree as the docker build context when creating our production images. Instead, build the release binaries and then copy them into the container images. Also, reorder some targets to alphabetize and remove the test-cmd target as we no longer require tags when running unit tests. FAB-16514 #done Signed-off-by: Matthew Sykes <matthew.sykes@gmail.com> Change-Id: Ia413fde87324bdb03290e33542fa3f4a12bd7939
1 parent d44b9d7 commit f270b72

File tree

7 files changed

+46
-87
lines changed

7 files changed

+46
-87
lines changed

Makefile

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
# - unit-test - runs the go-test based unit tests
2222
# - verify - runs unit tests for only the changed package tree
2323
# - profile - runs unit tests for all packages in coverprofile mode (slow)
24-
# - test-cmd - generates a "go test" string suitable for manual customization
2524
# - gotools - installs go tools like golint
2625
# - linter - runs all code checks
2726
# - check-deps - check for vendored dependencies that are no longer used
@@ -87,18 +86,19 @@ GO_TAGS ?=
8786

8887
# No sense rebuilding when non production code is changed
8988
PROJECT_FILES = $(shell git ls-files | grep -Ev '^integration/|^vagrant/|.png$|^LICENSE|^vendor/')
90-
IMAGES = peer orderer baseos ccenv buildenv tools
91-
RELEASE_PLATFORMS = windows-amd64 darwin-amd64 linux-amd64 linux-s390x linux-ppc64le
92-
RELEASE_PKGS = configtxgen cryptogen idemixgen discover configtxlator peer orderer
93-
RELEASE_IMAGES = peer orderer tools ccenv baseos
89+
IMAGES = baseos buildenv ccenv orderer peer tools
90+
RELEASE_IMAGES = baseos ccenv orderer peer tools
91+
RELEASE_PKGS = orderer $(TOOLS_PKGS)
92+
RELEASE_PLATFORMS = darwin-amd64 linux-amd64 linux-ppc64le linux-s390x windows-amd64
93+
TOOLS_PKGS = configtxgen configtxlator cryptogen discover idemixgen peer
9494

95-
pkgmap.cryptogen := $(PKGNAME)/cmd/cryptogen
96-
pkgmap.idemixgen := $(PKGNAME)/cmd/idemixgen
9795
pkgmap.configtxgen := $(PKGNAME)/cmd/configtxgen
9896
pkgmap.configtxlator := $(PKGNAME)/cmd/configtxlator
99-
pkgmap.peer := $(PKGNAME)/cmd/peer
100-
pkgmap.orderer := $(PKGNAME)/cmd/orderer
97+
pkgmap.cryptogen := $(PKGNAME)/cmd/cryptogen
10198
pkgmap.discover := $(PKGNAME)/cmd/discover
99+
pkgmap.idemixgen := $(PKGNAME)/cmd/idemixgen
100+
pkgmap.orderer := $(PKGNAME)/cmd/orderer
101+
pkgmap.peer := $(PKGNAME)/cmd/peer
102102

103103
include docker-env.mk
104104

@@ -147,8 +147,10 @@ tools-docker: $(BUILD_DIR)/images/tools/$(DUMMY)
147147

148148
buildenv: $(BUILD_DIR)/images/buildenv/$(DUMMY)
149149

150+
.PHONY: baseos
150151
baseos: $(BUILD_DIR)/images/baseos/$(DUMMY)
151152

153+
.PHONY: ccenv
152154
ccenv: $(BUILD_DIR)/images/ccenv/$(DUMMY)
153155

154156
.PHONY: peer
@@ -163,15 +165,19 @@ orderer-docker: $(BUILD_DIR)/images/orderer/$(DUMMY)
163165
configtxgen: GO_LDFLAGS=-X $(pkgmap.$(@F))/metadata.CommitSHA=$(EXTRA_VERSION)
164166
configtxgen: $(BUILD_DIR)/bin/configtxgen
165167

168+
.PHONY: configtxlator
166169
configtxlator: GO_LDFLAGS=-X $(pkgmap.$(@F))/metadata.CommitSHA=$(EXTRA_VERSION)
167170
configtxlator: $(BUILD_DIR)/bin/configtxlator
168171

172+
.PHONY: cryptogen
169173
cryptogen: GO_LDFLAGS=-X $(pkgmap.$(@F))/metadata.CommitSHA=$(EXTRA_VERSION)
170174
cryptogen: $(BUILD_DIR)/bin/cryptogen
171175

176+
.PHONY: idemixgen
172177
idemixgen: GO_LDFLAGS=-X $(pkgmap.$(@F))/metadata.CommitSHA=$(EXTRA_VERSION)
173178
idemixgen: $(BUILD_DIR)/bin/idemixgen
174179

180+
.PHONY: discover
175181
discover: GO_LDFLAGS=-X $(pkgmap.$(@F))/metadata.Version=$(PROJECT_VERSION)
176182
discover: $(BUILD_DIR)/bin/discover
177183

@@ -194,13 +200,9 @@ verify: unit-test
194200
profile: export JOB_TYPE=PROFILE
195201
profile: unit-test
196202

197-
# Generates a string to the terminal suitable for manual augmentation / re-issue, useful for running tests by hand
198-
test-cmd:
199-
@echo "go test -tags \"$(GO_TAGS)\""
200-
201203
docker: $(patsubst %,$(BUILD_DIR)/images/%/$(DUMMY), $(IMAGES))
202204

203-
native: peer orderer configtxgen cryptogen idemixgen configtxlator discover
205+
native: $(RELEASE_PKGS)
204206

205207
linter: check-deps buildenv
206208
@echo "LINT: Running code checks.."
@@ -231,9 +233,11 @@ $(BUILD_DIR)/bin/%: $(PROJECT_FILES)
231233
@echo "Binary available as $@"
232234
@touch $@
233235

234-
$(BUILD_DIR)/images/peer/$(DUMMY): BUILD_ARGS=--build-arg GO_TAGS=${GO_TAGS}
236+
$(BUILD_DIR)/images/tools/$(DUMMY): $(patsubst %,release/linux-amd64/bin/%, $(TOOLS_PKGS))
237+
238+
$(BUILD_DIR)/images/peer/$(DUMMY): release/linux-amd64/bin/peer
235239

236-
$(BUILD_DIR)/images/orderer/$(DUMMY): BUILD_ARGS=--build-arg GO_TAGS=${GO_TAGS}
240+
$(BUILD_DIR)/images/orderer/$(DUMMY): release/linux-amd64/bin/orderer
237241

238242
$(BUILD_DIR)/images/%/$(DUMMY):
239243
@mkdir -p $(@D)
@@ -242,7 +246,6 @@ $(BUILD_DIR)/images/%/$(DUMMY):
242246
$(DBUILD) -f images/$(TARGET)/Dockerfile \
243247
--build-arg GO_VER=${GO_VER} \
244248
--build-arg ALPINE_VER=${ALPINE_VER} \
245-
${BUILD_ARGS} \
246249
-t $(DOCKER_NS)/fabric-$(TARGET) .
247250
docker tag $(DOCKER_NS)/fabric-$(TARGET) $(DOCKER_NS)/fabric-$(TARGET):$(BASE_VERSION)
248251
docker tag $(DOCKER_NS)/fabric-$(TARGET) $(DOCKER_NS)/fabric-$(TARGET):$(DOCKER_TAG)

images/baseos/Dockerfile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
#
33
# SPDX-License-Identifier: Apache-2.0
44

5-
ARG GO_VER
65
ARG ALPINE_VER
76

8-
FROM alpine:${ALPINE_VER} as base
7+
FROM alpine:${ALPINE_VER}
98
RUN apk add --no-cache tzdata
109
RUN addgroup -g 500 chaincode && adduser -u 500 -D -h /home/chaincode -G chaincode chaincode
1110
USER chaincode

images/buildenv/Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
#
33
# SPDX-License-Identifier: Apache-2.0
44

5-
ARG GO_VER
65
ARG ALPINE_VER
6+
ARG GO_VER
7+
78
FROM golang:${GO_VER}-alpine${ALPINE_VER} as golang
89
RUN apk add --no-cache \
910
gcc \

images/ccenv/Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
#
33
# SPDX-License-Identifier: Apache-2.0
44

5-
ARG GO_VER
65
ARG ALPINE_VER
6+
ARG GO_VER
7+
78
FROM golang:${GO_VER}-alpine${ALPINE_VER}
89
RUN apk add --no-cache \
910
g++ \

images/orderer/Dockerfile

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,16 @@
22
#
33
# SPDX-License-Identifier: Apache-2.0
44

5-
ARG GO_VER
65
ARG ALPINE_VER
7-
FROM alpine:${ALPINE_VER} as base
8-
RUN apk add --no-cache tzdata
9-
10-
FROM golang:${GO_VER}-alpine${ALPINE_VER} as golang
11-
RUN apk add --no-cache \
12-
gcc \
13-
musl-dev \
14-
git \
15-
bash \
16-
make;
17-
ADD . $GOPATH/src/github.com/hyperledger/fabric
18-
WORKDIR $GOPATH/src/github.com/hyperledger/fabric
196

20-
FROM golang as orderer
21-
ARG GO_TAGS
22-
RUN make orderer GO_TAGS=${GO_TAGS}
23-
24-
FROM base
7+
FROM alpine:${ALPINE_VER}
8+
RUN apk add --no-cache tzdata
259
ENV FABRIC_CFG_PATH /etc/hyperledger/fabric
2610
VOLUME /etc/hyperledger/fabric
2711
VOLUME /var/hyperledger
28-
COPY --from=orderer /go/src/github.com/hyperledger/fabric/.build/bin /usr/local/bin
29-
COPY --from=orderer /go/src/github.com/hyperledger/fabric/sampleconfig/msp ${FABRIC_CFG_PATH}/msp
30-
COPY --from=orderer /go/src/github.com/hyperledger/fabric/sampleconfig/orderer.yaml ${FABRIC_CFG_PATH}
31-
COPY --from=orderer /go/src/github.com/hyperledger/fabric/sampleconfig/configtx.yaml ${FABRIC_CFG_PATH}
12+
COPY --chown=0:0 release/linux-amd64/bin/orderer /usr/local/bin
13+
COPY --chown=0:0 sampleconfig/msp ${FABRIC_CFG_PATH}/msp
14+
COPY --chown=0:0 sampleconfig/orderer.yaml ${FABRIC_CFG_PATH}
15+
COPY --chown=0:0 sampleconfig/configtx.yaml ${FABRIC_CFG_PATH}
3216
EXPOSE 7050
3317
CMD ["orderer"]

images/peer/Dockerfile

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,15 @@
22
#
33
# SPDX-License-Identifier: Apache-2.0
44

5-
ARG GO_VER
65
ARG ALPINE_VER
76

8-
FROM alpine:${ALPINE_VER} as peer-base
7+
FROM alpine:${ALPINE_VER}
98
RUN apk add --no-cache tzdata
10-
11-
FROM golang:${GO_VER}-alpine${ALPINE_VER} as golang
12-
RUN apk add --no-cache \
13-
bash \
14-
gcc \
15-
git \
16-
make \
17-
musl-dev
18-
ADD . $GOPATH/src/github.com/hyperledger/fabric
19-
WORKDIR $GOPATH/src/github.com/hyperledger/fabric
20-
21-
FROM golang as peer
22-
ARG GO_TAGS
23-
RUN make peer GO_TAGS=${GO_TAGS}
24-
25-
FROM peer-base
269
ENV FABRIC_CFG_PATH /etc/hyperledger/fabric
2710
VOLUME /etc/hyperledger/fabric
2811
VOLUME /var/hyperledger
29-
COPY --from=peer /go/src/github.com/hyperledger/fabric/.build/bin /usr/local/bin
30-
COPY --from=peer /go/src/github.com/hyperledger/fabric/sampleconfig/msp ${FABRIC_CFG_PATH}/msp
31-
COPY --from=peer /go/src/github.com/hyperledger/fabric/sampleconfig/core.yaml ${FABRIC_CFG_PATH}
12+
COPY --chown=0:0 release/linux-amd64/bin/peer /usr/local/bin
13+
COPY --chown=0:0 sampleconfig/msp ${FABRIC_CFG_PATH}/msp
14+
COPY --chown=0:0 sampleconfig/core.yaml ${FABRIC_CFG_PATH}
3215
EXPOSE 7051
3316
CMD ["peer","node","start"]

images/tools/Dockerfile

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,17 @@
44

55
ARG GO_VER
66
ARG ALPINE_VER
7-
FROM golang:${GO_VER}-alpine as golang
87

9-
RUN apk add --no-cache \
10-
bash \
11-
gcc \
12-
git \
13-
make \
14-
musl-dev;
15-
16-
ADD . $GOPATH/src/github.com/hyperledger/fabric
17-
WORKDIR $GOPATH/src/github.com/hyperledger/fabric
18-
19-
FROM golang as tools
20-
RUN make configtxgen configtxlator cryptogen peer discover idemixgen
21-
22-
FROM golang:${GO_VER}-alpine
23-
# git is required to support `go list -m`
24-
RUN apk add --no-cache \
25-
bash \
26-
git \
27-
jq \
28-
tzdata;
8+
FROM golang:${GO_VER}-alpine${ALPINE_VER}
9+
RUN apk add --no-cache bash git jq tzdata
2910
ENV FABRIC_CFG_PATH /etc/hyperledger/fabric
3011
VOLUME /etc/hyperledger/fabric
31-
COPY --from=tools /go/src/github.com/hyperledger/fabric/.build/bin /usr/local/bin
32-
COPY --from=tools /go/src/github.com/hyperledger/fabric/sampleconfig ${FABRIC_CFG_PATH}
12+
COPY --chown=0:0 sampleconfig ${FABRIC_CFG_PATH}
13+
COPY --chown=0:0 \
14+
release/linux-amd64/bin/configtxgen \
15+
release/linux-amd64/bin/configtxlator \
16+
release/linux-amd64/bin/cryptogen \
17+
release/linux-amd64/bin/discover \
18+
release/linux-amd64/bin/idemixgen \
19+
release/linux-amd64/bin/peer \
20+
/usr/local/bin/

0 commit comments

Comments
 (0)