Sep 28, 2019

[k8s][docker file] multiple stage build

Reference:
https://docs.docker.com/develop/develop-images/multistage-build/


Dockerfile:
FROM golang:1.7.3
WORKDIR /go/src/github.com/vsdmars/example/
RUN go get -d -v golang.org/x/net/html
COPY app.go .
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .

FROM alpine:latest
RUN apk --no-cache add ca-certificates
WORKDIR /root/
COPY --from=0 /go/src/github.com/vsdmars/example/app .
CMD ["./app"]


Build command:
$ docker build -t alexellis2/href-counter:latest .


Name your build stages:
Dockerfile:
FROM golang:1.7.3 AS builder
WORKDIR /go/src/github.com/vsdmars/example/
RUN go get -d -v golang.org/x/net/html
COPY app.go    .
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .

FROM alpine:latest
RUN apk --no-cache add ca-certificates
WORKDIR /root/
WORKDIR /go/src/github.com/vsdmars/example/app .
CMD ["./app"]


Build specific stage:
$ docker build --target builder -t example:latest .


Use an external image as a "stage":
Use the COPY --from instruction to copy from a separate image,
either using the local image name, a tag available locally or on a Docker registry,
or a tag ID.
e.g
COPY --from=nginx:latest /etc/nginx/nginx.conf /nginx.conf


A few scenarios where this might be very powerful are:
- Debugging a specific build stage
- Using a debug stage with all debugging symbols or tools enabled, and a lean production stage
- Using a testing stage in which your app gets populated with test data, but building for production using a different stage which uses real data

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.