DockerFile Node App

Node Sample application

$ mkdir node 
$ cd node
$ apt install npm && npm init -y
$ npm install ronin-server ronin-mocks
$ vi server.jsconst ronin = require('ronin-server')
const mocks = require('ronin-mocks')

const server = ronin.server()

server.use('/', mocks.server(server.Router(), false, true))
  • The mocking server is called Ronin.js and will listen on port 8000 by default.
  • You can send GET requests to this endpoint — http://IP:8000/test and receive an array of JSON objects.
  • You can also make POST requests to the root (/) endpoint and any JSON structure you send to the server will be saved in memory.

Test the application

$ node server.js
$  curl --request POST \
--url http://localhost:8000/test \
--header 'content-type: application/json' \
--data '{"msg": "testing" }'
# syntax=docker/dockerfile:1# Base Image of node 
FROM node:12.18.1
#Set ENV veriable
ENV NODE_ENV=production

#Set working Directory
# Copy the necessy packages
COPY ["package.json", "package-lock.json*", "./"]
#Run the npm commands
RUN npm install --production
# Copy everything in current /app directory
COPY . .
# Strat the node application
CMD [ "node", "server.js" ]
$ docker build -t node-docker:v1.0.0 . 
$ docker run -d -p 80:8000 --name node-app node-docker:v1.0.0
  • -d option is used to run a container in the background and print container ID.
  • -p option is used for port mapping between container and Host machine.
  • — name option is used to assign a name to the container.
  • In the end, we provide the name of the docker image from which we wish to run our container.



DevOps Engineer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store