nodejs_web_app.rst 6.0 KB

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