【发布时间】:2021-03-18 23:16:57
【问题描述】:
我的目录是:
-Dockerfile
app/
-main.go
media/
/css
/html
/img
/svg
在 html 文件夹中,我有子文件夹来组织我的 HTML 文件,所以 HTML 文件的路径是 media/html/*/*.html
我的 Dockerfile 如下:
FROM golang:alpine
# Set necessary environmet variables needed for our image
ENV GO111MODULE=on \
CGO_ENABLED=0 \
GOOS=linux \
GOARCH=amd64
# Copy the code into the container
COPY media .
# Move to working directory /build
WORKDIR /build
# Copy the code from /app to the build folder into the container
COPY app .
# Configure the build (go.mod and go.sum are already copied with prior step)
RUN go mod download
# Build the application
RUN go build -o main .
WORKDIR /app
# Copy binary from build to main folder
RUN cp /build/main .
# Export necessary port
EXPOSE 8080
# Command to run when starting the container
CMD ["/app/main"]
而我的 main.go 是:
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
func main() {
// We create the instance for Gin
r := gin.Default()
// 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("../media/html/*/*.html")
r.NoRoute(renderHome)
// This get executed when the users gets into our website in the home domain ("/")
r.GET("/", renderHome)
r.Run(":8080")
}
func renderHome(c *gin.Context) {
c.HTML(http.StatusOK, "my-html.html", gin.H{})
}
问题是,我可以使用go run main.go 在 Golang 中毫无问题地运行我的应用程序,我可以毫无问题地构建 Docker 映像,但是在从映像运行 Docker 容器的那一刻,我得到了错误:
panic: html/template: pattern matches no files: ../media/html/*/*.html
路径是正确的(因为我可以在普通的go 中运行它也被证明是正确的)并且似乎 Docker 没有正确处理文件,或者至少不在正确的目录中。什么是失败?完整的简单项目可以找到here
【问题讨论】:
标签: docker go dockerfile