问题描述
有没有人成功地对Loopback-4(lb4)应用进行docker化?我设置了一个基于lb4的应用程序,并试图对其进行docker化,但是尽管Docker似乎正在运行该应用程序,但它并未显示在我的本地主机上.
has anyone successfully dockerize a Loopback-4 (lb4) app?I set up an lb4 based app and am trying to dockerize it, but although the Docker seems to be running the app, it's not showing on my localhost.
我执行的步骤:
- 在本地设置基于Loopback 4的应用程序
- 创建Dockerfile(代码此处)
- (cd进入Dockerfile所在的目录)构建:docker build -t lb4.
- 运行:docker run -p 3000:3000 lb4
- Setup Loopback 4 based app locally
- Create Dockerfile (code here)
- (cd into the dir where Dockerfile is) Build: docker build -t lb4 .
- Run: docker run -p 3000:3000 lb4
但是,该应用程序未显示在 http://localhost:3000 上运行容器的输出:
But, the app doesn't show up on http://localhost:3000The output from running the container:
trip@1.0.0构建/usr/src/app lb-tsc es2017 --outDir dist
trip@1.0.0 build /usr/src/app lb-tsc es2017 --outDir dist
trip@1.0.0开始/usr/src/app 节点.
trip@1.0.0 start /usr/src/app node .
服务器在 http://127.0.0.1:3000 上运行 尝试 http://127.0.0.1:3000/ping
Server is running at http://127.0.0.1:3000 Try http://127.0.0.1:3000/ping
编辑
EDIT
为了保留问题,将回购中的相关代码(第2步)粘贴到了这里,
For the sake of conserving the question, relevant code in the repo (step 2) is pasted here,
// 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中,更改为使用
As suggested by @Henry in the comment, in your index.js, change to use
host: '0.0.0.0',
要了解有关localhost(127.0.0.1)和0.0.0.0之间的区别的更多信息,请参见
To know more about the different between localhost (127.0.0.1) and 0.0.0.0, see https://superuser.com/questions/949428/whats-the-difference-between-127-0-0-1-and-0-0-0-0
PS
最好在构建阶段使用npm run build
,以便在运行时更快地启动.
It's better to have npm run build
during the build phrase for faster start at run time.
这篇关于无法从主机访问Dockerized Loopback 4应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!