Docker build first stage in multi stage pipeline


I’m currently writing a generic script to automatically cache stages for a multi stage dockerfile

The way I envision it to work is to automatically go through a dockerfile, find all the different stages (basically search for FROM .... AS ... Once the code finds this, I can then do a docker build where I tag this specific stage as something like imagename-stage:firststage.

However as the documentation describes you can also have multi-stage docker files without a name:


FROM golang:1.7.3
WORKDIR /go/src/
RUN go get -d -v  
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/ .
CMD ["./app"]  

In the example above you refer to the previous stage by adding --from=0


When i try to target the first stage in a dockerfile that contains no AS ... I would expect I could do something like:

docker build . --target 0

However when i do this, I get the following error:

Sending build context to Docker daemon  49.99MB
Error response from daemon: failed to reach build target 0 in Dockerfile

Does someone know how to resolve this?