我正在尝试在Docker容器中运行一个小的服务器应用程序
index.js
var server_port = 3111,http = require('http'),mosca = require('mosca'),dispatch = require('dispatch'),httpServ = http.createServer(
dispatch({
'/': function(req,res,next){
console.log('in route');
}
})
);
httpServ.listen(server_port,function() {
console.log("listening on",server_port);
});
package.json
{
"dependencies": {
"dispatch": "^1.0.0"
},"description": "","engines": {
"node": ">= 0.10.4"
},"main": "index.js","name": "SimpleServer","version": "0.0.1"
}
Docker文件
FROM centos:centos6
# Enable EPEL for Node.js
RUN rpm -Uvh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
# Install Node.js and npm
RUN yum install -y npm
# Install nodemon
RUN npm install -g nodemon
# Bundle app source
COPY . /src
# Install app dependencies
RUN cd /src; npm install
# Expose port
EXPOSE 3111
# Run app using nodemon
RUN echo "***" && pwd && echo "***"
CMD ["nodemon","/src/index.js"]
在Docker终端的SimpleServer工作目录中,命令$docker build -t test_simple.导致成功构建了43c8806574f4.到目前为止,任何在容器中运行映像的尝试(例如$docker run -p 3111 test_simple)都会始终导致代码退出,并且服务器未运行.
$docker run -p 3111 test_simple
19 Aug 07:22:56 - [nodemon] v1.4.1
19 Aug 07:22:56 - [nodemon] to restart at any time,enter `rs`
19 Aug 07:22:56 - [nodemon] watching: *.*
19 Aug 07:22:56 - [nodemon] starting `node /src/index.js`
Master: 16
19 Aug 07:22:57 - [nodemon] clean exit - waiting for changes before restart
$docker run -d -P test_simple node /src/index.js
a0a56ec5c13161bc37b523be0e32edec0a43ca82e7db88ddb2a91688f745b24b
$docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a0a56ec5c131 test_simple "node /src/index.js" 6 seconds ago Exited (0) 6 seconds ago jolly_bohr
我无法告知发生了什么,以及服务器为何退出.任何反馈表示赞赏.谢谢.
最佳答案
您可以使用docker run –rm -it test_simple bash将shell放入容器中,然后尝试手动运行nodemon /src/index.js并找出可能出问题的地方,以及直接运行node而不是nodemon同时调试此问题.
原文链接:https://www.f2er.com/docker/532583.html另外,最好以the official node image为基准