1.新建目錄src,並進入src目錄node
[xiejdm@localhost Documents]$ mkdir src [xiejdm@localhost Documents]$ cd src/
2.建立package.json和index.js文件,文件內容以下:nginx
package.jsonweb
[xiejdm@localhost Documents]$ mkdir src [xiejdm@localhost Documents]$ cd src/ [xiejdm@localhost src]$ cat package.json { "name": "docker-centos-hello", "private": true, "version": "0.0.1", "description": "Node.js Hello world app on CentOS using docker", "author": "Gideon xie <xiejdml@gmail.com>", "dependencies": { "express": "3.2.4" } }
index.jsdocker
[xiejdm@localhost src]$ cat index.js var express = require('express'); // Constants var PORT = 8080; // App var app = express(); app.get('/', function (req, res) { res.send('Hello world\n'); }); app.listen(PORT);
3.建立Dockfileexpress
[xiejdm@localhost src]$ cat Dockerfile FROM centos:centos7 # Enable EPEL for Node.js RUN yum install -y epel-release # Install Node.js and npm RUN yum install -y npm # Bundle app source COPY . /src # Install app dependencies RUN cd /src; npm install EXPOSE 8080 CMD ["node", "/src/index.js"] [xiejdm@localhost src]$
4.使用docker build構建生成鏡像npm
[xiejdm@localhost src]$ docker build -t gideon/centos-node-hello . Sending build context to Docker daemon 4.096 kB Sending build context to Docker daemon Step 0 : FROM centos:centos7 ---> 7322fbe74aa5 Step 1 : RUN yum install -y epel-release ---> Running in 858c0e3e9a22 Loaded plugins: fastestmirror Determining fastest mirrors * base: mirrors.yun-idc.com * extras: mirrors.yun-idc.com * updates: mirrors.nwsuaf.edu.cn Resolving Dependencies --> Running transaction check ---> Package epel-release.noarch 0:7-5 will be installed --> Finished Dependency Resolution Dependencies Resolved ================================================================================ Package Arch Version Repository Size ================================================================================ Installing: epel-release noarch 7-5 extras 14 k Transaction Summary ================================================================================ Install 1 Package Total download size: 14 k ······ ······ ······ npm http 200 https://registry.npmjs.org/qs/-/qs-0.6.4.tgz npm WARN engine formidable@1.0.13: wanted: {"node":"<0.9.0"} (current: {"node":"v0.10.36","npm":"1.3.6"}) express@3.2.4 node_modules/express ├── methods@0.0.1 ├── fresh@0.1.0 ├── range-parser@0.0.4 ├── cookie-signature@1.0.1 ├── buffer-crc32@0.2.1 ├── cookie@0.0.5 ├── commander@0.6.1 ├── mkdirp@0.3.4 ├── send@0.1.0 (mime@1.2.6) ├── debug@2.2.0 (ms@0.7.1) └── connect@2.7.9 (pause@0.0.1, qs@0.6.4, bytes@0.2.0, formidable@1.0.13) ---> 66738118fe5d Removing intermediate container cad428ec3167 Step 5 : EXPOSE 8080 ---> Running in 9ee7937e5835 ---> f6ccd16494ac Removing intermediate container 9ee7937e5835 Step 6 : CMD node /src/index.js ---> Running in 9ca19e7392e9 ---> ae5f980eea52 Removing intermediate container 9ca19e7392e9 Successfully built ae5f980eea52 [xiejdm@localhost src]$
5.查看個人鏡像json
[xiejdm@localhost src]$ docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE gideon/centos-node-hello latest ae5f980eea52 About a minute ago 408 MB nodejs_hello/node latest 2daac5743daa 52 minutes ago 544.7 MB gideon/nodejs latest f30730d7c260 3 days ago 544.7 MB nginx latest 6886fb5a9b8d 6 days ago 132.8 MB ubuntu latest d2a0ecffe6fa 2 weeks ago 188.3 MB centos centos7 7322fbe74aa5 5 weeks ago 172.2 MB centos latest 7322fbe74aa5 5 weeks ago 172.2 MB hello-world latest 91c95931e552 3 months ago 910 B [xiejdm@localhost src]$
6.運行剛剛生成的鏡像,並將容器的8080端口映射到主機的49161端口ubuntu
[xiejdm@localhost src]$ docker run -p 49161:8080 -d gideon/centos-node-hello 71baf591eb27a2373daf5a802ee9406e635878ee1b1fcd047eb3f39e476b4406 [xiejdm@localhost src]$
7.在本機輸入localhost:49161,訪問helloword應用。centos
備註: CentOS 7中增長EPEL庫,直接使用yum install epel-release便可(CentOS 5/6則須要下載對應的rpm包而後進行安裝)。cookie