I do a Dockerfile to put my development in production. However when I run my docker I have this following error :
http: panic serving xxx.xxx.xxx.xxx:xxxxx: open views/public/index.html: no such file or directory
My mistake is in my Dockerfile but I don't see where..
My 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"]
Example for routes "/" in my program :
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)
}
}
}
File Tree :
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