From 492bd6cfff23da64c3ae19cc6154b38301a35af1 Mon Sep 17 00:00:00 2001 From: Will Glynn Date: Sat, 24 Feb 2018 13:26:35 -0600 Subject: [PATCH 1/2] Switch to a multi-stage build process --- Dockerfile | 27 +++++++++++++++++++++++++++ Dockerfile.build | 5 ----- Dockerfile.release | 7 ------- 3 files changed, 27 insertions(+), 12 deletions(-) create mode 100644 Dockerfile delete mode 100644 Dockerfile.build delete mode 100644 Dockerfile.release diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..490cbbd --- /dev/null +++ b/Dockerfile @@ -0,0 +1,27 @@ +FROM golang:1-alpine AS builder + +RUN apk add --no-cache curl + +# Make a /target directory which we'll copy into the target image as a single layer, and +# populate it with some SSL roots +RUN mkdir -p /target/etc/ssl/certs && \ + curl -s -o /target/etc/ssl/certs/ca-certificates.crt https://curl.haxx.se/ca/cacert.pem + +# Copy in the app +ENV CGO_ENABLED=0 +WORKDIR /go/src/github.com/swipely/iam-docker/ +ADD . . + +# Run tests +RUN go test -v ./... + +# Build the app via `go install`, copy the binary to /target/iam-docker, and copy the license file +RUN go install ./... && \ + cp /go/bin/src /target/iam-docker && \ + cp LICENSE /target/ + +# Build the final image +FROM scratch +MAINTAINER Tom Hulihan (hulihan.tom159@gmail.com) +COPY --from=builder /target / +ENTRYPOINT ["/iam-docker"] diff --git a/Dockerfile.build b/Dockerfile.build deleted file mode 100644 index 591eeab..0000000 --- a/Dockerfile.build +++ /dev/null @@ -1,5 +0,0 @@ -FROM golang:1.6 -MAINTAINER Tom Hulihan (hulihan.tom159@gmail.com) -ADD . /go/src/github.com/swipely/iam-docker/ -WORKDIR /go/src/github.com/swipely/iam-docker/ -RUN make get-deps diff --git a/Dockerfile.release b/Dockerfile.release deleted file mode 100644 index 381447d..0000000 --- a/Dockerfile.release +++ /dev/null @@ -1,7 +0,0 @@ -FROM scratch -MAINTAINER Tom Hulihan (hulihan.tom159@gmail.com) -ADD ./LICENSE / -ADD ./dist/iam-docker / -ADD ./dist/ca-certificates.crt /etc/ssl/certs/ -ENV GOMAXPROCS 4 -CMD ["/iam-docker"] From 2dfef39a2b17cd10c9ef2a0b68e14e8404c9931d Mon Sep 17 00:00:00 2001 From: Will Glynn Date: Sat, 24 Feb 2018 13:35:48 -0600 Subject: [PATCH 2/2] Update Makefile + docs to use multi-stage build --- Makefile | 52 ++++++++-------------------------------------------- README.md | 1 - 2 files changed, 8 insertions(+), 45 deletions(-) diff --git a/Makefile b/Makefile index 70c49eb..0099e73 100644 --- a/Makefile +++ b/Makefile @@ -5,69 +5,33 @@ SRC=$(SRCDIR)/... SRCS=$(SRCDIR)/**/*.go MAIN=$(SRCDIR)/main.go TEST_OPTS=-v -DIST=./dist -EXE_NAME=iam-docker -EXE=$(DIST)/$(EXE_NAME) -CACERT=$(DIST)/ca-certificates.crt -CACERT_SRC=https://curl.haxx.se/ca/cacert.pem VERSION_FILE=VERSION VERSION=$(shell cat $(VERSION_FILE)) DOCKER=docker -DOCKER_BUILD_IMAGE_NAME=swipely/iam-docker-build -DOCKER_RELEASE_IMAGE_NAME=swipely/iam-docker +DOCKER_IMAGE_NAME=swipely/iam-docker DOCKER_TAG=$(VERSION) DOCKER_BUILD_IMAGE=$(DOCKER_BUILD_IMAGE_NAME):$(DOCKER_TAG) -DOCKER_RELEASE_IMAGE=$(DOCKER_RELEASE_IMAGE_NAME):$(DOCKER_TAG) -DOCKER_RELEASE_IMAGE_LATEST=$(DOCKER_RELEASE_IMAGE_NAME):latest -DOCKER_BUILD_EXE=/go/src/github.com/swipely/iam-docker/dist/iam-docker -BUILD_DOCKERFILE=Dockerfile.build -RELEASE_DOCKERFILE=Dockerfile.release +DOCKER_IMAGE=$(DOCKER_IMAGE_NAME):$(DOCKER_TAG) +DOCKER_IMAGE_LATEST=$(DOCKER_RELEASE_IMAGE_NAME):latest default: test -clean: - rm -rf $(DIST) - build: $(GO) build $(SRC) test: $(GO) test $(TEST_OPTS) $(SRC) -exe: $(EXE) - get-deps: go get -u github.com/tools/godep -test-in-docker: docker-build - $(DOCKER) run $(DOCKER_BUILD_IMAGE) make test - release: docker git tag $(VERSION) git push origin --tags - docker push $(DOCKER_RELEASE_IMAGE) - docker push $(DOCKER_RELEASE_IMAGE_LATEST) - -docker: docker-build $(CACERT) - $(eval CONTAINER := $(shell $(DOCKER) create $(DOCKER_BUILD_IMAGE) make exe)) - $(DOCKER) start $(CONTAINER) - $(DOCKER) logs -f $(CONTAINER) - mkdir -p $(DIST) - $(DOCKER) cp $(CONTAINER):$(DOCKER_BUILD_EXE) $(EXE) - $(DOCKER) rm -f $(CONTAINER) - $(DOCKER) build -t $(DOCKER_RELEASE_IMAGE) -f $(RELEASE_DOCKERFILE) . - $(DOCKER) tag $(DOCKER_RELEASE_IMAGE) $(DOCKER_RELEASE_IMAGE_LATEST) - -docker-build: $(SRCS) - $(DOCKER) build -t $(DOCKER_BUILD_IMAGE) -f $(BUILD_DOCKERFILE) . - -$(CACERT): $(DIST) - curl -s $(CACERT_SRC) > $(CACERT) - -$(EXE): $(DIST) $(SRCS) - $(GO) build $(GO_BUILD_OPTS) -o $(EXE) $(MAIN) + docker push $(DOCKER_IMAGE) + docker push $(DOCKER_IMAGE_LATEST) -$(DIST): - mkdir -p $(DIST) +docker: $(SRCS) Dockerfile + $(DOCKER) build -t $(DOCKER_IMAGE) . -.PHONY: build clean default docker docker-build exe get-deps release test test-in-docker +.PHONY: build default docker get-deps release test diff --git a/README.md b/README.md index 40f0c33..08e82e4 100644 --- a/README.md +++ b/README.md @@ -95,6 +95,5 @@ Commonly used commands: * `make get-deps` - install the system dependencies * `make test` - run the application tests * `make docker` - build a release Docker image -* `make test-in-docker` - run the tests in Docker All source code is in the `src/` directory.