【问题标题】:Why my docker file does not copy the HTML files?为什么我的 docker 文件不复制 HTML 文件?
【发布时间】: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


    【解决方案1】:

    media 不适合 Docker 文件夹,因为典型的 Linux 容器已经有一个 /media 文件夹。

    但这不是根本原因。

    根本原因是COPY media .media文件夹的内容复制到/。如果您想保留media 文件夹本身(或使用WORKDIR /media),您可能需要COPY media/ /media/

    作为调试工具,您可以使用 shell 作为入口点来运行容器,以便在不启动应用的情况下“环顾四周”:

    docker build . -t test
    docker run -it --rm test sh
    /app # ls /media
    cdrom   floppy  usb                                                                                                                                             
    /app # ls -R /html
    /html:
    website
    
    /html/website:
    my-html.html
    

    如您所见,您的media/html 文件夹位于/html

    还有一些注意事项:

    最好将go mod download 移动到之前 COPY app 以便可以缓存下载的模块:

    FROM golang:alpine
    
    WORKDIR /build
    COPY app/go.mod app/go.sum ./
    RUN go mod download
    
    COPY app .
    RUN go build -o main .
    
    WORKDIR /app
    RUN cp /build/main .
    
    COPY media /media/
    
    EXPOSE 8080
    
    CMD ["/app/main"]
    

    下一步,您可以研究两阶段构建,以不依赖 golang 映像来运行已编译的应用程序(仅在真正构建时需要)。

    【讨论】:

    • 非常感谢。你是对的,问题是媒体的COPY说明,只需将COPY media .`更改为COPY media /media/。非常感谢!!
    • 也感谢您的所有解释:)
    猜你喜欢
    • 2010-10-27
    • 2023-01-13
    • 1970-01-01
    • 1970-01-01
    • 2013-01-06
    • 2019-12-03
    • 2014-06-15
    • 2016-12-06
    • 2021-04-23
    相关资源
    最近更新 更多