nodejs_web_app.rst 6.0 KB

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