├── .gitignore ├── .dockerignore ├── README.md ├── src └── server.js ├── 2-node-hot-reload.Dockerfile ├── 1-node-simple.Dockerfile ├── 4.node-ui-static.Dockerfile ├── package.json ├── 5.single-stage-production-node.Dockerfile └── 6.multi-stage-production-node.Dockerfile /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | npm-debug.log 3 | -------------------------------------------------------------------------------- /.dockerignore: -------------------------------------------------------------------------------- 1 | .git 2 | node_modules 3 | npm-debug.log -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | A simple Hello World app written in Node.js (Express). 2 | 3 | Contains Dockerfiles for Development (with Hot Reloading) and Production. 4 | 5 | Build and run using any dockerfile: 6 | 7 | ``` 8 | $ docker build -f [dockerfile] -t node-docker . 9 | $ docker run --rm -it -p 8080:8080 node-docker 10 | ``` 11 | -------------------------------------------------------------------------------- /src/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}`); -------------------------------------------------------------------------------- /2-node-hot-reload.Dockerfile: -------------------------------------------------------------------------------- 1 | FROM node:carbon 2 | 3 | # Create app directory 4 | WORKDIR /app 5 | 6 | # Install nodemon for hot reload 7 | RUN npm install -g nodemon 8 | 9 | # Install app dependencies 10 | # A wildcard is used to ensure both package.json AND package-lock.json are copied 11 | # where available (npm@5+) 12 | COPY package*.json ./ 13 | 14 | RUN npm install 15 | 16 | # Bundle app source 17 | COPY src /app 18 | 19 | EXPOSE 8080 20 | CMD [ "nodemon", "server.js" ] -------------------------------------------------------------------------------- /1-node-simple.Dockerfile: -------------------------------------------------------------------------------- 1 | FROM node:carbon 2 | 3 | # Create app directory 4 | WORKDIR /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 install --only=production 14 | 15 | # Bundle app source 16 | COPY src /app 17 | 18 | EXPOSE 8080 19 | CMD [ "node", "server.js" ] -------------------------------------------------------------------------------- /4.node-ui-static.Dockerfile: -------------------------------------------------------------------------------- 1 | FROM node:carbon 2 | 3 | # Create app directory 4 | WORKDIR /app 5 | 6 | # Install app dependencies 7 | RUN npm -g install serve 8 | # A wildcard is used to ensure both package.json AND package-lock.json are copied 9 | COPY package*.json ./ 10 | 11 | RUN npm install 12 | 13 | # Bundle app source 14 | COPY src /app 15 | #Build react/vue/angular bundle static files 16 | RUN npm run build 17 | 18 | EXPOSE 8080 19 | # serve dist folder on port 8080 20 | CMD ["serve", "-s", "dist", "-p", "8080"] 21 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "node-docker", 3 | "version": "1.0.0", 4 | "description": "Dockerfiles for Node.js Web Apps", 5 | "main": "server.js", 6 | "scripts": { 7 | "start": "node server.js" 8 | }, 9 | "dependencies": { 10 | "express": "^4.16.1" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "github.com/praveenweb/node-docker" 15 | }, 16 | "keywords": [ 17 | "dockerfile", 18 | "nodejs" 19 | ], 20 | "author": "praveen@hasura.io", 21 | "license": "ISC" 22 | } 23 | -------------------------------------------------------------------------------- /5.single-stage-production-node.Dockerfile: -------------------------------------------------------------------------------- 1 | FROM node:carbon 2 | 3 | # Create app directory 4 | WORKDIR /app 5 | 6 | # Install app dependencies 7 | # RUN npm -g install serve 8 | 9 | # A wildcard is used to ensure both package.json AND package-lock.json are copied 10 | COPY package*.json ./ 11 | 12 | RUN npm install 13 | 14 | # Bundle app source 15 | COPY src /app 16 | 17 | # Build react/vue/angular bundle static files 18 | # RUN npm run build 19 | 20 | EXPOSE 8080 21 | # If serving static files 22 | #CMD ["serve", "-s", "dist", "-p", "8080"] 23 | CMD [ "node", "server.js" ] 24 | -------------------------------------------------------------------------------- /6.multi-stage-production-node.Dockerfile: -------------------------------------------------------------------------------- 1 | # ---- Base Node ---- 2 | FROM node:carbon AS base 3 | # Create app directory 4 | WORKDIR /app 5 | 6 | # ---- Dependencies ---- 7 | FROM base AS dependencies 8 | # A wildcard is used to ensure both package.json AND package-lock.json are copied 9 | COPY package*.json ./ 10 | # install app dependencies including 'devDependencies' 11 | RUN npm install 12 | 13 | # ---- Copy Files/Build ---- 14 | FROM dependencies AS build 15 | WORKDIR /app 16 | COPY src /app 17 | # Build react/vue/angular bundle static files 18 | # RUN npm run build 19 | 20 | # --- Release with Alpine ---- 21 | FROM node:8.9-alpine AS release 22 | # Create app directory 23 | WORKDIR /app 24 | # optional 25 | # RUN npm -g install serve 26 | COPY --from=dependencies /app/package.json ./ 27 | # Install app dependencies 28 | RUN npm install --only=production 29 | COPY --from=build /app ./ 30 | #CMD ["serve", "-s", "dist", "-p", "8080"] 31 | CMD ["node", "server.js"] 32 | --------------------------------------------------------------------------------