gpt4 book ai didi

Dockerfile : no such file or directory

转载 作者:行者123 更新时间:2023-12-04 07:28:25 24 4
gpt4 key购买 nike

我做了一个 Dockerfile 来将我的开发投入生产。
但是,当我运行 docker 时,出现以下错误:

http: panic serving xxx.xxx.xxx.xxx:xxxxx: open views/public/index.html: no such file or directory


我的错误在我的 Dockerfile 中,但我看不到在哪里..
我的 Dockerfile :
FROM golang:alpine as builder
RUN apk update && apk add --no-cache git ca-certificates gcc g++ make && update-ca-certificates
RUN adduser -D -g '' appuser
WORKDIR /usr/src/app
COPY . .
RUN go mod download
RUN go mod verify
WORKDIR /usr/src/app/cmd/web
RUN make docker
FROM scratch
WORKDIR /
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=builder /etc/passwd /etc/passwd
COPY --from=builder /usr/src/app/cmd/web/web /web
USER appuser
ENTRYPOINT ["./web"]
CMD [":9000"]
我的程序中路由“/”的示例:
func (s *server) handleIndex() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
auth, _ := strconv.ParseBool(s.GetSessionValue(w, r, "authenticated"))
files := []string{
"views/public/index.html",
"views/public/nav.html",
}
templates := template.Must(template.ParseFiles(files...))
if err := templates.ExecuteTemplate(w, "index", authData{Authenticated: auth, NotAuthenticated: !auth}); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
}
文件树:
ants-map/

┣ auth/
┣ cmd/
┃ ┗ web/
┃ ┣ static/
┃ ┣ views/ .html file here
┃ ┣ Makefile
┃ ┣ main.go
┃ ┣ routes.go
┃ ┣ routes_public.go
┃ ┗ server.go
┣ database/
┣ .gitignore
┣ Dockerfile
┣ README.md
┣ go.mod
┗ go.sum

最佳答案

您需要将静态和 View 目录添加到最终图像中,为此在 USER appuser 之前添加以下 2 行:

COPY --from=builder /usr/src/app/cmd/web/views /web/views
COPY --from-builder /usr/src/app/cmd/web/static /web/static

关于Dockerfile : no such file or directory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68101331/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com