Skip to content

Instantly share code, notes, and snippets.

@shovanmaity
Last active May 12, 2020 18:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shovanmaity/8523a193a96722008a3c3f7fa16c5051 to your computer and use it in GitHub Desktop.
Save shovanmaity/8523a193a96722008a3c3f7fa16c5051 to your computer and use it in GitHub Desktop.
delete intermediate container images for docker multi-stage build
FROM golang:1.13.5 as tester
LABEL type=intermediate-container
COPY main.go /go/src/main.go
COPY Makefile /go/src/Makefile
WORKDIR /go/src
RUN make test
FROM golang:1.13.5 as builder
LABEL type=intermediate-container
COPY main.go /go/src/main.go
COPY Makefile /go/src/Makefile
WORKDIR /go/src
RUN make build
FROM alpine:3.8 as cert
LABEL type=intermediate-container
RUN apk --update add ca-certificates
FROM scratch
ENV PATH=/bin
COPY --from=builder /go/src/demo /bin/demo
COPY --from=cert /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
CMD ["demo"]
EXPOSE 8080
package main
import (
"net/http"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
w.Write([]byte("ok"))
})
http.ListenAndServe(":8080", nil)
}
.PHONY: dependency
dependency:
.PHONY: build
build: dependency
@GO111MODULE=off CGO_ENABLED=0 go build -a -ldflags '-extldflags "-static"' -o demo .
.PHONY: test
test: dependency
@GO111MODULE=off go test ./...
.PHONY: image
image: dependency
@docker build -t demo .
.PHONY: prune
prune:
@docker image prune -f --filter label=type=intermediate-container
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment