From a49f19e00b273dde399083a85efe2e3140dc9fed Mon Sep 17 00:00:00 2001 From: Vishal Lal Date: Mon, 29 Jan 2018 13:25:42 +0100 Subject: [PATCH] Multistage docker build --- Makefile | 21 +++++++-------------- docker/user/Dockerfile-release | 12 +++++++++++- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/Makefile b/Makefile index 0161f903..badfda6d 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ GROUP = weaveworksdemos TAG=$(TRAVIS_COMMIT) -default: build +default: docker pre: @@ -48,17 +48,14 @@ dockerruntest: dockertestdb dockerdev docker run -d --name my$(TESTDB) -h my$(TESTDB) $(TESTDB) docker run -d --name $(INSTANCE)-dev -p 8084:8084 --link my$(TESTDB) -e MONGO_HOST="my$(TESTDB):27017" $(INSTANCE)-dev -docker: build - cp -rf bin docker/user/ - docker build -t $(NAME) -f docker/user/Dockerfile-release docker/user/ +docker: + docker build -t $(NAME) -f docker/user/Dockerfile-release . -dockerlocal: build - cp -rf bin docker/user/ - docker build -t $(INSTANCE)-local -f docker/user/Dockerfile-release docker/user/ +dockerlocal: + docker build -t $(INSTANCE)-local -f docker/user/Dockerfile-release . -dockertravisbuild: build - cp -rf bin docker/user/ - docker build -t $(NAME):$(TAG) -f docker/user/Dockerfile-release docker/user/ +dockertravisbuild: + docker build -t $(NAME):$(TAG) -f docker/user/Dockerfile-release . docker build -t $(DBNAME):$(TAG) -f docker/user-db/Dockerfile docker/user-db/ if [ -z "$(DOCKER_PASS)" ]; then \ echo "This is a build triggered by an external PR. Skipping docker push."; \ @@ -88,7 +85,3 @@ clean: cleandocker rm -rf bin rm -rf docker/user/bin rm -rf vendor - -build: deps - mkdir -p bin - CGO_ENABLED=0 go build -a -installsuffix cgo -o bin/$(INSTANCE) main.go diff --git a/docker/user/Dockerfile-release b/docker/user/Dockerfile-release index 76b5a186..79f391d5 100644 --- a/docker/user/Dockerfile-release +++ b/docker/user/Dockerfile-release @@ -1,3 +1,13 @@ +FROM golang:1.7-alpine + +COPY . /go/src/github.com/microservices-demo/user/ +WORKDIR /go/src/github.com/microservices-demo/user/ + +RUN apk update +RUN apk add git +RUN go get -v github.com/Masterminds/glide +RUN glide install && CGO_ENABLED=0 go build -a -installsuffix cgo -o /user main.go + FROM alpine:3.4 ENV SERVICE_USER=myuser \ @@ -15,7 +25,7 @@ ENV MONGO_HOST user-db WORKDIR / EXPOSE 80 -COPY bin/user / +COPY --from=0 /user / RUN chmod +x /user && \ chown -R ${SERVICE_USER}:${SERVICE_GROUP} /user && \