Can't get Docker container to run on localhosts it says "connection reset" when going to localhost:8080. Here is what I do know so bear with me:
- The code runs locally when I run it and I am able to see the http://localhost:8080 page
- The Docker build command completes with no errors
Error when curling the server:
curl -X GET http://localhost:8080
curl: (52) Empty reply from server
docker run -d -p 8080:8080 --name goserver -it goserver
The Dockerfile:
FROM golang:1.9.2
ENV SRC_DIR=/go/src/
ENV GOBIN=/go/bin
WORKDIR $GOBIN
# Add the source code:
ADD . $SRC_DIR
RUN cd /go/src/;
RUN go get github.com/gorilla/mux;
CMD ["go","run","main.go"]
#ENTRYPOINT ["./main"]
EXPOSE 8080
Here is the go code:
package main
import (
"fmt"
"net/http"
"github.com/gorilla/mux"
)
func main() {
r := mux.NewRouter()
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "<h1>This is the homepage. Try /hello and /hello/Sammy\n</h1>")
})
r.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "<h1>Hello from Docker!\n</h1>")
})
r.HandleFunc("/hello/{name}", func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
title := vars["name"]
fmt.Fprintf(w, "<h1>Hello, %s!\n</h1>", title)
})
http.ListenAndServe(":8080", r)
}