登录
首页 >  Golang >  Go问答

Docker 容器缺少正确的 GOPATH 配置

来源:stackoverflow

时间:2024-02-22 16:00:27 178浏览 收藏

哈喽!大家好,很高兴又见面了,我是golang学习网的一名作者,今天由我给大家带来一篇《Docker 容器缺少正确的 GOPATH 配置》,本文主要会讲到等等知识点,希望大家一起学习进步,也欢迎大家关注、点赞、收藏、转发! 下面就一起来看看吧!

问题内容

当我尝试在 docker 容器中运行我的应用程序时遇到问题。使用简单的 go run main.go 就可以正常运行,但是每当我构建图像并运行 docker 容器时,我都会收到 panic: html/template: pattern matches no files: *.html 的错误,所以我猜是 gopath没有在 docker 容器中正确设置(虽然我使用其他项目中的相同 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()
}

完整的项目可以在 https://github.com/kiketordera/advanced-performance 中找到,是一个使用 i18n 和 post 表单处理程序渲染的简单网站


解决方案


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

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

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

FROM alpine:3.12
copy 2.txt .

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

在您的 dockerfile 中,您将 media 文件夹复制到 /go/src/github.com/kiketordera/advanced-performance/media/ ,假设将设置 workdir ;但事实并非如此(默认为 /)。最简单的修复方法是将 copy --from=builder /go/src/github.com/kiketordera/advanced-performance/media/ /go/src/github.com/kiketordera/advanced-performance/media/ 更改为 copy --来自=builder /go/src/github.com/kiketordera/advanced-performance/media/ /media/

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

注意:第二张图片中不需要设置gopath;如上所述,运行可执行文件时它不相关。我建议使用模块(对 gopath 的支持可能是 dropped in 1.17);这也将使您能够大大缩短您的路径!

文中关于的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《Docker 容器缺少正确的 GOPATH 配置》文章吧,也可关注golang学习网公众号了解相关技术文章。

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>