Build docker multi stage with files from the first stage

Keywords: linux docker dockerfile docker-multi-stage-build

Question: 

i’ve the following docker file which works OK, I use multistage build and the “runner” uses FROM golang:1.11.4-alpine3.8

FROM golang:1.11.4-alpine3.8 AS builder
ENV SOURCES /github/myapp
RUN apk add --update --no-cache make curl \
    git

ADD https://github.com/golang/dep/releases/download/v0.5.0/dep-linux-amd64 /usr/bin/dep
RUN chmod +x /usr/bin/dep

WORKDIR  $GOPATH/src/${SOURCES}
COPY Gopkg.toml Gopkg.lock ./
RUN dep ensure 

# Copy project
COPY . .

RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix nocgo -o bin .

# Runner
FROM golang:1.11.4-alpine3.8

RUN apk add --update --no-cache curl \

# Install CA cert
RUN curl -sSL -f -k http://aia.pki.vs.com/aia/0CA.crt -o /usr/share/pki/trust/anchors/CA.crt && \
    update-ca-certificates

COPY --from=builder /bin ./

Now I want to change the runner stage to use scratch image and try like following:


FROM golang:1.11.4-alpine3.8 AS builder
ENV SOURCES /github/myapp
RUN apk add --update --no-cache make curl \
    git

ADD https://github.com/golang/dep/releases/download/v0.5.0/dep-linux-amd64 /usr/bin/dep
RUN chmod +x /usr/bin/dep

# Install CA cert
RUN curl -sSL -f -k http://aia.pki.vs.com/aia/0CA.crt -o /usr/share/pki/trust/anchors/CA.crt && \
    update-ca-certificates

WORKDIR  $GOPATH/src/${SOURCES}
COPY Gopkg.toml Gopkg.lock ./
RUN dep ensure 

# Copy project
COPY . .

RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix nocgo -o bin .

# THIS IS THE CHANGE
FROM scratch

RUN apk add --update --no-cache curl \



COPY --from=builder /bin ./
COPY --from=builder /usr/share/pki/trust/anchors/CA.crt /usr/share/pki/trust/anchors/CA.crt
ENTRYPOINT [“./bin”]

The error is

curl: (23) Failed writing body (0 != 1132)

what I need is to copy also (not just bin) the certificate from the first stage to the second stage with scratch image

Answers: