github.com/feiyang21687/docker@v1.5.0/docs/sources/examples/nodejs_web_app.md (about)

     1  page_title: Dockerizing a Node.js Web App
     2  page_description: Installing and running a Node.js app with Docker
     3  page_keywords: docker, example, package installation, node, centos
     4  
     5  # Dockerizing a Node.js Web App
     6  
     7  > **Note**: 
     8  > - **If you don't like sudo** then see [*Giving non-root
     9  >   access*](/installation/binaries/#giving-non-root-access)
    10  
    11  The goal of this example is to show you how you can build your own
    12  Docker images from a parent image using a `Dockerfile`
    13  . We will do that by making a simple Node.js hello world web
    14  application running on CentOS. You can get the full source code at
    15  [https://github.com/enokd/docker-node-hello/](https://github.com/enokd/docker-node-hello/).
    16  
    17  ## Create Node.js app
    18  
    19  First, create a directory `src` where all the files
    20  would live. Then create a `package.json` file that
    21  describes your app and its dependencies:
    22  
    23      {
    24        "name": "docker-centos-hello",
    25        "private": true,
    26        "version": "0.0.1",
    27        "description": "Node.js Hello world app on CentOS using docker",
    28        "author": "Daniel Gasienica <daniel@gasienica.ch>",
    29        "dependencies": {
    30          "express": "3.2.4"
    31        }
    32      }
    33  
    34  Then, create an `index.js` file that defines a web
    35  app using the [Express.js](http://expressjs.com/) framework:
    36  
    37      var express = require('express');
    38  
    39      // Constants
    40      var PORT = 8080;
    41  
    42      // App
    43      var app = express();
    44      app.get('/', function (req, res) {
    45        res.send('Hello world\n');
    46      });
    47  
    48      app.listen(PORT);
    49      console.log('Running on http://localhost:' + PORT);
    50  
    51  In the next steps, we'll look at how you can run this app inside a
    52  CentOS container using Docker. First, you'll need to build a Docker
    53  image of your app.
    54  
    55  ## Creating a Dockerfile
    56  
    57  Create an empty file called `Dockerfile`:
    58  
    59      touch Dockerfile
    60  
    61  Open the `Dockerfile` in your favorite text editor
    62  
    63  Define the parent image you want to use to build your own image on
    64  top of. Here, we'll use
    65  [CentOS](https://registry.hub.docker.com/_/centos/) (tag: `centos6`)
    66  available on the [Docker Hub](https://hub.docker.com/):
    67  
    68      FROM    centos:centos6
    69  
    70  Since we're building a Node.js app, you'll have to install Node.js as
    71  well as npm on your CentOS image. Node.js is required to run your app
    72  and npm to install your app's dependencies defined in
    73  `package.json`. To install the right package for
    74  CentOS, we'll use the instructions from the [Node.js wiki](
    75  https://github.com/joyent/node/wiki/Installing-Node.js-
    76  via-package-manager#rhelcentosscientific-linux-6):
    77  
    78      # Enable EPEL for Node.js
    79      RUN     rpm -Uvh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
    80      # Install Node.js and npm
    81      RUN     yum install -y npm
    82  
    83  To bundle your app's source code inside the Docker image, use the `COPY`
    84  instruction:
    85  
    86      # Bundle app source
    87      COPY . /src
    88  
    89  Install your app dependencies using the `npm` binary:
    90  
    91      # Install app dependencies
    92      RUN cd /src; npm install
    93  
    94  Your app binds to port `8080` so you'll use the` EXPOSE` instruction to have
    95  it mapped by the `docker` daemon:
    96  
    97      EXPOSE  8080
    98  
    99  Last but not least, define the command to run your app using `CMD` which
   100  defines your runtime, i.e. `node`, and the path to our app, i.e. `src/index.js`
   101  (see the step where we added the source to the container):
   102  
   103      CMD ["node", "/src/index.js"]
   104  
   105  Your `Dockerfile` should now look like this:
   106  
   107      FROM    centos:centos6
   108  
   109      # Enable EPEL for Node.js
   110      RUN     rpm -Uvh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
   111      # Install Node.js and npm
   112      RUN     yum install -y npm
   113  
   114      # Bundle app source
   115      COPY . /src
   116      # Install app dependencies
   117      RUN cd /src; npm install
   118  
   119      EXPOSE  8080
   120      CMD ["node", "/src/index.js"]
   121  
   122  ## Building your image
   123  
   124  Go to the directory that has your `Dockerfile` and run the following command
   125  to build a Docker image. The `-t` flag lets you tag your image so it's easier
   126  to find later using the `docker images` command:
   127  
   128      $ sudo docker build -t <your username>/centos-node-hello .
   129  
   130  Your image will now be listed by Docker:
   131  
   132      $ sudo docker images
   133  
   134      # Example
   135      REPOSITORY                          TAG        ID              CREATED
   136      centos                              centos6    539c0211cd76    8 weeks ago
   137      <your username>/centos-node-hello   latest     d64d3505b0d2    2 hours ago
   138  
   139  ## Run the image
   140  
   141  Running your image with `-d` runs the container in detached mode, leaving the
   142  container running in the background. The `-p` flag redirects a public port to
   143  a private port in the container. Run the image you previously built:
   144  
   145      $ sudo docker run -p 49160:8080 -d <your username>/centos-node-hello
   146  
   147  Print the output of your app:
   148  
   149      # Get container ID
   150      $ sudo docker ps
   151  
   152      # Print app output
   153      $ sudo docker logs <container id>
   154  
   155      # Example
   156      Running on http://localhost:8080
   157  
   158  ## Test
   159  
   160  To test your app, get the the port of your app that Docker mapped:
   161  
   162      $ sudo docker ps
   163  
   164      # Example
   165      ID            IMAGE                                     COMMAND              ...   PORTS
   166      ecce33b30ebf  <your username>/centos-node-hello:latest  node /src/index.js         49160->8080
   167  
   168  In the example above, Docker mapped the `8080` port of the container to `49160`.
   169  
   170  Now you can call your app using `curl` (install if needed via:
   171  `sudo apt-get install curl`):
   172  
   173      $ curl -i localhost:49160
   174  
   175      HTTP/1.1 200 OK
   176      X-Powered-By: Express
   177      Content-Type: text/html; charset=utf-8
   178      Content-Length: 12
   179      Date: Sun, 02 Jun 2013 03:53:22 GMT
   180      Connection: keep-alive
   181  
   182      Hello world
   183  
   184  If you use Boot2docker on OS X, the port is actually mapped to the Docker host VM,
   185  and you should use the following command:
   186  
   187      $ curl $(boot2docker ip):49160
   188  
   189  We hope this tutorial helped you get up and running with Node.js and
   190  CentOS on Docker. You can get the full source code at
   191  [https://github.com/enokd/docker-node-hello/](https://github.com/enokd/docker-node-hello/).