Project structure:
project_dir/
├── prj1/
│ ├── feature1/
│ ├─ |─ go.mod
│ ├─ |─ main.go
│ └─ |─ ...
│ └── ...
├── prj2/
│ ├── go.mod
│ ├── main.go
│ └── Dockerfile
│ └── ...
└── ...
The code in prj2 is referencing a function from prj1/feature1 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
WORKDIR /app
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: github.com/name/project/[email protected] (replaced by ../prj1/feature1/): reading /prj1/feature1/go.mod: open /prj1/feature1/go.mod: no such file or directory
So, I added the ADD
command to the docker file to copy prj1. Next, I cd 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?
2
Answers
Considering your replace directive references ../prj1, your ADD should be
And your should execute your docker from
prj2
, not./prj2/Dockerfile
, or yourCOPY go.* ./
would not copy any go file.As commented, that would exclude prj1 from the context. So you must change your COPY to
COPY ./prj2/go.* ./
I assume your docker build command is including the path to use as the build context?
If so, your build is running in the
project_dir/
folder. There’s nogo.mod
file inproject_dir/
.You should
cd
into theprj2/
dir before trying to rungo mod download