So I am trying to automatically run a simple "hello world" web server in a docker container on CoreOS. But I get an error when the app tries to exectute the HTML template.
Here is the offending code:
func GateHandler(w http.ResponseWriter, r *http.Request) {
fmt.Println("Entered the GateHandler.")
t, _ := template.ParseFiles("templates/helloworld.html")
fmt.Println("Passed the ParseFiles.")
err := t.Execute(w, nil)
fmt.Println("Passed the Execute statement.")
if err != nil {
fmt.Println(err)
}
}
Here is my Dockerfile:
FROM ubuntu:14.04
RUN mkdir app
ADD assets /app/assets
ADD templates /app/templates
ADD web /app/web
ENV PORT 80
EXPOSE 80
ENTRYPOINT ["/app/web"]
When I run the docker container and navigate to the appropriate URL in my browser, I see the following error:
Entered the GateHandler.
Passed the ParseFiles.
2015/03/28 00:10:53 http: panic serving 10.0.2.2:56292: runtime error: invalid memory address or nil pointer dereference
goroutine 5 [running]:
net/http.func·011()
/usr/local/go/src/net/http/server.go:1130 +0xbb
html/template.(*Template).escape(0x0, 0x0, 0x0)
/usr/local/go/src/html/template/template.go:56 +0x3a
html/template.(*Template).Execute(0x0, 0x7f1593124360, 0xc20804c460, 0x0, 0x0, 0x0, 0x0)
/usr/local/go/src/html/template/template.go:75 +0x3d
main.GateHandler(0x7f1593124290, 0xc20804c460, 0xc20803c1a0)
... and so on.
However, when I remove the entry point from the Dockerfile, run the container with /bin/bash and manually launch the app "./app/web", the web app runs perfectly. Looks like t.Execute() is not passing up an error like it should. The only difference is that I am launching the web app from docker run as opposed to manually starting it within the container.
Any insights? It would be a lot more convenient to launch the container and not have to worry about starting the web server manually.
Thanks.
Being new to Go, I ignored the fundamental principle of never ignoring returned errors. Fixing the code gave more useful information.
Entered the GateHandler.
open templates/helloworld.html: no such file or directory
Passed the ParseFiles.
... and so on
So this means that when I automatically run the web app with the Docker container, it can't find the template file. Still working on figuring out why that is.
golang-nuts post: https://groups.google.com/forum/#!topic/golang-nuts/j6JTFpGg6fI
It was a working directory issue. Adding the following to the Dockerfile prior to the ENTRYPOINT statement fixed it.
WORKDIR /app
Previously, the app was trying to run in the root directory of the container and was unable to find the template file with the relative path in the GateHandler.