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