无法从主机访问 Dockerized Loopback 4 应用程序

Dockerized Loopback 4 app not reachable from the host

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

我做的步骤:

  1. 在本地设置基于 Loopback 4 的应用程序
  2. 创建Docker文件(代码here
  3. (cd 进入 Docker 文件所在的目录)构建:docker build -t lb4 .
  4. 运行: docker 运行 -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 中,更改为使用

  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

PS

最好在构建短语中包含 npm run build,以便在 运行 时更快地启动。