gpt4 book ai didi

node.js - 无法从主机访问 Dockerized Loopback 4 应用程序

转载 作者:搜寻专家 更新时间:2023-10-31 22:44:23 25 4
gpt4 key购买 nike

有没有人成功将 Loopback-4 (lb4) 应用程序 docker 化?我设置了一个基于 lb4 的应用程序并试图将其 dockerize 化,但尽管 Docker 似乎正在运行该应用程序,但它并未显示在我的本地主机上。

我做的步骤:

  1. 在本地设置基于 Loopback 4 的应用
  2. 创建 Dockerfile(代码 here)
  3. (cd 到 Dockerfile 所在的目录)构建:docker build -t lb4。
  4. 运行:docker run -p 3000:3000 lb4

但是,该应用程序未显示在 http://localhost:3000 上运行容器的输出:

trip@1.0.0 prestart /usr/src/app npm run build

trip@1.0.0 build /usr/src/app lb-tsc es2017 --outDir dist

trip@1.0.0 start /usr/src/app node .

Server is running at http://127.0.0.1:3000 Try http://127.0.0.1:3000/ping


编辑

为了留住问题,将repo(第2步)中的相关代码贴在这里,

// index.js
const application = require('./dist');
module.exports = application;
if (require.main === module) {
// Run the application
const config = {
rest: {
port: +process.env.PORT || 3000,
host: process.env.HOST || 'localhost',
openApiSpec: {
// useful when used with OASGraph to locate your application
setServersFromRequest: true,
},
},
};
application.main(config).catch(err => {
console.error('Cannot start the application.', err);
process.exit(1);
});
}

最佳答案

正如@Henry 在评论中所建议的,在您的 index.js 中,更改为 use

  host: '0.0.0.0',

要了解更多本地主机 (127.0.0.1) 和 0.0.0.0 之间的区别,请参阅 https://superuser.com/questions/949428/whats-the-difference-between-127-0-0-1-and-0-0-0-0

附言

最好在构建阶段使用 npm run build 以便在运行时更快地启动。

关于node.js - 无法从主机访问 Dockerized Loopback 4 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53759617/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com