title: Docker multistage: how to COPY built files between stages?
body: <p>I'm beginner with Docker, and I'm trying to build an image in two stages, as explained here: <a href="https://docs.docker.com/develop/develop-images/multistage-build/" rel="noreferrer">https://docs.docker.com/develop/develop-images/multistage-build/</a></p>
  
  <blockquote>
    <p>You can selectively copy artifacts from one stage to another</p>
  </blockquote>
  
  <p>Looking at the examples given there, I had thought that one could build some files during a first stage, and then make them available for the next one:</p>
  
  <pre><code>FROM golang:1.7.3 AS builder
  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=builder /go/src/github.com/alexellis/href-counter/app .
  CMD ["./app"]  
  </code></pre>
  
  <p>(Example taken from the above-linked page)</p>
  
  <p>Isn't that what the <code>COPY app.go    .</code> and the <code>COPY --from=builder /go/src/github.com/alexellis/href-counter/app .</code> are supposed to do?</p>
  
  <p>I probably have a complete misunderstanding of what is going on, because when I try to do something similar (see below), it seems that the <code>COPY</code> command from the first stage is not able to see the files that have just been built (I can confirm that they have been actually built using a <code>RUN ls</code> step, but then I get a <code>lstat &lt;the file&gt;: no such file or directory</code> error).</p>
  
  <p>And indeed, most other information I can gather regarding <code>COPY</code> (except the examples in the above link) rather suggest that <code>COPY</code> is actually meant to copy files from the directory where the <code>docker build</code> command was launched, not from within the build environment.</p>
  
  <p>Here is my Dockerfile:</p>
  
  <pre><code>FROM haskell:8.6.5 as haskell
  RUN git clone https://gitlab+deploy-token-75:sakyTxfe-PxPHDwqsoGm@gitlab.pasteur.fr/bli/bioinfo_utils.git
  WORKDIR bioinfo_utils/remove-duplicates-from-sorted-fastq/Haskell
  RUN stack --resolver ghc-8.6.5 build &amp;&amp; \
      stack --resolver ghc-8.6.5 install --local-bin-path .
  RUN pwd; echo "---"; ls
  COPY remove-duplicates-from-sorted-fastq .
  
  FROM python:3.7-buster
  RUN python3.7 -m pip install snakemake
  RUN mkdir -p /opt/bin
  COPY --from=haskell /bioinfo_utils/remove-duplicates-from-sorted-fastq/Haskell/remove-duplicates-from-sorted-fastq /opt/bin/remove-duplicates-from-sorted-fastq
  CMD ["/bin/bash"]
  </code></pre>
  
  <p>And here is how the build ends when I run <code>docker build .</code> from the directory containing the Dockerfile:</p>
  
  <pre><code>Step 5/11 : RUN pwd; echo "---"; ls
   ---&gt; Running in 28ff49fe9150
  /bioinfo_utils/remove-duplicates-from-sorted-fastq/Haskell
  ---
  LICENSE
  Setup.hs
  install.sh
  remove-duplicates-from-sorted-fastq
  remove-duplicates-from-sorted-fastq.cabal
  src
  stack.yaml
   ---&gt; f551efc6bba2
  Removing intermediate container 28ff49fe9150
  Step 6/11 : COPY remove-duplicates-from-sorted-fastq .
  lstat remove-duplicates-from-sorted-fastq: no such file or directory
  </code></pre>
  
  <p>How am I supposed to proceed to have the built file available for the next stage?</p>