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
Now here is the docker file for creating nodejs application docker container.
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.
where nodejsapp is the image name
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.
Now you can access the nodejs application using your web browser. here is the screenshot for it.
© 2018, Techrunnr. All rights reserved.