Docker Community Forums

Share and learn in the Docker community.

Docker build first stage in multi stage pipeline

Introduction

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:

Example:

FROM golang:1.7.3
WORKDIR /go/src/github.com/alexellis/href-counter/
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/alexellis/href-counter/app .
CMD ["./app"]  

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

Problem

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?