node.js-节点Docker运行,但看不到该应用程序

前端之家收集整理的这篇文章主要介绍了node.js-节点Docker运行,但看不到该应用程序 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

看来我的Hapi应用正在Docker容器中运行,但无法在浏览器中找到它.我以为docker run -d -p 8080:3000可以做到,但我想没有.我正在引导至docker,并且http:// localhost:8080 / hello或http://192.168.99.100:8080/hello都无法正常工作.

我也尝试过很多变化.

这是我在运行docker inspect< container id>时看到的内容

  1. Server running at: http://localhost:8080

这是我的Hapi.js服务器:

  1. 'use strict';
  2. const Hapi = require('hapi');
  3. // Create a server with a host and port
  4. const server = Hapi.server({
  5. host: 'localhost',port: 3000
  6. });
  7. // Add the route
  8. server.route({
  9. method: 'GET',path:'/hello',handler: function (request,h) {
  10. return 'hello world';
  11. }
  12. });
  13. async function start() {
  14. try {
  15. await server.start();
  16. }
  17. catch (err) {
  18. console.log(err);
  19. process.exit(1);
  20. }
  21. console.log(`App running at: ${server.info.uri}/hello`);
  22. }
  23. start();

这是我的Dockerfile:

  1. FROM node:8.9.3
  2. MAINTAINER My Name <email@email.com>
  3. ENV NODE_ENV=production
  4. ENV PORT=3000
  5. ENV user node
  6. WORKDIR /var/www
  7. COPY package.json yarn.lock ./
  8. RUN cd /var/www && yarn
  9. COPY . .
  10. EXPOSE $PORT
  11. ENTRYPOINT ["yarn","start"]

这是我的package.json:

  1. {
  2. "name": "my-app","version": "1.0.0","repository": "https://github.com/myname/myrepo.git","author": "My Name","license": "MIT","private": true,"dependencies": {
  3. "hapi": "17.2.0"
  4. },"scripts": {
  5. "start": "node ./src/server"
  6. }
  7. }
最佳答案
问题不在于Docker,而在于如何配置节点服务器.

如果绑定到localhost,则只能在docker容器中使用.如果要允许来自Docker主机的连接,请不要提供主机名或使用0.0.0.0.

  1. const server = Hapi.server({
  2. host: '0.0.0.0',port: 3000
  3. });

猜你在找的Docker相关文章