Running "go mod download" from parent directory

Project structure:

├── prj1/
│   ├── go/
│   ├─    |─ go.mod
│   ├─    |─ main.go
│   └─    |─ ...
│   └── ...
├── prj2/
│   ├── go.mod
│   ├── main.go
│   └── Dockerfile
│   └── ...
└── ...

The code in prj2 is referencing a function from prj1 via the go mod edit -replace command. Now, this works fine locally but I need to deploy with Docker and I am facing issues.

My docker scripts look like this:

FROM golang:1.17.5 as builder


COPY go.* ./
ADD ./prj1 ./prj1
RUN go mod download

Initially, I was running docker build . from inside prj2 without the ADD command in the docker file but RUN go mod download fails with:

go mod download: (replaced by …/prj1/go/): reading /prj1/go/go.mod: open /prj1/go/go.mod: no such file or directory

So, I added the ADD command to the docker file to copy prj1. Next, I went back to project_dir and ran docker build -f ./prj2/Dockerfile but RUN go mod download is now failing with

go mod download: no modules specified (see ‘go help mod download’)

So basically, my question is, how do I tell the go command that the go.mod is inside prj2?

Correct project structure

The “replace” in your prj2/go.mod is the relative path? probably you could try
replace … => /app/prj1

@mason-liu, the relace is in this format:
replace => …/proj1/go/

Hi @ozuf,

go mod download must be run in the directory where go.mod is. (In other words, inside the main module`. For docker, this would probably mean that the go.mod file must be in the WORKDIR (or WORKDIR must be where the go.mod file is).

Regarding the other error, are the three dots in .../prj1/go intentional? I’d reckon there should be only two dots, which would match the project structure.

I think
replace => /app/proj1/go
could be working since the whole path of proj1 is under /app

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.