"Dockerized" Node.js 应用程序未响应主机
"Dockerized" Node.js Application Not Responding to Host
我正在尝试 "Dockerize" 一个相当大、复杂的 Node.js 应用程序,其结构大致如下:
Nebula [项目根目录; 运行是 Node.js 应用程序]
|__CosmosD3 [包含所有 HTML 和 JS 文件的目录]
|__Nebula-Pipeline [包含执行所有繁重计算的大多数 Python 文件的目录]
我们这个项目的标准安装过程相当复杂,因为所有相互关联的部分都必须完美地协同工作,以使我们的前端 JS 文件能够与我们的后端 Python 脚本进行通信。对于 Linux/Unix 系统,这是粗略的过程(使用 Nebula 目录中的命令 运行):
- 安装Java。 (几乎任何版本都应该这样做;我们有一个 Java 文件需要 运行 用于前端的特定交互)
- 安装 Python 2.7 和 pip
- 运行
pip install --upgrade pip
确保使用最新的 pip
- 使用
sudo apt-get install libzmq-dev npm nodejs-legacy
安装 Node.js、npm 和 zmq
- 运行
npm install
安装我们的 Node.js 包依赖项
- 运行
pip install -e ./Nebula-Pipeline
安装所有 Python 依赖项
- 祈祷一切顺利,运行 申请
npm start
。默认端口为 8081
,因此应可通过 localhost:8081
. 访问应用程序
已经有一个与此非常相似的项目 "Dockerized." 通过调整另一个项目的 Docker 文件以更接近上述步骤,这就是 Docker我创建的文件:
FROM ubuntu:16.04
RUN mkdir /www
WORKDIR /www
RUN apt-get update
RUN apt-get install -y libzmq-dev npm nodejs-legacy python python-pip
RUN apt-get update && \
apt-get install -y openjdk-8-jdk && \
apt-get install -y ant && \
apt-get clean;
# Fix certificate issues, found as of
# https://bugs.launchpad.net/ubuntu/+source/ca-certificates-java/+bug/983302
RUN apt-get update && \
apt-get install ca-certificates-java && \
apt-get clean && \
update-ca-certificates -f;
# Setup JAVA_HOME, this is useful for docker commandline
ENV JAVA_HOME /usr/lib/jvm/java-8-openjdk-amd64/
RUN export JAVA_HOME
##COPY ./lib/ /opt/lib
RUN pip install --upgrade pip
COPY ./Nebula /www
RUN ls -a /www
RUN npm install
RUN pip install -e /www/Nebula-Pipeline
EXPOSE 8081
CMD ["npm", "start"]
当我 运行 这个 Docker 带有 docker build -t nebulaserver
的文件时,图像似乎创建成功。当我 运行 带有 docker run -p 8081:8081 nebulaserver
的图像时,我得到以下打印输出,似乎表明一切实际上都在正常工作。 (请注意,"PORT: 8081" 打印输出是对 Node.js 应用程序正在使用的端口的确认。)
Michelles-MacBook-Pro-8$ docker run -p 8081:8081 nebulaserver
> Nebula@0.0.1 start /www
> node app.js
PORT:
8081
但是,当我随后尝试连接到 localhost:8081
时,我没有收到任何响应。此外,当我的 Node.js 服务器收到对 HTML 页面之一的请求时,我希望看到它的其他打印输出。我也没有看到任何这些打印输出。就好像端口转发没有正常工作一样。根据我读过的所有内容,我应该正确地做事,但我以前从未使用过 Docker,所以也许我遗漏了什么?
有人知道我做错了什么吗?
编辑:
这是我的 app.js 文件,以防它有助于弄清楚发生了什么...
/*Import packages required in package.json */
/*Add these packages from the ../node_modules path*/
var express = require('express');//A lightweight nodejs web framework
var path = require('path');//Ability to join filepaths to filenames.
var favicon = require('serve-favicon');//Set prefered icon in browser URL bar. Unused?
var logger = require('morgan');//HTTP request logger. Unused?
var cookieParser = require('cookie-parser');//Stores cookies in req.cookies
var bodyParser = require('body-parser');//Middleware parser for incoming request bodies,
/* REST API routes */
var routes = require('./routes/index');//Points to /routes/index.js. Currently, index.js points to CosmosD3/CosmosD3.html
/* Connect to the databases */
//var mongo = require('mongodb');
//var monk = require('monk');
//var db = monk('localhost:27017/nodetest');
//var datasets = monk('localhost:27017/datasets');
/* The HTTP request handler */
var app = express();//Creates app from express class. (Baseline famework for an app. No web functionality).
var debug = require('debug')('Nebula:server');//Require the debug module. Pass it scoping 'Nebula:server'
var http = require('http').Server(app);//Create an http server on top of app.
/* The Socket.io WebSocket module */
var io = require('socket.io')(http);//Create an io/websocket on top of http object.
/* Our custom Nebula module handles the WebSocket synchronization */
var nebula = require('./nebula')(io);//Creates nebula layer on top of io.
/* Set the port we want to run on */
var port = process.env.PORT || 8080;
//app.set('port', port);
var host = '0.0.0.0';
app.listen(port, host);
console.log(`Running on http://${host}:${port}`);
/* view engine setup, currently not used */
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
/* Expose everything in public/ through our web server */
app.use(express.static(path.join(__dirname, 'public')));
app.use("/cosmos", express.static(path.join(__dirname, 'CosmosD3')));
// Make our db accessible to our router
//app.use(function(req, res, next){
// req.db = db;
// req.datasets = datasets;
// next();
//});
/* Initiate the REST API */
app.use('/', routes);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
/**
* Event listener for HTTP server "error" event.
*/
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
var bind = (typeof port === 'string' ? 'Pipe ' + port : 'Port ' + port);
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
/**
* Event listener for HTTP server "listening" event.
*/
function onListening() {
var addr = http.address();
var bind = (typeof addr === 'string' ? 'pipe ' + addr : 'port ' + addr.port);
debug('Listening on ' + bind);
}
/**
* Listen on provided port, on all network interfaces.
*/
http.listen(port);
http.on('error', onError);
http.on('listening', onListening);
我以前遇到过这个问题(正如@davidmaze 在 ) I had to change the host that my app was listening on. In my case, it was that I was using the Express framework, which (despite the docs implying otherwise) was listening only for localhost (127.0.0.1
), and I needed it to listen to 0.0.0.0
. See lines 58-66 (which make up the 2nd code block in the Create the Node.js app section of the Dockerizing a Node.js web app 示例中提到的)他们明确告诉 express 听 0.0.0.0
.
我终于明白了!!经过大量挖掘后,我发现 Docker 应用程序的默认 IP 是 http://192.168.99.100 as opposed to http://localhost。我从这里得到了答案:
https://forums.docker.com/t/docker-running-host-but-not-accessible/44082/13
在 MAC 上,我不得不将端口放在两个位置:
在我需要放入的 Dockerfile 中:
暴露 5000 5010 5011
在 CMD 行之前(倒数第二行)并且它公开了 3 个端口
在docker-compose.yml(我有两个dockers)我需要在应用程序部分输入:
端口:
- "5000:5000"
- "5010:5010"
- "5011:5011"
我正在尝试 "Dockerize" 一个相当大、复杂的 Node.js 应用程序,其结构大致如下:
Nebula [项目根目录; 运行是 Node.js 应用程序]
|__CosmosD3 [包含所有 HTML 和 JS 文件的目录]
|__Nebula-Pipeline [包含执行所有繁重计算的大多数 Python 文件的目录]
我们这个项目的标准安装过程相当复杂,因为所有相互关联的部分都必须完美地协同工作,以使我们的前端 JS 文件能够与我们的后端 Python 脚本进行通信。对于 Linux/Unix 系统,这是粗略的过程(使用 Nebula 目录中的命令 运行):
- 安装Java。 (几乎任何版本都应该这样做;我们有一个 Java 文件需要 运行 用于前端的特定交互)
- 安装 Python 2.7 和 pip
- 运行
pip install --upgrade pip
确保使用最新的 pip - 使用
sudo apt-get install libzmq-dev npm nodejs-legacy
安装 Node.js、npm 和 zmq
- 运行
npm install
安装我们的 Node.js 包依赖项 - 运行
pip install -e ./Nebula-Pipeline
安装所有 Python 依赖项 - 祈祷一切顺利,运行 申请
npm start
。默认端口为8081
,因此应可通过localhost:8081
. 访问应用程序
已经有一个与此非常相似的项目 "Dockerized." 通过调整另一个项目的 Docker 文件以更接近上述步骤,这就是 Docker我创建的文件:
FROM ubuntu:16.04
RUN mkdir /www
WORKDIR /www
RUN apt-get update
RUN apt-get install -y libzmq-dev npm nodejs-legacy python python-pip
RUN apt-get update && \
apt-get install -y openjdk-8-jdk && \
apt-get install -y ant && \
apt-get clean;
# Fix certificate issues, found as of
# https://bugs.launchpad.net/ubuntu/+source/ca-certificates-java/+bug/983302
RUN apt-get update && \
apt-get install ca-certificates-java && \
apt-get clean && \
update-ca-certificates -f;
# Setup JAVA_HOME, this is useful for docker commandline
ENV JAVA_HOME /usr/lib/jvm/java-8-openjdk-amd64/
RUN export JAVA_HOME
##COPY ./lib/ /opt/lib
RUN pip install --upgrade pip
COPY ./Nebula /www
RUN ls -a /www
RUN npm install
RUN pip install -e /www/Nebula-Pipeline
EXPOSE 8081
CMD ["npm", "start"]
当我 运行 这个 Docker 带有 docker build -t nebulaserver
的文件时,图像似乎创建成功。当我 运行 带有 docker run -p 8081:8081 nebulaserver
的图像时,我得到以下打印输出,似乎表明一切实际上都在正常工作。 (请注意,"PORT: 8081" 打印输出是对 Node.js 应用程序正在使用的端口的确认。)
Michelles-MacBook-Pro-8$ docker run -p 8081:8081 nebulaserver
> Nebula@0.0.1 start /www
> node app.js
PORT:
8081
但是,当我随后尝试连接到 localhost:8081
时,我没有收到任何响应。此外,当我的 Node.js 服务器收到对 HTML 页面之一的请求时,我希望看到它的其他打印输出。我也没有看到任何这些打印输出。就好像端口转发没有正常工作一样。根据我读过的所有内容,我应该正确地做事,但我以前从未使用过 Docker,所以也许我遗漏了什么?
有人知道我做错了什么吗?
编辑: 这是我的 app.js 文件,以防它有助于弄清楚发生了什么...
/*Import packages required in package.json */
/*Add these packages from the ../node_modules path*/
var express = require('express');//A lightweight nodejs web framework
var path = require('path');//Ability to join filepaths to filenames.
var favicon = require('serve-favicon');//Set prefered icon in browser URL bar. Unused?
var logger = require('morgan');//HTTP request logger. Unused?
var cookieParser = require('cookie-parser');//Stores cookies in req.cookies
var bodyParser = require('body-parser');//Middleware parser for incoming request bodies,
/* REST API routes */
var routes = require('./routes/index');//Points to /routes/index.js. Currently, index.js points to CosmosD3/CosmosD3.html
/* Connect to the databases */
//var mongo = require('mongodb');
//var monk = require('monk');
//var db = monk('localhost:27017/nodetest');
//var datasets = monk('localhost:27017/datasets');
/* The HTTP request handler */
var app = express();//Creates app from express class. (Baseline famework for an app. No web functionality).
var debug = require('debug')('Nebula:server');//Require the debug module. Pass it scoping 'Nebula:server'
var http = require('http').Server(app);//Create an http server on top of app.
/* The Socket.io WebSocket module */
var io = require('socket.io')(http);//Create an io/websocket on top of http object.
/* Our custom Nebula module handles the WebSocket synchronization */
var nebula = require('./nebula')(io);//Creates nebula layer on top of io.
/* Set the port we want to run on */
var port = process.env.PORT || 8080;
//app.set('port', port);
var host = '0.0.0.0';
app.listen(port, host);
console.log(`Running on http://${host}:${port}`);
/* view engine setup, currently not used */
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
/* Expose everything in public/ through our web server */
app.use(express.static(path.join(__dirname, 'public')));
app.use("/cosmos", express.static(path.join(__dirname, 'CosmosD3')));
// Make our db accessible to our router
//app.use(function(req, res, next){
// req.db = db;
// req.datasets = datasets;
// next();
//});
/* Initiate the REST API */
app.use('/', routes);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
/**
* Event listener for HTTP server "error" event.
*/
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
var bind = (typeof port === 'string' ? 'Pipe ' + port : 'Port ' + port);
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
/**
* Event listener for HTTP server "listening" event.
*/
function onListening() {
var addr = http.address();
var bind = (typeof addr === 'string' ? 'pipe ' + addr : 'port ' + addr.port);
debug('Listening on ' + bind);
}
/**
* Listen on provided port, on all network interfaces.
*/
http.listen(port);
http.on('error', onError);
http.on('listening', onListening);
我以前遇到过这个问题(正如@davidmaze 在 127.0.0.1
), and I needed it to listen to 0.0.0.0
. See lines 58-66 (which make up the 2nd code block in the Create the Node.js app section of the Dockerizing a Node.js web app 示例中提到的)他们明确告诉 express 听 0.0.0.0
.
我终于明白了!!经过大量挖掘后,我发现 Docker 应用程序的默认 IP 是 http://192.168.99.100 as opposed to http://localhost。我从这里得到了答案:
https://forums.docker.com/t/docker-running-host-but-not-accessible/44082/13
在 MAC 上,我不得不将端口放在两个位置:
在我需要放入的 Dockerfile 中: 暴露 5000 5010 5011
在 CMD 行之前(倒数第二行)并且它公开了 3 个端口在docker-compose.yml(我有两个dockers)我需要在应用程序部分输入: 端口:
- "5000:5000"
- "5010:5010"
- "5011:5011"