nodejs_web_app.rst 5.7 KB

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