title: How to prevent docker build from redownloading copied Go vendor
body: <p>I'm trying to make <code>Dockerfile</code> runs faster by copying whole directory (including <code>vendor</code>, because redownloading dependencies took about 10m+ in 3rd world country where I live), but when I tried to run it, it always redownload vendor again and again, unlike when <code>go mod vendor</code> in local:</p>
  <pre><code>FROM golang:1.14-alpine AS builder
  RUN apk --update add ca-certificates git make g++
  ENV GO111MODULE=on
  
  WORKDIR /app
  
  RUN go get github.com/go-delve/delve/cmd/dlv
  
  COPY . .
  
  RUN go mod vendor
  
  ARG COMMIT_HASH
  ENV COMMIT_HASH=${COMMIT_HASH}
  ARG BUILD_DATE
  ENV BUILD_DATE=${BUILD_DATE}
  
  RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
      go build \
      -o app
  
  FROM golang:1.14-alpine
  
  WORKDIR /app
  
  COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
  COPY --from=builder /go/bin/dlv /
  COPY --from=builder /app/app .
  COPY --from=builder /app/db ./db
  EXPOSE 8080 63342
  CMD [ &quot;/dlv&quot;, &quot;--listen=:63342&quot;, &quot;--headless=true&quot;, &quot;--api-version=2&quot;, &quot;--accept-multiclient&quot;, &quot;exec&quot;, &quot;./app&quot; ]
  </code></pre>
  <p>previously using this (without vendor) also slow:</p>
  <pre><code>COPY go.mod .
  COPY go.sum .
  RUN go mod download -x
  COPY . .
  </code></pre>
  <p>trying with this also didn't work:</p>
  <pre><code>COPY vendor /go/pkg/mod
  COPY vendor /go/pkg/mod/cache/download
  COPY go.mod .
  COPY go.sum .
  
  RUN go mod download -x
  
  COPY . .
  </code></pre>
  <p>how to force it to use copied vendor directory instead of redownloading again and again?</p>
  <p>so the expected behavior are:</p>
  <ol>
  <li>when local have <code>vendor</code> (used <code>go mod vendor</code>), the <code>docker build</code> should use it</li>
  <li>but when on CI (since <code>vendor/*/*</code> not committed to the repo) or developer that doesn't have <code>vendor/*/*</code> it should probably redownload everything (I don't really care, since they have good bandwidth)</li>
  </ol>
  <p>the <code>go mod vendor</code> command is for the CI and devs that haven't use <code>go mod vendor</code></p>