英文:
How to run "go mod download" from parent directory?
问题
项目结构:
project_dir/
├── prj1/
│ ├── feature1/
│ ├─ |─ go.mod
│ ├─ |─ main.go
│ └─ |─ ...
│ └── ...
├── prj2/
│ ├── go.mod
│ ├── main.go
│ └── Dockerfile
│ └── ...
└── ...
prj2中的代码通过go mod edit -replace命令引用了prj1/feature1中的一个函数。现在,在本地运行正常,但是我需要使用Docker部署时遇到了问题。
我的Docker脚本如下:
FROM golang:1.17.5 as builder
WORKDIR /app
COPY go.* ./
ADD ./prj1 ./prj1
RUN go mod download
...
最初,我在prj2内部运行docker build .
,但是在docker文件中没有ADD
命令,而RUN go mod download
失败,报错信息如下:
> go mod download: github.com/name/project/prj1@v0.0.0-00010101000000-000000000000 (replaced by ../prj1/feature1/): reading /prj1/feature1/go.mod: open /prj1/feature1/go.mod: no such file or directory
因此,我在docker文件中添加了ADD
命令来复制prj1。接下来,我进入project_dir并运行docker build -f ./prj2/Dockerfile
,但是RUN go mod download
现在失败了,报错信息如下:
> go mod download: no modules specified (see 'go help mod download')
所以,我的问题是,如何告诉go命令go.mod文件在prj2内部?
英文:
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/prj1@v0.0.0-00010101000000-000000000000 (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?
答案1
得分: 2
考虑到你的替换指令引用了 "../prj1",你的 ADD 应该是:
ADD ./prj1 ../prj1
并且你应该从 prj2
执行你的 Docker,而不是 ./prj2/Dockerfile
,否则你的 COPY go.* ./
将不会复制任何 go 文件。
正如注释所述,这将排除上下文中的 prj1。所以你必须将你的 COPY 改为 COPY ./prj2/go.* ./
。
英文:
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.* ./
答案2
得分: 1
我假设你的Docker构建命令中包含要用作构建上下文的路径?
docker build -f ./prj2/Dockerfile . # <- period
如果是这样的话,你的构建将在project_dir/
文件夹中运行。在project_dir/
中没有go.mod
文件。
在尝试运行go mod download
之前,你应该先cd
到prj2/
目录中。
FROM golang:1.17.5 as builder
WORKDIR /app
COPY go.* ./
ADD ./prj1 ./prj1
RUN cd prj2/ && go mod download
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论