dockerdockerfiledocker-multi-stage-build

Docker `file does not exist` Error on multi stage build COPY command


While build the following Dockerfile, I get the error "COPY failed: stat dispatcher: file does not exist" but I am not sure what I am doing wrong.

FROM golang
COPY ["dispatcher.go", "dispatcher.go"]
RUN go build dispatcher.go

FROM scratch
COPY --from=0 dispatcher .

Solution

  • You should use an explicit path when COPYing the dispatcher file. Note that the image golang has /go as working directory.

    FROM golang
    COPY ["dispatcher.go", "dispatcher.go"]
    RUN go build dispatcher.go
    
    FROM scratch
    COPY --from=0 /go/dispatcher /usr/local/bin/dispatcher
    

    Output (with podman but docker should work the same way):

    $ docker build .
    Emulate Docker CLI using podman. Create /etc/containers/nodocker to quiet msg.
    [1/2] STEP 1/3: FROM golang
    [1/2] STEP 2/3: COPY ["dispatcher.go", "dispatcher.go"]
    --> Using cache 0ea9f7534e15e49bf768e986c69bcde27001d26d674d6681ceed3096438b2d35
    --> 0ea9f7534e1
    [1/2] STEP 3/3: RUN go build dispatcher.go
    --> Using cache 357be3e894ec59a274ef5dc7dda237743e4ab1d389ff4d247288b31987be0ae6
    --> 357be3e894e
    [2/2] STEP 1/2: FROM scratch
    [2/2] STEP 2/2: COPY --from=0 /go/dispatcher /usr/local/bin/dispatcher
    [2/2] COMMIT
    --> 942793c6573
    942793c6573dbe63136d329f60ccecd1eea45b40ab61703ec363e6291cc2e9f4