【发布时间】:2020-10-11 09:46:13
【问题描述】:
我的 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"
}
}
【问题讨论】: