title: Docker build : "go: go.mod file not found in current directory or any parent directory"
body: <p>I am trying to build a Dockerfile of my Golang application which includes delve debugger - I want to debug my Golang application in Docker container.</p>
  <p>When I try to build my Docker I consistently go error below:</p>
  <pre><code>Step 5/9 : RUN go build -gcflags &quot;all=-N -l&quot; -o ./feedme
   ---&gt; Running in a0579ec8a85c
  go: go.mod file not found in current directory or any parent directory; see 'go help modules'
  </code></pre>
  <p>The command go build runs well on my local folder(refer tree commands below)</p>
  <pre><code>go build -gcflags &quot;all=-N -l&quot; -o ./feedme
  </code></pre>
  <p>Here are my folder structure and files:</p>
  <pre><code>tree
  .
  ├── Dockerfile
  ├── Makefile
  ├── docker-compose.yml
  └── parsedata-xml-fp.go
  
  0 directories, 4 files
  </code></pre>
  <p>Single application file parsedata-xml-fp.go (I will dismiss it , since I think the error has nothing to do with it)</p>
  <p>My Dockerfile :</p>
  <pre><code>FROM golang:1.17 AS build
  WORKDIR /
  COPY . .
  RUN go install github.com/go-delve/delve/cmd/dlv@latest
  RUN go build -gcflags &quot;all=-N -l&quot; -o ./feedme
  
  EXPOSE 8000 2345 
  
  COPY --from=build /go/bin/dlv /dlv
  COPY --from=build /feedme /feedme
  
  CMD [&quot;/dlv&quot;, &quot;--listen=:2345&quot;, &quot;--headless=true&quot;, &quot;--api-version=2&quot;, &quot;--accept-multiclient&quot;, &quot;exec&quot;, &quot;/feedme&quot;]
  </code></pre>
  <p>Any idea why this error occur and how can I fix it ?
  I searched for it and tried to set some env variable:</p>
  <pre><code>1) RUN GO111MODULE=off/on
  2) RUN CGO_ENABLED=0
  </code></pre>
  <p>Seems like none of them works</p>
  <p><strong>Edit:</strong>
  I modified my Dockerfile added 'go mod init feedme' and I got some new error:</p>
  <p>New Dockerfile</p>
  <pre><code>FROM golang:1.17 AS build
  
  WORKDIR /
  COPY . .
  RUN go mod init feedme
  RUN go mod tidy
  
  RUN GO111MODULE=auto
  RUN go build -gcflags=&quot;all=-N -l&quot; -o /feedme
  
  EXPOSE 8000 2345 
  
  COPY --from=build /go/bin/dlv /dlv
  COPY --from=build /feedme /feedme
  
  CMD [&quot;/dlv&quot;, &quot;--listen=:2345&quot;, &quot;--headless=true&quot;, &quot;--api-version=2&quot;, &quot;--accept-multiclient&quot;, &quot;exec&quot;, &quot;/feedme&quot;]
  </code></pre>
  <p><strong>Error message is</strong> :<strong>invalid from flag value build: pull access denied for build, repository does not exist or may require 'docker login': denied: requested access to the resource is denied</strong></p>
  <pre><code>docker build --network=host -f Dockerfile -t fdebug .
  Sending build context to Docker daemon  7.056MB
  Step 1/10 : FROM golang:1.17 AS build
   ---&gt; 57ac3b44728a
  Step 2/10 : WORKDIR /
   ---&gt; Using cache
   ---&gt; 59325512e59b
  Step 3/10 : COPY . .
   ---&gt; Using cache
   ---&gt; 8e744ca7fb7f
  Step 4/10 : RUN go mod init feedme
   ---&gt; Using cache
   ---&gt; c9a5fd57769c
  Step 5/10 : RUN go mod tidy
   ---&gt; Using cache
   ---&gt; cbc9d9ca142b
  Step 6/10 : RUN go build -gcflags=&quot;all=-N -l&quot; -o /feedme
   ---&gt; Using cache
   ---&gt; 38e79d1b85c9
  Step 7/10 : EXPOSE 8000 2345
   ---&gt; Using cache
   ---&gt; fd4950bcf8c9
  Step 8/10 : COPY --from=build /go/bin/dlv /dlv
  invalid from flag value build: pull access denied for build, repository does not exist or may require 'docker login': denied: requested access to the resource is denied
  </code></pre>