How to Create a Dockerfile in Node.js ?
Last Updated :
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` directive
let 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 8081
app.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
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
JavaScript Interview Questions and Answers JavaScript (JS) is the most popular lightweight, scripting, and interpreted programming language. JavaScript is well-known as a scripting language for web pages, mobile apps, web servers, and many other platforms. Both front-end and back-end developers need to have a strong command of JavaScript, as
15+ min read
React Tutorial React is a JavaScript Library known for front-end development (or user interface). It is popular due to its component-based architecture, Single Page Applications (SPAs), and Virtual DOM for building web applications that are fast, efficient, and scalable.Applications are built using reusable compon
8 min read
Steady State Response In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read