【问题标题】:Can't get Docker container to run on localhosts it says "connection reset"?无法让 Docker 容器在显示“连接重置”的本地主机上运行?
【发布时间】:2019-12-21 03:46:17
【问题描述】:

无法让 Docker 容器在 localhosts 上运行,它在访问 localhost:8080 时显示“连接重置”。 以下是我所知道的,请耐心等待:

  1. 代码在我运行时在本地运行,我可以看到http://localhost:8080 页面
  2. 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)
}


【问题讨论】:

  • 尝试运行不带“分离”选项的图像(-d 标志)。另外,这里不需要-it。只需运行 docker run -p 8080:8080 --name goserver goserver 即可查看日志和错误消息

标签: docker go


【解决方案1】:

您正在以分离 (-d) 模式启动图像 - 这就是您看不到错误消息的原因。 Dockerfile 的问题很少,应该用 @andre 的回答来解决,但很可能你忘记重建图像并没有看到效果。

我提交此答案是为了建议对您的 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

我已将您的Dockerfile 分为两个阶段:构建和运行。 Build 阶段负责构建服务器二进制文件,run 阶段负责运行它。见https://docs.docker.com/develop/develop-images/multistage-build/
然后我将多个RUNs 合并为一个:go get github.com/gorilla/mux &amp;&amp; go build -o server . 以避免创建冗余层。
我修复了WORKDIRs 并给了它们可读的语义名称。

别忘了用 docker build . -t goserver 重建它并用它运行

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

如果一切正常,并且您已准备好(并且您需要)以分离模式启动,则添加 -d 标志。

另外,您可能需要检查Dockerfile best practices

【讨论】:

  • 现在我通过使用 -d 标志看到了 docker 守护进程的错误(对吗),我得到了这个错误。 “模板解析错误:打开模板/home.html:没有这样的文件或目录”
  • @ReverendRonja 看起来您在main.go 这个问题中的示例与实际文件不同。确保项目目录中的 main.go 与此问题中的相同。我创建了示例存储库来重现您的问题,您可以按照README 中的步骤构建和运行服务器:github.com/g4s8/so59417246
【解决方案2】:

您的WORKDIR 错误,这取决于您设置CMD 的方式

将您的WORKDIR 更改为SRC_DIR 而不是GOBIN,它将起作用

你也可以在你的 Dockerfile 上运行 go install main.go

go install 将创建可执行文件并将其移动到 bin 文件夹

这是一个工作 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

发生的事情是:您的 CMD 失败,因为 WORKDIR 指向 bin 文件夹。

一些旁注:

不要这样做:RUN cd,按照:Docker : RUN cd ... does not work as expected

【讨论】:

  • 为什么我在进行这些更改后仍然被拒绝连接?
猜你喜欢
  • 1970-01-01
  • 2018-03-15
  • 1970-01-01
  • 1970-01-01
  • 2014-12-20
  • 2020-01-20
  • 1970-01-01
  • 2022-01-07
  • 2017-06-13
相关资源
最近更新 更多