看来我的Hapi应用正在Docker容器中运行,但无法在浏览器中找到它.我以为docker run -d -p 8080:3000可以做到,但我想没有.我正在引导至docker,并且http:// localhost:8080 / hello或http://192.168.99.100:8080/hello都无法正常工作.
我也尝试过很多变化.
这是我在运行docker inspect< container id>时看到的内容:
Server running at: http://localhost:8080
这是我的Hapi.js服务器:
'use strict';
const Hapi = require('hapi');
// Create a server with a host and port
const server = Hapi.server({
host: 'localhost',port: 3000
});
// Add the route
server.route({
method: 'GET',path:'/hello',handler: function (request,h) {
return 'hello world';
}
});
async function start() {
try {
await server.start();
}
catch (err) {
console.log(err);
process.exit(1);
}
console.log(`App running at: ${server.info.uri}/hello`);
}
start();
这是我的Dockerfile:
FROM node:8.9.3
MAINTAINER My Name <email@email.com>
ENV NODE_ENV=production
ENV PORT=3000
ENV user node
WORKDIR /var/www
COPY package.json yarn.lock ./
RUN cd /var/www && yarn
COPY . .
EXPOSE $PORT
ENTRYPOINT ["yarn","start"]
这是我的package.json:
{
"name": "my-app","version": "1.0.0","repository": "https://github.com/myname/myrepo.git","author": "My Name","license": "MIT","private": true,"dependencies": {
"hapi": "17.2.0"
},"scripts": {
"start": "node ./src/server"
}
}
最佳答案
问题不在于Docker,而在于如何配置节点服务器.
如果绑定到localhost,则只能在docker容器中使用.如果要允许来自Docker主机的连接,请不要提供主机名或使用0.0.0.0.
const server = Hapi.server({
host: '0.0.0.0',port: 3000
});