
Create Lightweight Docker Images With Multi Staged Build
Me Before # 1 2 3 4 5 6 7 8 9 10 11 12 FROM golang:alpine WORKDIR /go/src/github.com/santosh/qagine COPY . . RUN go get -d RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -installsuffix cgo -o app . EXPOSE 8080 CMD ["./app"] Me building the image.
$ docker build -t sntshk/qagine . Sending build context to Docker daemon 259.6kB Step 1/7 : FROM golang:alpine ---> 760fdda71c8f Step 2/7 : WORKDIR /go/src/github.com/santosh/qagine ---> Running in e2cf75dd1655 Removing intermediate container e2cf75dd1655 ---> 8b4955b2e885 Step 3/7 : COPY . . ---> 43cd9a41efcb Step 4/7 : RUN go get -d ---> Running in 55c01f67fa99 go: downloading github.com/gorilla/mux v1.7.4 go: downloading go.mongodb.org/mongo-driver v1.3.2 go: downloading github.com/dgrijalva/jwt-go v3.2.0+incompatible go: downloading golang.org/x/crypto v0.0.0-20200420201142-3c4aac89819a go: downloading github.com/pkg/errors v0.8.1 go: downloading github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c go: downloading github.com/golang/snappy v0.0.1 go: downloading github.com/go-stack/stack v1.8.0 go: downloading github.com/klauspost/compress v1.9.5 go: downloading golang.org/x/sync v0.0.0-20190423024810-112230192c58 go: downloading github.com/xdg/stringprep v0.0.0-20180714160509-73f8eece6fdc go: downloading golang.org/x/text v0.3.2 Removing intermediate container 55c01f67fa99 ---> 038b29b80653 Step 5/7 : RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -installsuffix cgo -o app . ---> Running in 122df20d3329 Removing intermediate container 122df20d3329 ---> f4616dc3fe1c Step 6/7 : EXPOSE 8080 ---> Running in 4258f9aac8c6 Removing intermediate container 4258f9aac8c6 ---> b643c7e16c0b Step 7/7 : CMD ["./app"] ---> Running in 948fa65b093b Removing intermediate container 948fa65b093b ---> 621b8e5396ad Successfully built 621b8e5396ad Successfully tagged sntshk/qagine:latest The sweet size of the image is 525MB.