Dockerizing existing nodejs apps in git using Dockerfile

lazy way to move nodejs code in git repo, to become working docker container

Rizky Ramadhan
4 min readJul 12, 2019

So we begin with expectation, that we want our existing nodejs apps to become a containerized apps. As we know, today containerization is a must. It is highly portable, highly scalable, simple & fast in deployment, enhanced developer productivity, and highly secure. Then we get a problem here, how do we start? we dont know yet the technique to containerized our nodejs apps. so, come, let start the drill. :)

Pre-Requisite

application need to be installed

  • git
  • nodejs
  • docker

you also must have account on github to simulate the case

Pre-Condition

right now all we know is just develop a basic standalone express nodejs apps. Our code has been stored in github repository. To make it clear, let simulate it step by step.

  1. create simple express nodejs webapi
$ npm install express --save

refer to expressjs official we can create simple helloworld app simply by typing this code, then save the code as app.js file

const express = require(‘express’)
const app = express()
const port = 3000
app.get(‘/’, (req, res) => res.send(‘Hello World!’))app.listen(port, () => console.log(`Example app listening on port ${port}!`))

so we can have structure like this inside our expressjs apps folder

try run the expressjs apps by type “node app.js” in your console, and you will get the result like below:

try to access the helloworld apps with your browser

helloworld in browser http://localhost:3000

2. push helloworld source code to your own github

  • prepare to exclude node_module from push to git repository, we dont need node_module, we can install it later with “npm install” using list of module in package.json
/node_modules
  • I give my repo name hellome, so with repository ready, I do what github instruct to me to push my existing project
git init
git add .
git commit -m "initial commit"
git remote add origin https://github.com/addonrizky/hellome.git
git push -u origin master

we’re done with github repo, so in github we will see our repository hellome is like picture below:

3. Start containterizing

so basically my lazy idea to containerized our working nodejs project using shell command which consist of these step :

Idea

  • clone repo hellome to any directory
  • copy Dockerfile (to build nodejs image), into the newly cloned folder. the content of Dockerfile as we can see below
FROM node:9-slimWORKDIR /appCOPY . /appRUN npm install expressEXPOSE 3000CMD ["node", "app.js"]
  • go to inside newly cloned folder
  • build container nodejs apps using command “docker build -t rizkyaddon/hellome .”
  • and after container building is done, then we remove folder that we just clone

lets implement the idea..

implementation

  • create new folder named “containterized_nodejs” anywhere you like
  • then, create the Dockerfile. do the “vi Dockerfile” command and fill the content like below
FROM node:9-slimWORKDIR /appCOPY . /appRUN npm install expressEXPOSE 3000CMD ["node", "app.js"]
  • the key success of my lazy method is using shell script. so create file : “ vi containerized-nodejs.sh” and fill the content of the file like below
git clone https://github.com/addonrizky/hellome.gitcp Dockerfile hellome/.cd hellomedocker build -t rizkyaddon/hellome:v1 .cd ..rm -rf hellome
  • so the preparation complete, we can now execute the shell script using command “sh containerized-nodejs.sh” and see what happen in console
I added logging in shell script for traceability purpose
  • images successfully created. If we type command “docker images” then we can see the newly created images
  • run the image to using command:
docker run -p 3000:3000 -it rizkyaddon/hellome:v1
  • voila, the nodejs container successfully running
  • try to access http://localhost:3000 in your browser. See the magic, containerized nodejs apps can be accessed from outside world …..

thats all for today folks. This is my first time in blogging, sorry for lack of detail of the tutorial. hope i can fix it later. :)

--

--