├── .gitignore ├── .dockerignore ├── image1.png ├── image2.png ├── image3.png ├── image4.png ├── image5.png ├── nodeJsDocker.png ├── package.json ├── server.js ├── Dockerfile ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /.dockerignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | npm-debug.log -------------------------------------------------------------------------------- /image1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chandu-muthyala/Dockerizing-a-NodeJS-web-app/master/image1.png -------------------------------------------------------------------------------- /image2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chandu-muthyala/Dockerizing-a-NodeJS-web-app/master/image2.png -------------------------------------------------------------------------------- /image3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chandu-muthyala/Dockerizing-a-NodeJS-web-app/master/image3.png -------------------------------------------------------------------------------- /image4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chandu-muthyala/Dockerizing-a-NodeJS-web-app/master/image4.png -------------------------------------------------------------------------------- /image5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chandu-muthyala/Dockerizing-a-NodeJS-web-app/master/image5.png -------------------------------------------------------------------------------- /nodeJsDocker.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chandu-muthyala/Dockerizing-a-NodeJS-web-app/master/nodeJsDocker.png -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "docker_web_app", 3 | "version": "1.0.0", 4 | "description": "Node.js on Docker", 5 | "author": "cmuth001@odu.edu", 6 | "main": "server.js", 7 | "scripts": { 8 | "start": "node server.js" 9 | }, 10 | "dependencies": { 11 | "express": "^4.16.1" 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /server.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const express = require('express'); 4 | 5 | // Constants 6 | const PORT = 8080; 7 | const HOST = '0.0.0.0'; 8 | 9 | // App 10 | const app = express(); 11 | app.get('/', (req, res) => { 12 | res.send('Hello world\n'); 13 | }); 14 | 15 | app.listen(PORT, HOST); 16 | console.log(`Running on http://${HOST}:${PORT}`); -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM node:10 2 | 3 | # Create app directory 4 | WORKDIR /usr/app 5 | 6 | # Install app dependencies 7 | # A wildcard is used to ensure both package.json AND package-lock.json are copied 8 | # where available (npm@5+) 9 | COPY package*.json ./ 10 | 11 | RUN npm install 12 | # If you are building your code for production 13 | # RUN npm ci --only=production 14 | 15 | # Bundle app source 16 | COPY . . 17 | 18 | EXPOSE 8080 19 | CMD [ "node", "server.js" ] -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Chandrasekhar Reddy Muthyala 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Dockerizing-a-NodeJS-web-app 2 |

3 | Node.js 8 |

9 |
10 | 11 | Create a simple nodeJs application and deploy it onto a docker container. 12 | 13 | 1. Create a working directory 14 | > mkdir 15 | 16 | 2. Running this command in working directory will initialize your project 17 | > npm init 18 | 19 | This will create a **package.json** file in the folder, that file contains app dependency packages. 20 | 21 | Replace the following code of package.json 22 | 23 | ```js 24 | // package.json 25 | 26 | { 27 | "name": "docker_web_app", 28 | "version": "1.0.0", 29 | "description": "Node.js deploy on Docker container", 30 | "author": "cmuth001@odu.edu", 31 | "main": "server.js", 32 | "scripts": { 33 | "start": "node server.js" 34 | }, 35 | "dependencies": { 36 | "express": "^4.16.1" 37 | } 38 | } 39 | ``` 40 | 41 | 2. Running this command will install all the dependencies from package.json 42 | > npm install 43 | 3. Lets create a **server.js** file that defines a web-app using an Express framework. 44 | 45 | 46 | ```js 47 | // server.js 48 | 'use strict'; 49 | var express = require('express'); 50 | var app = express(); 51 | app.get('/', function (req, res) { 52 | res.send('Hello World!'); 53 | }); 54 | app.listen(3000, function () { 55 | console.log('Example app listening on port 3000!'); 56 | }); 57 | 58 | ``` 59 | 60 | 4. Lets test the application, run the below command 61 | > node server.js 62 | 63 | If you followed the above steps on your system, you will see the same output as below image: [http://localhost:3000/](http://localhost:3000/) 64 | 65 |

66 | Node.js 71 |

72 | 73 | Now node.js app is running successfully. 74 | 75 | Lets try running the same node.js application running on the docker container. To run the application on the docker conatiner we need a docker image. 76 | 77 | First, we will create a docker image for the application. 78 | 79 | 5. Create a **Dockerfile** 80 | > touch Dockerfile 81 | 6. Dockerfile should look like this 82 | ``` 83 | FROM node:10 84 | # Create app directory 85 | WORKDIR /usr/app 86 | 87 | # Install app dependencies 88 | # A wildcard is used to ensure both package.json AND package-lock.json are copied 89 | # where available (npm@5+) 90 | COPY package*.json ./ 91 | 92 | RUN npm install 93 | # If you are building your code for production 94 | # RUN npm ci --only=production 95 | 96 | # Bundle app source 97 | COPY . . 98 | EXPOSE 3000 99 | CMD [ "node", "server.js" ] 100 | 101 | ``` 102 | 7. Create **.dockerignore** file with following content 103 | 104 | ``` 105 | node_modules 106 | npm-debug.log 107 | ``` 108 | This will prevent from copying onto docker image. 109 | 110 | 8. Building Docker image 111 | > docker build -t node-web-app . 112 | 113 |

114 | Node.js 119 |

120 | 9. Check the Docker images 121 | > docker images 122 | 123 |

124 | Node.js 129 |

130 | 10. Run the docker image 131 | > docker run -p 49160:3000 -d node-web-app 132 | 133 | 11. Get the container id 134 | > docker ps 135 | 136 |

137 | Node.js 142 |

143 | 144 | 12. Lets know where it is running on 145 | > docker logs 146 | 147 | ``` 148 | output: 149 | Example app listening on port 3000! 150 | ``` 151 | 13. If you followed the above steps on your system, you will see the sam output as below image: [http://localhost:49160/](http://localhost:49160/) 152 | 153 |

154 | Node.js 159 |

160 | 161 | I hope this tutorial helped you get up and running a simple Node.js application on Docker container. 162 | 163 | 164 | --------------------------------------------------------------------------------