目的
实现简单的docker的nodejs容器,使用Dockerfile构建我们的使用nodejs开发的系统
技术栈
- Docker
- Nodejs
- Express
- Linux
step1 下拉nodejs基础容器 node:
本次我需要使用基础alpine操作系统构建的node基础容器

image.png
step2 创建项目文件夹
创建一个空目录,并且创建如下文件
root@ubuntu-docker:/server/nodejs# ls
Dockerfile package.json server.js

image.png
Dockerfile内容
FROM node:6.10.2-alpine
ADD . /server/www/
WORKDIR /server/www/
RUN cd /server/www && npm install
EXPOSE 3001
CMD ["node","server.js"]
package.json内容
{
"name": "docker-node-hello",
"private": true,
"version": "0.0.1", "description": "Node.js with docker", "author": "zhaojunlike@gmail.com", "dependencies": { "express": "3.2.4" } }
server.js app的入口文件
var express = require('express');
// Constants var PORT = 3001; // App var app = express(); app.get('/', function (req, res) { res.send('Hello World\n'); }); app.listen(PORT) console.log('Running on http://localhost:' + PORT);
step3 build
使用以下命令来构建node-app容器
#docker build -t node-hello ./

image.png
step4 运行测试
root@ubuntu-docker:/server/nodejs# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
node-hello latest 58a5182bd055 40 seconds ago 56.34 MB root@ubuntu-docker:/server/nodejs# docker run -d -p 3001:3001 node-hello 0af03634720b3572e860b9d353fd140b88f729e938713f3c4fc694cfda3ec065 root@ubuntu-docker:/server/nodejs# curl localhost:3001 Hello World root@ubuntu-docker:/server/nodejs#
作者:Godtoy
链接:https://www.jianshu.com/p/022aadc70c80
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。