github.com/akashshinde/docker@v1.9.1/docs/examples/nodejs_web_app.md (about)

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