由于main.go文件中的本地包导入,构建Docker容器时出现错误。

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

Error building docker container due to local package import in main.go file

问题

我正在尝试为我的Go项目构建一个Docker容器,但是我一直遇到以下错误:

=> ERROR [backend-builder 5/5] RUN --mount=type=cache,mode=0777,target=/go/pkg/mod \
--mount=type=cache,mode=0777,target=/.cache/go-build \
CGO_ENABLED=0 GOOS=linux go build -o serve ./backend/cmd/main.go: \
#17 0.343 backend/cmd/main.go:10:2: no required module provides package \
github.com/LarsDepuydt/peno-entrepreneurship-3d-oclusion/backend/cmd/serve; \
to add it: \
#17 0.343 go get github.com/LarsDepuydt/peno-entrepreneurship-3d-oclusion/backend/cmd/serve \
------
executor failed running [/bin/sh -c CGO_ENABLED=0 GOOS=linux go build -o serve ./backend/cmd/main.go]: exit code: 1

我怀疑错误是由于我的main.go文件导入了项目中的一个名为serve的本地包所致。

这是我的项目结构:

peno-entrepreneurship-3d-oclusion/
│
├── backend/
│   ├── cmd/
│   │   ├── main.go
│   │   └── serve/
│   │       └── serve.go
│   ├── go.mod
│   └── go.sum
│
├── Dockerfile
│
└── ... (其他前端文件和目录)


这是我在main.go中的导入语句:

import (
	"database/sql"
	"fmt"
	"log"
	"net/http"
	"os"

	"github.com/LarsDepuydt/peno-entrepreneurship-3d-oclusion/backend/cmd/serve"
)

这是我的Dockerfile:

################################
# Build backend
################################

FROM dev AS backend-builder

# 在backend-builder阶段更改WORKDIR
WORKDIR /usr/src/app

# 预先复制/缓存go.mod以预先下载依赖项,并仅在后续构建中更改时重新下载它们
COPY ./backend/go.mod ./backend/go.sum ./ 
RUN --mount=type=cache,mode=0777,target=/go/pkg/mod go mod download
#RUN go get github.com/LarsDepuydt/peno-entrepreneurship-3d-oclusion/backend/cmd/serve

COPY . .

# 使用本机go包(CGO_ENABLED)
RUN --mount=type=cache,mode=0777,target=/go/pkg/mod \
    --mount=type=cache,mode=0777,target=/.cache/go-build \
    CGO_ENABLED=0 GOOS=linux go build -o serve ./backend/cmd/main.go


有人可以帮助我解释这里出了什么问题吗?我尝试了很多改变路径的方法,但是无法解决这个错误。

在main.go中,我尝试了这个:"./serve",而不是当前的导入方式。

我还尝试了很多关于WORKDIR和docker文件中路径的方法。但是我一直收到相同的错误,无法解决这个错误。

英文:

I am trying to build a Docker container for my Go project, but I keep encountering the following error:

=> ERROR [backend-builder 5/5] RUN --mount=type=cache,mode=0777,target=/go/pkg/mod \
--mount=type=cache,mode=0777,target=/.cache/go-build \
CGO_ENABLED=0 GOOS=linux go build -o serve ./backend/cmd/main.go: \
#17 0.343 backend/cmd/main.go:10:2: no required module provides package \
github.com/LarsDepuydt/peno-entrepreneurship-3d-oclusion/backend/cmd/serve; \
to add it: \
#17 0.343 go get github.com/LarsDepuydt/peno-entrepreneurship-3d-oclusion/backend/cmd/serve \
------
executor failed running [/bin/sh -c CGO_ENABLED=0 GOOS=linux go build -o serve ./backend/cmd/main.go]: exit code: 1

I suspect that the error is caused by the fact that my main.go file imports a local package called serve from my project.

This is my project structure:

peno-entrepreneurship-3d-oclusion/
│
├── backend/
│   ├── cmd/
│   │   ├── main.go
│   │   └── serve/
│   │       └── serve.go
│   ├── go.mod
│   └── go.sum
│
├── Dockerfile
│
└── ... (other frontend files and directories)


This is my import statement in my main.go:

import (
	"database/sql"
	"fmt"
	"log"
	"net/http"
	"os"

	"github.com/LarsDepuydt/peno-entrepreneurship-3d-oclusion/backend/cmd/serve"
)

This is my docker file:

################################
# Build backend
################################

FROM dev AS backend-builder

# Change the WORKDIR in the backend-builder stage
WORKDIR /usr/src/app

# pre-copy/cache go.mod for pre-downloading dependencies and only redownloading them in subsequent builds if they change
COPY ./backend/go.mod ./backend/go.sum ./
RUN --mount=type=cache,mode=0777,target=/go/pkg/mod go mod download
#RUN go get github.com/LarsDepuydt/peno-entrepreneurship-3d-oclusion/backend/cmd/serve

COPY . .

# Use native go packages (CGO_ENABLED)
RUN --mount=type=cache,mode=0777,target=/go/pkg/mod \
    --mount=type=cache,mode=0777,target=/.cache/go-build \
    CGO_ENABLED=0 GOOS=linux go build -o serve ./backend/cmd/main.go


Could someone please help me explain what goes wrong here? I have tried a lot of things with changing the path but can't get around this error.

In main.go I tried this: "./serve" instead of the current import.

I also tried many things regarding the WORKDIR and the path in the docker file. But I keep getting the same error and can't seem to fix the error.

答案1

得分: 1

Dockerfile中,你使用了以下脚本:

WORKDIR /usr/src/app
COPY ./backend/go.mod ./backend/go.sum ./
RUN --mount=type=cache,mode=0777,target=/go/pkg/mod go mod download

通过这个脚本,go.mod文件位于/usr/src/app的根目录下。

根据你的代码结构,go.mod应该位于/usr/src/app/backend下,而不是/usr/src/app下。

尝试将脚本更改为以下内容:

FROM dev AS backend-builder

WORKDIR /usr/src/app/backend
COPY ./backend/go.mod ./backend/go.sum ./
RUN --mount=type=cache,mode=0777,target=/go/pkg/mod go mod download

WORKDIR /usr/src/app
COPY . .

WORKDIR /usr/src/app/backend
RUN --mount=type=cache,mode=0777,target=/go/pkg/mod \
    --mount=type=cache,mode=0777,target=/.cache/go-build \
    CGO_ENABLED=0 GOOS=linux go build -o serve ./cmd/main.go
英文:

On the Dockerfile, you use the following scripts:

WORKDIR /usr/src/app
COPY ./backend/go.mod ./backend/go.sum ./
RUN --mount=type=cache,mode=0777,target=/go/pkg/mod go mod download

With that, the go.mod file is located right under /usr/src/app, and the same path will become the root path of your go module.

Based on your code structure, the go.mod should be located under /usr/src/app/backend, NOT in /usr/src/app.

Try to change the script to this one below:

FROM dev AS backend-builder

WORKDIR /usr/src/app/backend
COPY ./backend/go.mod ./backend/go.sum ./
RUN --mount=type=cache,mode=0777,target=/go/pkg/mod go mod download

WORKDIR /usr/src/app
COPY . .

WORKDIR /usr/src/app/backend
RUN --mount=type=cache,mode=0777,target=/go/pkg/mod \
    --mount=type=cache,mode=0777,target=/.cache/go-build \
    CGO_ENABLED=0 GOOS=linux go build -o serve ./cmd/main.go

huangapple
  • 本文由 发表于 2023年4月5日 18:24:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75938286.html
匿名

发表评论

匿名网友

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

确定