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?
CodePudding user response:
Considering your replace directive references ../prj1, your ADD should be
ADD ./prj1 ../prj1
And your should execute your docker from prj2
, not ./prj2/Dockerfile
, or your COPY 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.* ./
CodePudding user response:
I assume your docker build command is including the path to use as the build context?
docker build -f ./prj2/Dockerfile . # <- period
If so, your build is running in the project_dir/
folder. There's no go.mod
file in project_dir/
.
You should cd
into the prj2/
dir before trying to run go mod download
FROM golang:1.17.5 as builder
WORKDIR /app
COPY go.* ./
ADD ./prj1 ./prj1
RUN cd prj2/ && go mod download