【发布时间】:2021-06-23 13:59:04
【问题描述】:
我做了一个 Dockerfile 来将我的开发投入生产。 但是,当我运行 docker 时,出现以下错误:
http: panic serving xxx.xxx.xxx.xxx:xxxxx: open views/public/index.html: 没有这样的文件或目录
我的错误在我的 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
【问题讨论】:
-
您只是将二进制文件复制到图像中,而不是 HTML 文件。
-
是的,但是如果我复制视图和静态文件,我也会遇到同样的问题
标签: docker go dockerfile