我试图在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"
}
Dockerfile
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 .
导致Successfully built 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
我不知道发生了什么,以及服务器退出的原因。任何反馈都表示赞赏。感谢。
答案 0 :(得分:3)
您可以使用docker run --rm -it test_simple bash
将shell添加到容器中,然后尝试手动运行nodemon /src/index.js
并找出可能出现的问题,并直接运行node
而不是调试此问题时nodemon
。
此外,最好以the official node image
为基础