title: How to build a golang docker image with go.mod in a sub folder?
body: <p>I have a go project folder as</p>
  <pre><code>my_project
  |- common
      |- library
          |- golang
  |- my_app
      |- Dockerfile
      |- go.mod
      |- go.sum
      |- main.go
  </code></pre>
  <p>The Dockerfile is from google cloud official example:</p>
  <pre><code>FROM golang:1.15-buster as builder
  
  WORKDIR /app
  
  COPY go.* ./
  RUN go mod download
  
  COPY . ./
  
  RUN go build -mod=readonly -v -o server
  
  FROM debian:buster-slim
  RUN set -x &amp;&amp; apt-get update &amp;&amp; DEBIAN_FRONTEND=noninteractive apt-get install -y \
      ca-certificates &amp;&amp; \
      rm -rf /var/lib/apt/lists/*
  
  COPY --from=builder /app/server /server
  
  CMD [&quot;/server&quot;]
      
  </code></pre>
  <p>The go.mod is using a common library in the same project but belongs to a private repository:</p>
  <pre><code>module run
  
  go 1.15
  
  require (
      github.com/my/private/my_project/common/library/golang v0.0.0-00010101000000-000000000000
  )
  
  replace (
      github.com/my/private/my_project/common/library/golang =&gt; ../common/library/golang
  )
  </code></pre>
  <p>When I run docker build under <code>my_app</code> path to build an image, got this error:</p>
  <pre><code>...
   =&gt; ERROR [builder 4/6] RUN go mod download                                                                                                                                                                  1.0s
  ------
   &gt; [builder 4/6] RUN go mod download:
  #13 0.934 go: github.com/my/private/my_project/golang@v0.0.0-00010101000000-000000000000: parsing /common/library/golang/go.mod: open /common/library/golang/go.mod: no such file or directory
  ------
  executor failed running [/bin/sh -c go mod download]: exit code: 1
  </code></pre>
  <p>Why can't it find the <code>go.mod</code> file in the current path but using <code>/common/library/golang/go.mod</code>?</p>