소스 검색

Merge pull request #13 from ulyssessouza/dockerize-build

Dockerize build
Ulysses Souza 5 년 전
부모
커밋
31ad0385b2
3개의 변경된 파일80개의 추가작업 그리고 9개의 파일을 삭제
  1. 39 0
      Dockerfile
  2. 39 7
      Makefile
  3. 2 2
      backend/v1/backend.pb.go

+ 39 - 0
Dockerfile

@@ -0,0 +1,39 @@
+# syntax = docker/dockerfile:experimental
+ARG GO_VERSION=1.14.2
+
+FROM golang:${GO_VERSION} AS fs
+ARG TARGET_OS=unknown
+ARG TARGET_ARCH=unknown
+ARG PWD=$GOPATH/src/github.com/docker/api
+RUN apt-get update && apt-get install --no-install-recommends -y \
+    make \
+    git \
+    protobuf-compiler \
+    libprotobuf-dev
+RUN go get github.com/golang/protobuf/protoc-gen-go
+WORKDIR ${PWD}
+ADD go.* ${PWD}
+RUN go mod download
+ADD . ${PWD}
+
+FROM fs AS make-protos
+RUN make protos
+
+FROM make-protos AS make-bins
+RUN --mount=type=cache,target=/root/.cache/go-build \
+    GOOS=${TARGET_OS} \
+    GOARCH=${TARGET_ARCH} \
+    make bins
+
+FROM make-protos AS make-xbins
+RUN --mount=type=cache,target=/root/.cache/go-build \
+    make xbins
+
+FROM scratch AS protos
+COPY --from=make-protos /go/src/github.com/docker/api/backend/v1/*.pb.go .
+
+FROM scratch AS bins
+COPY --from=make-bins /go/src/github.com/docker/api/bin/* .
+
+FROM scratch AS xbins
+COPY --from=make-xbins /go/src/github.com/docker/api/bin/* .

+ 39 - 7
Makefile

@@ -25,21 +25,53 @@
 
 GIT_COMMIT=$(shell git rev-parse --short HEAD)
 GOOS ?= $(shell go env GOOS)
-PROTOS=$(shell find . -name \*.proto)
+GOARCH ?= $(shell go env GOARCH)
 
-export GO111MODULE=auto
+PROTOS=$(shell find . -name \*.proto)
 
-all: protos example cli
+export DOCKER_BUILDKIT=1
 
-cli:
-	cd cmd && go build -v -o ../bin/docker
+all: dbins
+xall: dxbins
+bins: cli example
+xbins: xcli xexample
 
 protos:
 	@protoc -I. --go_out=plugins=grpc,paths=source_relative:. ${PROTOS}
 
-example:
+cli: protos
+	cd cmd && GOOS=${GOOS} 	GOARCH=${GOARCH} go build -v -o ../bin/docker
+
+example: protos
 	cd example/backend && go build -v -o ../../bin/backend-example
 
+xcli: cli
+	cd cmd && GOOS=linux 	GOARCH=amd64 	go build -v -o ../bin/docker-linux-amd64
+	cd cmd && GOOS=darwin 	GOARCH=amd64 	go build -v -o ../bin/docker-darwin-amd64
+	cd cmd && GOOS=windows 	GOARCH=amd64 	go build -v -o ../bin/docker-windows-amd64.exe
+
+xexample: example
+	cd example/backend && GOOS=linux	GOARCH=amd64	go build -v -o ../../bin/backend-example-linux-amd64
+	cd example/backend && GOOS=darwin	GOARCH=amd64	go build -v -o ../../bin/backend-example-darwin-amd64
+	cd example/backend && GOOS=windows	GOARCH=amd64	go build -v -o ../../bin/backend-example-windows-amd64.exe
+
+dprotos:
+	docker build . \
+	--output type=local,dest=./backend/v1 \
+	--target protos
+
+dbins: dprotos
+	docker build . \
+	--output type=local,dest=./bin \
+	--build-arg TARGET_OS=${GOOS} \
+	--build-arg TARGET_ARCH=${GOARCH} \
+	--target bins
+
+dxbins: dbins
+	docker build . \
+	--output type=local,dest=./bin \
+	--target xbins
+
 FORCE:
 
-.PHONY: protos example cli
+.PHONY: all xall protos example xexample xcli cli bins dbins dxbins dprotos

+ 2 - 2
backend/v1/backend.pb.go

@@ -1,7 +1,7 @@
 // Code generated by protoc-gen-go. DO NOT EDIT.
 // versions:
-// 	protoc-gen-go v1.21.0
-// 	protoc        v3.11.4
+// 	protoc-gen-go v1.21.0-devel
+// 	protoc        v3.6.1
 // source: backend/v1/backend.proto
 
 package v1