Hi Readers, this document deals with sample dockerfile for nodejs application

Here I’m trying to build the nodejs application in the docker container. For building the custom docker container we need a dockerfile. Dockerfile is a file which has a set of instruction to build to Docker container.


Here is the sample nodejs application code

vi main.js

var http = require("http");

http.createServer(function (request, response) {
// Send the HTTP header 
// HTTP Status: 200 : OK
// Content Type: text/plain
response.writeHead(200, {'Content-Type': 'text/plain'});

// Send the response body as "Hello World"
response.end('Hello World\n');

// Console will print the message
console.log('Server running at');

Now here is the docker file for creating nodejs application docker container.

vi dockerfile

FROM ubuntu:latest
RUN apt-get update && apt-get install -y nodejs && apt-get -y install npm
COPY main.js /tmp/main.js
CMD node /tmp/main.js

FROM refers which operating system to use, here I’m using ubuntu OS out of the that, os with Latest tag.

RUN refers to run the command in the container. Here it will update the operating system repositories and install nodejs and npm.

COPY refers to copy the file from the host machine to Docker container. Here COPY commands will copy the main.js file from my local machine to Docker container.

CMD command is the command which executes after launching the docker container, here once you start the container, it executes node /tmp/main.js

EXPOSE refers the ports need to be exposed, as my nodejs application runs on 8081 ports, so it’s required to expose the port for outside.

Now I’m going to build to build to docker image with above dockerfile. Make sure that you are in the dockerfile root folder.

Now execute the below command to build the docker image.

docker build -t nodejsapp .

where nodejsapp is the image name

docker build -t nodejsapp .
Sending build context to Docker daemon 876.5kB
Step 1/5 : FROM ubuntu:latest
---> 113a43faa138
Step 2/5 : RUN apt-get update && apt-get install -y nodejs && apt-get -y install npm
---> Using cache
---> 1c829cb9248c
Step 3/5 : COPY main.js /tmp/main.js
---> Using cache
---> dbcfdef27723
Step 4/5 : CMD node /tmp/main.js
---> Running in c6db4ddd41d8
Removing intermediate container c6db4ddd41d8
---> 3686ac263c65
Step 5/5 : EXPOSE 8081
---> Running in da013c9921d7
Removing intermediate container da013c9921d7
---> f463b347b814
Successfully built f463b347b814
Successfully tagged nodejsapp:latest

You will be able to see the above output while building the docker image.

Now we will run our docker container with the new image.
Execute the following command to run the container.

docker run -d -p 8081:8081 nodejsapp

Now you can access the nodejs application using your web browser. here is the screenshot for it.


© 2018, Techrunnr. All rights reserved.

Questions Answered
Articles Written
Overall Points
Categories: devopsdocker

Prabhin Prabharkaran

He is Technical professional. He is a person who loves to share tricks and tips on the Internet. He Posts what he does!!

Leave a Reply

Please wait...

Subscribe to our newsletter

Want to be notified when our article is published? Enter your email address and name below to be the first to know.