Mux路由器在Docker化的golang项目中无法工作。

huangapple go评论90阅读模式
英文:

Mux Router doesn't work in Dockerized golang project

问题

我在本地进行了测试,应用程序在本地完全正常工作。
我可以从POST和GET方法中获取响应。

但是当我将应用程序容器化并尝试测试相同的端点时,我什么都得不到。
我已经尝试了Stackoverflow上的所有建议。所以我将粘贴我的Dockerfile和Makefile,以获取您的意见。

我的Dockerfile:

FROM golang:1.16.5 AS build-env

RUN set -x \
    && apt-get update -y \
    && apt-get install -y locales \
                          make \
                          xz-utils \
                          zip \
    && rm -rf /var/lib/apt/lists/*

FROM build-env AS builder
WORKDIR /build
COPY . .
RUN set -x \
    && make

FROM debian:buster AS runner

RUN set -x \
    && apt-get update -y \
    && apt-get install -y ca-certificates \
    && rm -rf /var/lib/apt/lists/* \
    && update-ca-certificates

COPY --from=builder /build/bin/* .
CMD ["./applicaitonx"]

以及我的Makefile:

build:
    go build -o bin/applicaitonx -v -buildmode=exe .

为了能够构建Docker,我使用以下命令:

docker build -t app .

然后我运行它。

我看到应用程序正在运行,并且listenandserve没有抛出任何错误。但是mux根本没有路由。
它说它在监听,但实际上并没有。
我还将在main.go中复制以下部分:

srv := &http.Server{
    Handler: router,
    Addr: ":" + os.Getenv("PORT"),
}

if err := srv.ListenAndServe(); err != nil {
    log.Fatal("ListenAndServe: ", err)
}

我检查了所有的环境变量,所以os.GetEnv("PORT")不会返回空值。我对此非常确定。
我写了一些调试注释,就我所看到的,代码甚至不执行那些调试注释。所以它根本不打印任何内容。

英文:

I tested locally and it works totally fine on my local.
I can get responses from POST, GET methods.

But when I dockerize the application and try to test the same endpoints, I get nothing.
I have tried all the suggestions herein Stackoverflow. So I will paste my Dockerfile and Makefile to get your opinions on it.

My Dockerfile:

FROM golang:1.16.5 AS build-env

RUN set -x \
    && apt-get update -y \
    && apt-get install -y locales \
                          make \
                          xz-utils \
                          zip \
    && rm -rf /var/lib/apt/lists/*

FROM build-env AS builder
WORKDIR /build
COPY . .
RUN set -x \
    && make

FROM debian:buster AS runner

RUN set -x \
    && apt-get update -y \
    && apt-get install -y ca-certificates \
    && rm -rf /var/lib/apt/lists/* \
    && update-ca-certificates

COPY --from=builder /build/bin/* .
CMD ["./applicaitonx"]

and my Makefile:

build:
    go build -o bin/applicaitonx -v -buildmode=exe .

to be able to build the docker, I'm using the following command;

docker build -t app .

and then I run it.

I see that the application is running and listenandserve doesn't throw an error at all. BUT mux is not routing at all.
It says it listens but it doesn't.
I will also copy the following part in main.go

srv := &http.Server{
	Handler: router,
	Addr: ":" + os.Getenv("PORT"),
}

if err := srv.ListenAndServe(); err != nil {
	log.Fatal("ListenAndServe: ", err)
}

I inspect all the env variables, so os.GetEnv("PORT") doesn't return null. I'm sure about it.
I wrote some debug comments, and as far as I see codes doesn't go even that debug comments. So it doesn't print anything at all.

答案1

得分: 1

可能是因为你在容器内部使用的端口没有对外部公开,所以你无法访问它们。尝试像下面这样添加-p标志:

docker run -p P1:P2

P1 = TCP端口

P2 = 容器端口

请参考:这个文档

英文:

It might be possible that the port you are using inside the container is not exposed to outside world, hence you can't reach them. Try adding -p flag like below:

docker run -p P1:P2

P1 = TCP port

P2 = Container port

Please refer: this doc

huangapple
  • 本文由 发表于 2021年8月16日 21:42:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/68803839.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定