GO / Golang 在 Docker 上运行项目时出现问题 – 找不到包(内部文件夹)

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

GO / Golang run project on Docker issues - Cannot find packages (internal folders)

问题

我正在尝试在Docker上运行我的第一个Golang项目。
虽然我成功地运行了一个简单的“hello world”应用程序,但在一个稍微结构化一些的项目中遇到了一些困难。

该项目有一些文件夹,我在main.go文件中导入了这些文件夹。
当我尝试在Docker上运行我的解决方案时,它在加载这些“包”时卡住了。

这是我的Dockerfile:

FROM golang:1.17-alpine

WORKDIR /app

COPY go.mod ./
COPY go.sum ./
RUN go mod download

COPY *.go ./

RUN go env GOROOT
RUN go build -o /my-api

EXPOSE 8080

CMD ["/my-api"]

这是我得到的错误:

Step 8/10 : RUN go build -o /my-api
 ---> Running in c1c9d1058caa
main.go:10:2: package my-api/Repositories is not in GOROOT (/usr/local/go/src/my-api/Repositories)
main.go:11:2: package my-api/Settings is not in GOROOT (/usr/local/go/src/my-api/Settings)

我注意到一个问题是这个路径

/usr/local/go/src/my-api/Settings

应该是

/usr/local/gocode/src/my-api/Settings

在我的本地机器上。

这是我项目结构的一瞥:

GO / Golang 在 Docker 上运行项目时出现问题 – 找不到包(内部文件夹)

有什么想法吗?

英文:

I'm trying to run my first Golang project on Docker.
While I succeeded with a simple "hello world" application, I'm facing some difficulties with a slightly more structured project.

The project has some folders inside which I imported in the main.go file.
When I try to run my solution on Docker, it gets stuck while loading those "packages".

This is my Dockerfile

FROM golang:1.17-alpine

WORKDIR /app

COPY go.mod ./
COPY go.sum ./
RUN go mod download

COPY *.go ./

RUN go env GOROOT
RUN go build -o /my-api

EXPOSE 8080

CMD [ "/my-api" ]

This is the error I get

Step 8/10 : RUN go build -o /my-api
 ---> Running in c1c9d1058caa
main.go:10:2: package my-api/Repositories is not in GOROOT (/usr/local/go/src/my-api/Repositories)
main.go:11:2: package my-api/Settings is not in GOROOT (/usr/local/go/src/my-api/Settings)

One thing I noticed is that this path

/usr/local/go/src/my-api/Settings

should be

/usr/local/gocode/src/my-api/Settings

on my local machine

Here is a glimpse into how my project is structured

GO / Golang 在 Docker 上运行项目时出现问题 – 找不到包(内部文件夹)

Any ideas?

答案1

得分: 2

为什么不使用像下面这样的Dockerfile:

FROM golang:1.17-alpine
RUN apk add build-base

WORKDIR /app

ADD . /app
RUN go build -o /my-api

EXPOSE 8080

CMD ["/my-api"]

这将把本地目录的内容复制到镜像中的/app目录中。

注意:为了构建一个“生产级别”的镜像,我会使用多阶段构建:

FROM golang:1.17-alpine as builder
RUN apk add build-base

WORKDIR /app
ADD . /app
RUN go build -o /my-api

FROM alpine:3.14
COPY --from=builder /my-api /
EXPOSE 8080
CMD ["/my-api"]
英文:

Why not use a Dockerfile like:

FROM golang:1.17-alpine
RUN apk add build-base

WORKDIR /app

ADD . /app
RUN go build -o /my-api

EXPOSE 8080

CMD [ "/my-api" ]

This will copy the contents of the local directory on your local machine to /app in the image.

Note: To build a "production grade" image, I'd use a multistage build:

FROM golang:1.17-alpine as builder
RUN apk add build-base

WORKDIR /app
ADD . /app
RUN go build -o /my-api

FROM alpine:3.14
COPY --from=builder /my-api /
EXPOSE 8080
CMD [ "/my-api" ]

huangapple
  • 本文由 发表于 2021年9月10日 16:28:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/69129376.html
匿名

发表评论

匿名网友

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

确定