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