How to Create a Dockerfile in Node.js ?

01 Dec, 2021
A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. It is something like a shellscript that gathers multiple commands into a single document to fulfill a single task.
Prerequisite: Before you can Dockerize any application, you need to install the Docker Engine based on your operating system. Go to  https://docs.docker.com/engine/install/ to install the Docker Engine. Download it based on your OS.
Commands: Basic commands that we have to know while writing a Dockerfile.
WORKDIR: It is used to specify the working path, that is to say in the image file, we are currently in /app
COPY: It is used to copy the files in the project into the image file.
RUN: Now image files which have Node.js environment, as well as package.json archives, then we can perform npm install the dependencies of all the kit also packed them into the image file
EXPOSE: When using the container, you can imagine that it is a closed machine, so we need to set up a communication channel with the outside. Here we set port 8081, which is our usual port.
CMD: It is used to set the way to start the image. 
PULL: It is used to add files from your Docker repository.
VOLUME:  It is used to create a directory mount point to access and store persistent data.
ARG: It is used to define build-time variable.
DOCKER BUILD: It is used to build command is used to create an image from the Dockerfile.
 DOCKER PUSH: It is used to used to push an image to your Docker Repository.
Let’s create a simple DockerFile by creating a sample express application.
Step 1: Create a sample NodeJs application via the Express framework. 
npm init
Step 2: Let’s add the Express framework as the first dependency by running the following command.
npm i express -s
Step 3: We can create a server_init.js file with simple http server.
server_init.js
// Load express module using `require` directivelet express = require('express')let app = express()  // Define request response in root URL (/)app.get('/', function (req, res) {  res.send('Dockerize the node app')})  // Launch listening server on port 8081app.listen(8081, function () {  console.log('app listening on port 8081')})
Step to run the application: Open the terminal and type the following command.
node server_init.js
Go to http://localhost:8081/ in your browser to view it.
Now let’s create a docker file  for the above Example:
Step 1: Let’s go ahead and create a Dockerfile for our demo application at the root of the project.
touch Dockerfile
Step 2: Open the Dockerfile and add the below steps. Every Dockerfile must start with the FROM instruction. The idea behind this is you need a starting point to build your image. You can start your Docker images from any valid image that you pull from public registries. The image you start from is called the base image. In our case let’s add FROM node:12.16-alpine to the Dockerfile.
FROM node:12.16-alpine
Step 3: Create the directory in the container and We shall use this directory to store files, run NPM, and launch our application:
RUN mkdir node
COPY . ./node
WORKDIR ./node/
Step 4: Install all the NodeJs dependencies by the following command
RUN npm install 
Step 5: In the app example we are running our NodeJs application on port 8081.
EXPOSE 8081
Step 6: Start our NodeJs application by the following command.
CMD node server_init.js
Step 7:Build Docker Image. The command to run our application is
docker build -t hello-world .
Step 8:We can find the docker image we have created.
docker image ls
Run the application by the following command
docker run -p 8081:8081 hello-world
Output: When we run the docker build steps which we have written in the docker file will get executed. Open Browser and open localhost:8081
Push To ContainerRegistry: We can push the image to our public or private Registry. Just three simple steps to push image to Dockerhub
docker login --username username --password yourpassword
docker tag localimagename username/repositoryname:tagname
docker push username/repositoryname:tagname
docker
NodeJS-Questions
Picked
Node.js
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
JWT Authentication with Node.js
Installation of Node.js on Windows
Difference between dependencies, devDependencies and peerDependencies
Mongoose Populate() Method
Mongoose find() Function
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?
Differences between Functional Components and Class Components in React