docker - 在 docker-compose 中通过 tcp 与 Nest.js 微服务对话

标签 docker tcp docker-compose microservices nestjs

代码和更多文档可以在 repo 中找到

预期行为:

使用和不使用 docker 运行应用程序时都会收到响应。

我得到了什么:

在没有 Docker 的情况下运行应用程序但不在 Docker 内部运行时的响应。

我认为问题可能是:

该应用程序似乎在 docker 内部运行,但端口只是未正确通过。

我已经尝试过的:

  • 制作混合应用。发出 GET 请求,然后在内部传递给 TCP 微服务(这有效,但不是我想要的行为)。
  • 在 docker 容器内运行 yarn start:dev 而不是 yarn start:prod。这没有任何作用,但使用的端口又是相同的。
  • 像这样公开端口:(这什么也没做)
- target: 3000
  published: 3000
  protocol: tcp
  mode: host

最佳答案

this issue ( this comment ) 解释

If no host is specified, NestJS will bind to localhost

这导致我无法连接在 docker 中运行的服务。因此,通过将主机设置为 0.0.0.0,我能够通过 TCP 进行连接。

完整示例:

const app = await NestFactory.createMicroservice(AppModule, {
  transport: Transport.TCP,
  options: {
    host: '0.0.0.0',
    port: 3000
  }
});

关于docker - 在 docker-compose 中通过 tcp 与 Nest.js 微服务对话,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58892667/

相关文章:

networking - Modbus 从站数量限制

docker - 如何在具有共享卷的 2 个容器之间更新文件

docker - 警告 : no logs are available with the 'none' log driver

reactjs - 容器化的webpack-dev-server不响应文件更改

python - 通过 TCP 使用 Python 连接 Teltonika 设备

tcp - IP分片和TCP ACK

docker - Mercure Docker 由于忽略 docker-compose 中的 JWT_KEY 而导致 JWT 签名无效

docker - 删除 Docker 目录中的文件后,有没有办法修复或重新安装 Docker Desktop?

java - Couchbase 错误 : rx. exceptions.OnErrorThrowable$OnNextValue: OnError while emitting onNext value

php - Mariadb Docker 容器拒绝使用数据库架构进行初始化