目录

完成第一个docker应用程序

本文参考《Kubernetes in action》中的案例,部署一个node.js应用。在vscode中搭建node.js开发环境请自行百度。

创建应用程序

var http=require('http');

http.createServer(function(req,res){ 
  res.writeHead(200,{'Content-Type':'text/plain'});
  res.end('hello node.js');
  }).listen(3000,'0.0.0.0',function(){
      console.log('Server running at http://localhost:3000');
});

使用 node app.js启动应用程序,使用curl localhost:3000即可验证应用部署是否成功。

编写Dockerfile

FROM node:12
ADD app.js /app.js 
ENTRYPOINT [ "node", "app.js" ]

注意:

  1. dockerfile 和app.js 在同一目录下
  2. node版本号最好根据当期开发环境中版本保存一致,具体版本号使用node -v查询

制作镜像

在dockerfile目录下执行命令:sudo docker build -t kubia .

运行程序

sudo docker run --name kubia-container -p 3000:3000 -d kubia

验证应用

在浏览器中访问http://docker-ip:3000,浏览器显示hello node.js。docker ip可以通过ifconfig/ipconfig 命令查询