How to Dockerize Node js app

In this tutorial, I will show you how to Dockerize Node js app.

Node.js is a JavaScript runtime built on Chrome’s V8 JavaScript engine.

Docker is a great way to ship and deploy your app easily. I have already created a sample app and hosted it on GitHub. You can feel free to clone the app and use it.

Read also:  How to Dockerize go (golang) web app

Clone a sample app

For this tutorial, I have created a sample app and hosted it on Github. Feel free to clone and fork it.

Dockerize the app

Now we start Dockerizing the app.  First of all, add Dockerfile in your app directory.

vim Dockerfile

FROM node:8.9.4
COPY package.json /app
RUN npm install
COPY . /app
CMD npm start

Build your app

After creating Dockerfile its time to build your app. You can use docker build command to build the docker image. Tag the docker image with -t option.

➜  sample-nodejs-app git:(master) docker build -t nodejs-sample .
Sending build context to Docker daemon  3.052MB
Step 1/7 : FROM node:8.9.4
 ---> 672002a50a0b
Step 2/7 : WORKDIR /app
 ---> Using cache
 ---> 0acfb15c929c
Step 3/7 : COPY package.json /app
 ---> Using cache
 ---> 63ade74afd25
Step 4/7 : RUN npm install
 ---> Using cache
 ---> 6e6bbac31ef6
Step 5/7 : COPY . /app
 ---> 9321155e0ae1
Step 6/7 : CMD npm start
 ---> Running in aa0acda06b9f
Removing intermediate container aa0acda06b9f
 ---> 2fc524010f1a
Step 7/7 : EXPOSE 3000
 ---> Running in 2fd16ed4f9c5
Removing intermediate container 2fd16ed4f9c5
 ---> a77b0ea953b2
Successfully built a77b0ea953b2
Successfully tagged nodejs-sample:latest

Test your Dockerized App

Now its time to test your docker image. Just run the docker image using docker run option. docker run -it -p 3000:3000 nodejs-sample:latest

Then Open your browser and open http://localhost:3000

 You might also like:  How to Dockerize a Bash Script (Shell Script)

Please let me know if you like this tutorial.

If you have any issues please feel free to comment below.

Leave a Reply

Your email address will not be published.


The reCAPTCHA verification period has expired. Please reload the page.

48-Hour Flash Sale! Courses from just $10.99