无法让 Docker 容器在 localhost 上运行,它在访问 localhost:8080 时显示“连接重置”。以下是我所知道的,请耐心等待:
代码在我运行时在本地运行,我可以看到http://localhost:8080页面
Docker 构建命令完成且没有错误
卷曲服务器时出错:
curl -X GET http://localhost:8080
curl: (52) 来自服务器的空回复
docker run -d -p 8080:8080 --name goserver -it goserver
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
这是go代码:
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)
}
猛跑小猪
繁花不似锦
相关分类