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
How to Copy a File in Node.js?
Node.js, with its robust file system (fs) module, offers several methods to copy files. Whether you're building a command-line tool, a web server, or a desktop application, understanding how to copy files is essential. This article will explore various ways to copy a file in Node.js, catering to bot
2 min read
How To Comment In Dockerfile?
The purpose of comments in a Dockerfile is to both clarify and improve the readability of the instructions. It can also be used to stop execution when testing other code. The comments are meant to serve as a source of code line information. Comments are a frequent way for programmers to document the
3 min read
How to Create Docker Image?
Docker is a powerful containerization tool that enables developers to package their applications and their dependencies into a single unit called Docker image. The Docker image offers seamless deployment, scalability, and portability. In this article, I will make sure that you understand what is doc
12 min read
How to Mount a File in Docker?
A popular technology called Docker lets you package and execute programs in isolated environments known as containers. These containers ensure consistency across many contexts by incorporating all the components required to run the application, such as the runtime, code, system tools, and libraries.
6 min read
How to Create a Directory using Node.js ?
In this article, we will create a directory using NodeJS. NodeJS has Filesystem(fs) core module, which enables interacting with the file system, has Node.js fs.mkdir() method or Node.js fs.mkdirSync() method method, to create new directory /parent directory. Prerequisites:Node File SystemThe approac
3 min read
How to create a RAM disk using Node.js ?
A RAM disk is a virtual disk device that stores data on a portion of a computer's RAM. It has faster read and write speeds than conventional hard drives or solid-state drives because data can be accessed and written to and from RAM much faster. We'll look at how to make a RAM disk with Node.js, a po
3 min read
How to Install Docker on CentOS?
Quick Preview to Install Docker on CentOS:Installation of Docker on CentOS:Open CentOS Terminal.Execute the command yum update in the Root Menu.Run the command yum install -y yum-utils device-mapper-persistent-data lvm2.Execute the command yum-config-manager --add-repo https://download.docker.com/li
4 min read
How to Access the File System in Node.js ?
In this article, we looked at how to access the file system in NodeJS and how to perform some useful operations on files. Prerequisite: Basic knowledge of ES6Basic knowledge of NodeJS NodeJS is one of the most popular server-side programming frameworks running on the JavaScript V8 engine, which uses
3 min read
How To Dockerize a ReactJS App?
Dockerizing a React app ensures that your application runs consistently across various environments by containerizing it. By creating a Docker container, you can package your React app, along with its dependencies, to ensure that it runs the same way regardless of the environment. In this article, w
4 min read
How to Install Docker on Debian?
Docker Service Product is the essential tool used for the development purpose of any software where the said software needs to be passed through different development phases. The Installed Docker Service makes Operating System-Level Virtualization to create Docker Containers. Docker can easily be in
4 min read