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 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 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 See Docker Image Contents? In the evolving world of containerization, Docker has emerged as a tool for packaging and deploying applications. While creating and sharing Docker images has streamlined the development and deployment process. Contents of a Docker image are important for troubleshooting, optimizing, and ensuring se
3 min read
How To Install Docker On AWS EC2 ? You can use the following instructions to install Docker on an AWS EC2 instance. Note that depending on the Linux distribution your EC2 instance is running, there may be differences in certain commands or processes. What is Docker?Docker is an OS-level virtualization that provides tools for building
3 min read