Docker 產生Node.js web app(含連接埠映射)

來源:互聯網
上載者:User

標籤:

1.建立目錄src,並進入src目錄

[[email protected] Documents]$ mkdir src[[email protected] Documents]$ cd src/

2.建立package.json和index.js檔案,檔案內容如下:

package.json

[[email protected] Documents]$ mkdir src[[email protected] Documents]$ cd src/[[email protected] 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 <[email protected]>",  "dependencies": {    "express": "3.2.4"  }}

index.js

[[email protected] src]$ cat index.js var express = require(‘express‘);// Constantsvar PORT = 8080;// Appvar app = express();app.get(‘/‘, function (req, res) {res.send(‘Hello world\n‘);});app.listen(PORT);

3.建立Dockfile

[[email protected] src]$ cat Dockerfile FROM    centos:centos7# Enable EPEL for Node.jsRUN     yum install -y epel-release# Install Node.js and npmRUN     yum install -y npm# Bundle app sourceCOPY . /src# Install app dependenciesRUN cd /src; npm installEXPOSE  8080CMD ["node", "/src/index.js"][[email protected] src]$ 

4.使用docker build構建產生鏡像

[[email protected] src]$ docker build -t gideon/centos-node-hello .Sending build context to Docker daemon 4.096 kBSending build context to Docker daemon Step 0 : FROM centos:centos7 ---> 7322fbe74aa5Step 1 : RUN yum install -y epel-release ---> Running in 858c0e3e9a22Loaded plugins: fastestmirrorDetermining fastest mirrors * base: mirrors.yun-idc.com * extras: mirrors.yun-idc.com * updates: mirrors.nwsuaf.edu.cnResolving Dependencies--> Running transaction check---> Package epel-release.noarch 0:7-5 will be installed--> Finished Dependency ResolutionDependencies Resolved================================================================================ Package                Arch             Version         Repository        Size================================================================================Installing: epel-release           noarch           7-5             extras            14 kTransaction Summary================================================================================Install  1 PackageTotal download size: 14 k··················npm http 200 https://registry.npmjs.org/qs/-/qs-0.6.4.tgznpm WARN engine [email protected]1.0.13: wanted: {"node":"<0.9.0"} (current: {"node":"v0.10.36","npm":"1.3.6"})[email protected]3.2.4 node_modules/express├── [email protected]0.0.1├── [email protected]0.1.0├── range[email protected]0.0.4├── cookie[email protected]1.0.1├── buffer[email protected]0.2.1├── [email protected]0.0.5├── [email protected]0.6.1├── [email protected]0.3.4├── [email protected]0.1.0 ([email protected]1.2.6)├── [email protected]2.2.0 ([email protected]0.7.1)└── [email protected]2.7.9 ([email protected]0.0.1, [email protected]0.6.4, [email protected]0.2.0, [email protected]1.0.13) ---> 66738118fe5dRemoving intermediate container cad428ec3167Step 5 : EXPOSE 8080 ---> Running in 9ee7937e5835 ---> f6ccd16494acRemoving intermediate container 9ee7937e5835Step 6 : CMD node /src/index.js ---> Running in 9ca19e7392e9 ---> ae5f980eea52Removing intermediate container 9ca19e7392e9Successfully built ae5f980eea52[[email protected] src]$ 

5.查看我的鏡像

[[email protected] src]$ docker images REPOSITORY                 TAG                 IMAGE ID            CREATED              VIRTUAL SIZEgideon/centos-node-hello   latest              ae5f980eea52        About a minute ago   408 MBnodejs_hello/node          latest              2daac5743daa        52 minutes ago       544.7 MBgideon/nodejs              latest              f30730d7c260        3 days ago           544.7 MBnginx                      latest              6886fb5a9b8d        6 days ago           132.8 MBubuntu                     latest              d2a0ecffe6fa        2 weeks ago          188.3 MBcentos                     centos7             7322fbe74aa5        5 weeks ago          172.2 MBcentos                     latest              7322fbe74aa5        5 weeks ago          172.2 MBhello-world                latest              91c95931e552        3 months ago         910 B[[email protected] src]$ 

6.運行剛剛產生的鏡像,並將容器的8080連接埠映射到主機的49161連接埠

[[email protected] src]$ docker run -p 49161:8080 -d gideon/centos-node-hello71baf591eb27a2373daf5a802ee9406e635878ee1b1fcd047eb3f39e476b4406[[email protected] src]$ 

7.在本機輸入localhost:49161,訪問helloword應用。

 

備忘: CentOS 7中增加EPEL庫,直接使用yum install epel-release即可(CentOS 5/6則需要下載對應的rpm包然後進行安裝)。

參考:http://docs.docker.com/examples/nodejs_web_app/

     http://www.rackspace.com/knowledge_center/article/install-epel-and-additional-repositories-on-centos-and-red-hat

 

Docker 產生Node.js web app(含連接埠映射)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.