main.go: 没有提供所需模块的包

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

main.go: no required module provides package

问题

我的Go模块存储在GitHub上,在本地成功编译;然而,如果我尝试通过Docker进行编译,即使在同一个文件夹中本地编译,我会收到一个错误,指出我的本地包不存在,对于子文件夹中的每个本地导入:

=> ERROR [build 7/7] RUN go build -o myrepo-test .                                                                                                                                                                                                                          0.6s 
------
 > [build 7/7] RUN go build -o myrepo-test .:
#14 0.535 main.go:10:2: no required module provides package github.com/myuser/myrepo-test/common; to add it:
#14 0.535       go get github.com/myuser/myrepo-test/common
#14 0.535 main.go:13:2: no required module provides package github.com/myuser/myrepo-test/scraper/data/process; to add it:
#14 0.535       go get github.com/myuser/myrepo-test/scraper/data/process
(....)

这是我的go.mod文件:

module github.com/myuser/myrepo-test

go 1.16

以及Docker文件:

# 使用alpine以减小镜像大小
FROM golang:1.16-buster AS build

WORKDIR /app

# 下载所需的Go依赖项
COPY go.mod ./
COPY go.sum ./
RUN go mod download

COPY *.go ./

# 失败
RUN go build -o myrepo-test .

##########
# 部署 #
##########

FROM gcr.io/distroless/base-debian10

WORKDIR /

COPY --from=build /myrepo-test /myrepo-test

USER nonroot:nonroot

ENTRYPOINT ["/myrepo-test"]

CMD ["/myrepo-test"]
英文:

My Go module, stored in GitHub, successfully compiles locally; however, if I try to do it via docker, even locally in the same folder, I get an error complaining that my local package does not exist, for every local import in a subfolder:

=> ERROR [build 7/7] RUN go build -o myrepo-test .                                                                                                                                                                                                                          0.6s 
------
 > [build 7/7] RUN go build -o myrepo-test .:
#14 0.535 main.go:10:2: no required module provides package github.com/myuser/myrepo-test/common; to add it:
#14 0.535       go get github.com/myuser/myrepo-test/common
#14 0.535 main.go:13:2: no required module provides package github.com/myuser/myrepo-test/scraper/data/process; to add it:
#14 0.535       go get github.com/myuser/myrepo-test/scraper/data/process
(....)

Here is my go.mod:

module github.com/myuser/myrepo-test

go 1.16

And the docker file:

# use alpine due to its small footprint
FROM golang:1.16-buster AS build

WORKDIR /app

# download the required Go dependencies
COPY go.mod ./
COPY go.sum ./
RUN go mod download

COPY *.go ./

# FAIL
RUN go build -o myrepo-test .

##########
# Deploy #
##########

FROM gcr.io/distroless/base-debian10

WORKDIR /

COPY --from=build /myrepo-test /myrepo-test

USER nonroot:nonroot

ENTRYPOINT ["/myrepo-test"]

CMD ["/myrepo-test"]

答案1

得分: 17

> #14 0.535 main.go:10:2: 找不到所需的模块来提供 github.com/myuser/myrepo-test/common 包;要添加它,请运行以下命令:<br/>
> #14 0.535 go get github.com/myuser/myrepo-test/common

上述错误提示意味着你的源代码中有一个名为 common 的包。但是 COPY *.go ./ 不会将这些文件夹添加到 Docker 构建容器中,它只会将当前目录中的 go 文件复制到 Docker 构建中。由于 go build 在容器中找不到源代码中的 common 包,它会尝试从 github 下载该包,因此构建失败。

因此,正确的解决方案如下:

Dockerfile:

FROM golang:1.16-buster AS build

WORKDIR /app

# 下载所需的 Go 依赖
COPY go.mod ./
COPY go.sum ./
RUN go mod download
#COPY *.go ./
COPY . ./

RUN ls

RUN go build -o myrepo-test .

上述解决方案将包括 common 包在内的所有源代码添加到容器中,然后构建应该可以成功。

英文:

> #14 0.535 main.go:10:2: no required module provides package github.com/myuser/myrepo-test/common; to add it:<br/>
> #14 0.535 go get github.com/myuser/myrepo-test/common

Above implies you have a package common in your source. But COPY *.go ./ won't add these folders to docker build container, it will just copy go files in current directory into docker build. As go build can't find the package common in your source in container, it will try to download it from github, so the build fails.

Then, the correct solution is as next:

Dockerfile:

FROM golang:1.16-buster AS build

WORKDIR /app

# download the required Go dependencies
COPY go.mod ./
COPY go.sum ./
RUN go mod download
#COPY *.go ./
COPY . ./

RUN ls

RUN go build -o myrepo-test .

Above will add all your sources to container including the package common etc, then build could be ok.

答案2

得分: 2

您可能还有其他文件夹,因此需要复制整个文件夹和文件进行构建阶段。

此外,
Go是一种编译语言。这意味着它将生成一个独立的可执行二进制文件。您需要进行多阶段部署以充分利用它。之前的答案只是进行构建,如果这样做,Go编程语言及其库将成为Docker镜像的一部分,并将将Docker镜像大小推高到400-500 MB。如果进行多阶段构建,这将大大减小镜像大小,只有15-50 MB。想象一下!

以下是如何操作!

阶段1:构建:第一阶段生成可执行文件。

阶段2:部署:第二阶段复制可执行文件和任何环境变量(可以跳过)。

Dockerfile 可以如下所示:

# 构建
FROM golang:alpine3.17 AS build
WORKDIR /app

COPY go.mod .
COPY go.sum .
RUN go mod download
COPY . .
RUN go build -o /my-go-app

# 部署
FROM alpine:latest
WORKDIR /app

COPY .env .
COPY --from=build /my-go-app .

ENTRYPOINT ["/app/my-go-app"]
英文:

You probably have some other folders, hence copy the entire folders and files for the build stage.

Additionally,
Go is a compiled language. What that means is, it will generate a standalone binary executable. You need to do a multi-stage deployment to take full advantage of that. The previous answers only do the build, if you do that, the Go programming language and its libraries will be part of the Docker image and will push the Docker image size to 400-500 MB. If you do the multi-stage build, this will reduce image size drastically to 15-50 MB. Imagine that!

Here's how you do it!

Stage 1: Build: First stage generates the executable.

Stage 2: Deploy: Second stage copies the executable and any environment variables (can skip).

The Dockerfile may look as follows:

# Build
FROM golang:alpine3.17 AS build
WORKDIR /app

COPY go.mod .
COPY go.sum .
RUN go mod download
COPY . .
RUN go build -o /my-go-app

# Deploy
FROM alpine:latest
WORKDIR /app

COPY .env .
COPY --from=build /my-go-app .

ENTRYPOINT [&quot;/app/my-go-app&quot;]

huangapple
  • 本文由 发表于 2021年8月8日 21:01:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/68701039.html
匿名

发表评论

匿名网友

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

确定