0

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:

  1. The code runs locally when I run it and I am able to see the http://localhost:8080 page
  2. 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)
}


FuXiang Shu
  • 100
  • 8
ReverendRonja
  • 139
  • 1
  • 2
  • 12
  • Try running the image without "detach" option (`-d` flag). Also, you don't need `-it` here. Just run `docker run -p 8080:8080 --name goserver goserver` to see logs and error messages – Kirill Dec 20 '19 at 19:13

2 Answers2

2

You're starting your image in detached (-d) mode - this is why you don't see error messages. There are few issues with the Dockerfile, it should be fixed with @andre answer, but most probably you forgot to rebuild the image and didn't see the effect.

I'm submitting this answer to suggest some improvements of your Dockerfile:

# first stage - builds the binary from sources
FROM golang:1.12.14-alpine3.10 as build

# using build as current directory
WORKDIR /build

# Add the source code:
COPY main.go ./

# install build deps
RUN apk --update --no-cache add git

# downloading dependencies and
# building server binary
RUN go get github.com/gorilla/mux && \
  go build -o server .

# second stage - using minimal image to run the server
FROM alpine:3.10

# using /app as current directory
WORKDIR /app

# copy server binary from `build` layer
COPY --from=build /build/server server

# binary to run
CMD "/app/server"

EXPOSE 8080

I've split your Dockerfile into two stages: build and run. Build stage is responsible for building the server binary, run stage is responsible for running it. See https://docs.docker.com/develop/develop-images/multistage-build/
Then I combined multiple RUNs into single one: go get github.com/gorilla/mux && go build -o server . to avoid creating redundant layers.
I fixed WORKDIRs and give them readable semantical names.

Don't forget to rebuild it with docker build . -t goserver and run it with

docker run -p 8080:8080 --name goserver goserver

If everything is fine, and you're ready to (and you need to) start in the detach mode, then add -d flag.

Also, you may want to check Dockerfile best practices.

Kirill
  • 7,580
  • 6
  • 44
  • 95
  • Now that I am seeing the docker daemon's errors ( is that right) by using the -d flag I get this one error. "template parsing error: open templates/home.html: no such file or directory" – ReverendRonja Dec 23 '19 at 14:08
  • 1
    @ReverendRonja it looks like your example in this question of `main.go` is differ from actual file. Make sure that you have same `main.go` in the project directory as in this question. I created example repository to reproduce your issue, you can follow steps from the `README` to build and run the server: https://github.com/g4s8/so59417246 – Kirill Dec 23 '19 at 15:59
1

your WORKDIR is wrong, based on how you are setting your CMD

change your WORKDIR to SRC_DIR instead of GOBIN and it will work

You could also run go install main.go on your Dockerfile

go install will create the executable and move it to the bin folder

here is an example of a working Dockerfile:

FROM golang:1

ENV SRC_DIR=/go/src/
ENV GOBIN=/go/bin

WORKDIR $SRC_DIR

# Add the source code:
ADD . $SRC_DIR

RUN go get github.com/gorilla/mux;
RUN go install main.go

WORKDIR $GOBIN
ENTRYPOINT ["./main"]

EXPOSE 8080

What was happening is: your CMD was failing because the WORKDIR was pointing to the bin folder.

A few side notes:

Don't do: RUN cd, as per: Docker : RUN cd ... does not work as expected

andre
  • 448
  • 1
  • 3
  • 8