瀏覽代碼

Merge pull request #160 from chris-crone/dockerfile-improvements

Dockerfile improvements
Chris Crone 5 年之前
父節點
當前提交
21b6a2ae71
共有 4 個文件被更改,包括 47 次插入64 次删除
  1. 1 0
      .dockerignore
  2. 30 38
      Dockerfile
  3. 9 19
      Makefile
  4. 7 7
      builder.Makefile

+ 1 - 0
.dockerignore

@@ -1,2 +1,3 @@
+.git/
 bin/
 tests/node-client/node_modules/

+ 30 - 38
Dockerfile

@@ -1,66 +1,58 @@
 # syntax = docker/dockerfile:experimental
 ARG GO_VERSION=1.14.3-alpine
-ARG GOLANGCI_LINT_VERSION=1.27.0
+ARG GOLANGCI_LINT_VERSION=v1.27.0-alpine
 
-FROM golang:${GO_VERSION} AS base
-ARG TARGET_OS=unknown
-ARG TARGET_ARCH=unknown
-ARG PWD=/api
+FROM --platform=${BUILDPLATFORM} golang:${GO_VERSION} AS base
+WORKDIR /api
 ENV GO111MODULE=on
-
-RUN apk update && apk add -U docker make
-
-WORKDIR ${PWD}
-ADD go.* ${PWD}
+RUN apk add --no-cache \
+    docker \
+    make \
+    protoc
+COPY go.* .
 RUN go mod download
-ADD . ${PWD}
-
-FROM golang:${GO_VERSION} AS protos-base
-ARG TARGET_OS=unknown
-ARG TARGET_ARCH=unknown
-ARG PWD=/api
-ENV GO111MODULE=on
-
-RUN apk update && apk add protoc make
 
+FROM base AS make-protos
 RUN go get github.com/golang/protobuf/[email protected]
-
-WORKDIR ${PWD}
-ADD go.* ${PWD}
-ADD . ${PWD}
-
-FROM protos-base AS make-protos
+COPY . .
 RUN make -f builder.Makefile protos
 
-FROM golangci/golangci-lint:v${GOLANGCI_LINT_VERSION}-alpine AS lint-base
+FROM golangci/golangci-lint:${GOLANGCI_LINT_VERSION} AS lint-base
 
 FROM base AS lint
-COPY --from=lint-base /usr/bin/golangci-lint /usr/bin/golangci-lint
 ENV CGO_ENABLED=0
-RUN --mount=id=build,type=cache,target=/root/.cache/go-build \
-    --mount=id=lint,type=cache,target=/root/.cache/golangci-lint \
+COPY --from=lint-base /usr/bin/golangci-lint /usr/bin/golangci-lint
+RUN --mount=target=. \
+    --mount=type=cache,target=/root/.cache/go-build \
+    --mount=type=cache,target=/root/.cache/golangci-lint \
     make -f builder.Makefile lint
 
 FROM base AS make-cli
-RUN --mount=id=build,type=cache,target=/root/.cache/go-build \
-    GOOS=${TARGET_OS} \
-    GOARCH=${TARGET_ARCH} \
-    make -f builder.Makefile cli
+ENV CGO_ENABLED=0
+ARG TARGETOS
+ARG TARGETARCH
+RUN --mount=target=. \
+    --mount=type=cache,target=/root/.cache/go-build \
+    GOOS=${TARGETOS} \
+    GOARCH=${TARGETARCH} \
+    make BINARY=/out/docker -f builder.Makefile cli
 
 FROM base AS make-cross
-RUN --mount=id=build,type=cache,target=/root/.cache/go-build \
-    make -f builder.Makefile cross
+RUN --mount=target=. \
+    --mount=type=cache,target=/root/.cache/go-build \
+    make BINARY=/out/docker  -f builder.Makefile cross
 
 FROM scratch AS protos
 COPY --from=make-protos /api/protos .
 
 FROM scratch AS cli
-COPY --from=make-cli /api/bin/* .
+COPY --from=make-cli /out/* .
 
 FROM scratch AS cross
-COPY --from=make-cross /api/bin/* .
+COPY --from=make-cross /out/* .
 
 FROM base as test
 ENV CGO_ENABLED=0
-RUN --mount=id=build,type=cache,target=/root/.cache/go-build \
+RUN --mount=target=. \
+    --mount=type=cache,target=/root/.cache/go-build \
     make -f builder.Makefile test

+ 9 - 19
Makefile

@@ -23,25 +23,18 @@
 # ARISING FROM, OUT OF OR IN CONNECTION WITH
 # THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
-GOOS ?= $(shell go env GOOS)
-GOARCH ?= $(shell go env GOARCH)
-PWD = $(shell pwd)
-
 export DOCKER_BUILDKIT=1
 
 all: cli
 
 protos: ## Generate go code from .proto files
-	@docker build . \
-	--output type=local,dest=./protos \
-	--target protos
+	@docker build . --target protos \
+	--output ./protos
 
 cli: ## Compile the cli
-	@docker build . \
-	--output type=local,dest=./bin \
-	--build-arg TARGET_OS=${GOOS} \
-	--build-arg TARGET_ARCH=${GOARCH} \
-	--target cli
+	@docker build . --target cli \
+	--platform local \
+	--output ./bin
 
 e2e-local: ## Run End to end local tests
 	go test -v ./tests/e2e ./moby/e2e
@@ -50,20 +43,17 @@ e2e-aci: ## Run End to end ACI tests (requires azure login)
 	go test -v ./tests/aci-e2e
 
 cross: ## Compile the CLI for linux, darwin and windows
-	@docker build . \
-	--output type=local,dest=./bin \
-	--target cross
+	@docker build . --target cross \
+	--output ./bin \
 
 test: ## Run unit tests
-	@docker build . \
-	--target test
+	@docker build . --target test
 
 cache-clear: ## Clear the builder cache
 	@docker builder prune --force --filter type=exec.cachemount --filter=unused-for=24h
 
 lint: ## run linter(s)
-	@docker build . \
-	--target lint
+	@docker build . --target lint
 
 classic-link: ## create docker-classic symlink if does not already exist
 	ln -s /Applications/Docker.app/Contents/Resources/bin/docker /usr/local/bin/docker-classic

+ 7 - 7
builder.Makefile

@@ -23,21 +23,21 @@
 # ARISING FROM, OUT OF OR IN CONNECTION WITH
 # THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
-GOOS ?= $(shell go env GOOS)
-GOARCH ?= $(shell go env GOARCH)
+GOOS?=$(shell go env GOOS)
+GOARCH?=$(shell go env GOARCH)
 
 PROTOS=$(shell find protos -name \*.proto)
 
-EXTENSION :=
+EXTENSION:=
 ifeq ($(GOOS),windows)
-  EXTENSION := .exe
+  EXTENSION:=.exe
 endif
 
 STATIC_FLAGS=CGO_ENABLED=0
-LDFLAGS := "-s -w"
-GO_BUILD = $(STATIC_FLAGS) go build -trimpath -ldflags=$(LDFLAGS)
+LDFLAGS:="-s -w"
+GO_BUILD=$(STATIC_FLAGS) go build -trimpath -ldflags=$(LDFLAGS)
 
-BINARY=bin/docker
+BINARY?=bin/docker
 BINARY_WITH_EXTENSION=$(BINARY)$(EXTENSION)
 
 all: cli