title: How to add local dependencies in Go Docker multi-stage build?
body: <p>How can I add local dependencies from a folder within the same Go project?</p>
  
  <p>I have the following directory structure:</p>
  
  <pre><code>.
  ├── backend
  │   ├── Dockerfile
  │   ├── conduit
  │   │   └── get_data.go
  │   ├── main.go
  │   ├── main_test.go
  │   ├── storage
  │   │   ├── create_client.go
  │   │   └── read_data.go
  ├── cron_job
  │   ├── Dockerfile
  │   ├── main.go
  │   └── main_test.go
  </code></pre>
  
  <p>And Dockerfile:</p>
  
  <pre><code>FROM golang:1.10.1 as builder
  ADD . github.com/dalderman77/phacts/backend/conduit
  ADD . github.com/dalderman77/phacts/backend/storage
  WORKDIR /go/src/github.com/dalderman77/phacts/cron_job
  # Get dependencies
  RUN go get -d -v cloud.google.com/go/datastore golang.org/x/net/context
  COPY . .
  # Compile Go binary
  RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main .
  
  # Create smaller lightweight container
  FROM alpine:latest  
  RUN apk --no-cache add ca-certificates
  WORKDIR /root/
  # Copy binary from previous stage
  COPY --from=builder /go/src/github.com/dalderman77/phacts/cron_job/main .
  CMD ["./main"]  
  </code></pre>
  
  <p>What I have so far is based on <a href="https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds" rel="nofollow noreferrer">Use multi-stage builds</a></p>
  
  <p>The issue is that compiled binary is dependent on code from the backend directory. It compiles fine locally but I am unsure how to correctly add these dependencies so they can be compiled within the container:</p>
  
  <pre><code>main.go:9:2: cannot find package "github.com/dalderman77/phacts/backend/conduit" in any of:
          /usr/local/go/src/github.com/dalderman77/phacts/backend/conduit (from $GOROOT)
          /go/src/github.com/dalderman77/phacts/backend/conduit (from $GOPATH)
  main.go:10:2: cannot find package "github.com/dalderman77/phacts/backend/storage" in any of:
          /usr/local/go/src/github.com/dalderman77/phacts/backend/storage 
  </code></pre>