Docker容器未正确设置GOPATH

Docker容器未正确设置GOPATH,docker,go,Docker,Go,当我尝试在Docker容器中运行我的应用程序时出现问题。使用一个简单的go-run main.go,它运行得很好,但是每当我构建一个映像并运行docker容器时,就会出现panic:html/template:pattern不匹配任何文件的错误:*.html,因此我猜docker容器中没有正确设置GOPATH(虽然我使用了其他项目中相同的docker文件,但我没有任何问题)。我在这里有点迷茫,因为我已经使用了一段时间,没有任何问题 我使用gin作为开发的框架 docker文件是: FROM go

当我尝试在Docker容器中运行我的应用程序时出现问题。使用一个简单的
go-run main.go
,它运行得很好,但是每当我构建一个映像并运行docker容器时,就会出现
panic:html/template:pattern不匹配任何文件的错误:*.html
,因此我猜docker容器中没有正确设置GOPATH(虽然我使用了其他项目中相同的docker文件,但我没有任何问题)。我在这里有点迷茫,因为我已经使用了一段时间,没有任何问题

我使用gin作为开发的框架

docker文件是:

FROM golang:alpine as builder

RUN apk update && apk add git && apk add ca-certificates 
# For email certificate
RUN apk add -U --no-cache ca-certificates

COPY . $GOPATH/src/github.com/kiketordera/advanced-performance/
WORKDIR $GOPATH/src/github.com/kiketordera/advanced-performance/

RUN go get -d -v $GOPATH/src/github.com/kiketordera/advanced-performance

# For Cloud Server
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -ldflags="-w -s" -o /go/bin/advanced-performance $GOPATH/src/github.com/kiketordera/advanced-performance

FROM scratch
COPY --from=builder /go/bin/advanced-performance /advanced-performance
COPY --from=builder /go/src/github.com/kiketordera/advanced-performance/media/ /go/src/github.com/kiketordera/advanced-performance/media/

# For email certificate
VOLUME /etc/ssl/certs/ca-certificates.crt:/etc/ssl/certs/ca-certificates.crt
COPY --from=alpine /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/

EXPOSE 8050/tcp

ENV GOPATH /go
ENTRYPOINT ["/advanced-performance"]
主要功能是:

package main

import (
    "fmt"
    "net/http"

    "github.com/gin-gonic/gin"
    i18n "github.com/suisrc/gin-i18n"
    "golang.org/x/text/language"
)

func main() {
    // We create the instance for Gin
    r := gin.Default()

    /* Internationalization for showing the right language to match the browser's  default settings
    */
    bundle := i18n.NewBundle(
        language.English,
        "text/en.toml",
        "text/es.toml",
    )

    // Tell Gin to use our middleware. This means that in every single request (GET, POST...), the call to i18n will be executed
    r.Use(i18n.Serve(bundle))

    // Path to the static files. /static is rendered in the HTML and /media is the link to the path to the  images, svg, css.. the static files
    r.StaticFS("/static", http.Dir("media"))

    // Path to the HTML templates. * is a wildcard
    r.LoadHTMLGlob("*.html")

    // Redirects when users introduces a wrong URL
    r.NoRoute(redirect)

    // This get executed when the users gets into our website in the home domain ("/")
    r.GET("/", renderHome)
    r.POST("/", getForm)
    // Listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")

    r.Run()
}
完整的项目可以在中找到,它是一个使用i18n的简单网站呈现,POST form handler

不相关;它用于“解析导入语句”,在运行可执行文件时不起任何作用(除非您的代码特别引用它!)。这里的问题是
WORKDIR

“清除以前指令创建的任何状态”。这包括
WORKDIR
。例如,如果使用docker文件:

FROM alpine:3.12
WORKDIR /test
copy 1.txt .

FROM alpine:3.12
copy 2.txt .
最终生成的图像将在根文件夹中包含文件
2.txt
(并且没有
/test
文件夹)

在您的
dockerfile
中,您正在将
媒体
文件夹复制到
/go/src/github.com/kiketordera/advanced performance/media/
,假设将设置
WORKDIR
;但情况并非如此(默认为
//code>)。最简单的修复方法是将
COPY--from=builder/go/src/github.com/kiketordera/advanced-performance/media//go/src/github.com/kiketordera/github.com/kiketordera/advanced-performance/media//
更改为
COPY--from=builder/go/src/github.com/kiketordera/advanced-performance/media//

您也在从根文件夹访问文件,因此需要使用
copy--from=builder/go/src/github.com/kiketordera/advanced performance/*.html/
(或类似文件)将这些文件复制到文件夹中。鉴于您正在这样做,最好将所有文件(exe、html文件和媒体文件夹)放入文件夹(例如
/app
)使根文件夹保持干净


注意:无需在第二幅图中设置
GOPATH
;如上所述,在运行可执行文件时,它与此无关。我建议使用模块(可能需要支持GOPATH);这也将使您能够大大缩短路径!

我尝试了您的解决方案,但不幸无效:/Have更新了我的答案(下载了您的回购协议,该更改启动正常)。