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