Can't automatically run simple Go web server with Docker container (func (*Template) Execute) -


so trying automatically run simple "hello world" web server in docker container on coreos. error when app tries exectute html template.

here offending code:

func gatehandler(w http.responsewriter, r *http.request) {     fmt.println("entered gatehandler.")     t, _ := template.parsefiles("templates/helloworld.html")     fmt.println("passed parsefiles.")     err := t.execute(w, nil)     fmt.println("passed execute statement.")     if err != nil {         fmt.println(err)     } } 

here 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 run docker container , navigate appropriate url in browser, see following error:

entered gatehandler. passed 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) 

... , on.

however, when remove entry point dockerfile, run container /bin/bash , manually launch app "./app/web", web app runs perfectly. looks t.execute() not passing error should. difference launching web app docker run opposed manually starting within container.

any insights? lot more convenient launch container , not have worry starting web server manually.

thanks.

update:

being new go, ignored fundamental principle of never ignoring returned errors. fixing code gave more useful information.

entered gatehandler. open templates/helloworld.html: no such file or directory passed parsefiles. ... , on 

so means when automatically run web app docker container, can't find template file. still working on figuring out why is.

golang-nuts post: https://groups.google.com/forum/#!topic/golang-nuts/j6jtfpgg6fi

it working directory issue. adding following dockerfile prior entrypoint statement fixed it.

workdir /app 

previously, app trying run in root directory of container , unable find template file relative path in gatehandler.

https://docs.docker.com/reference/builder/#workdir


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -