如何从父目录运行”go mod download”命令?

huangapple go评论92阅读模式
英文:

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之前,你应该先cdprj2/目录中。

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 .  # &lt;- 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/ &amp;&amp; go mod download

huangapple
  • 本文由 发表于 2021年12月31日 15:48:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/70540123.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定