Dockerize Your First Node.js Application: Building a Custom Image with Dockerfile

Dockerize Your First Node.js Application: Building a Custom Image with Dockerfile

In this blog post, we will explore the process of containerizing a simple Node.js application using Docker. Docker allows you to package your application along with its dependencies, ensuring consistency and portability across different environments. By following this step-by-step guide, you will learn how to create a Docker image for your Node.js application, enabling seamless deployment and scaling.

Before staring the project i want to give briefly introduction to what docker is?

In the world of Docker, we have two key concepts: images and containers. An image can be thought of as a blueprint or a snapshot of your application and its dependencies. It includes everything needed to run your application, such as the code, libraries, and even the operating system. Images are created using a Dockerfile, which specifies the instructions for building the image.

Once you have an image, you can create one or more containers from it. A container is an instance of an image that is isolated and runs independently. It encapsulates your application along with its dependencies, ensuring that it runs consistently across different environments. Containers are lightweight and provide an isolated runtime environment, making them highly efficient and portable.

OK now let’s back to work.

Prerequisites: Docker Installation

Before proceeding, please make sure you have Docker installed on your machine. If you don’t have Docker installed yet, please follow the steps below to install it:

Open your preferred web browser and navigate to the Docker website. On the Docker website, you will find the Docker Desktop page.

https://www.docker.com/products/docker-desktop/

This page provides Docker installation packages for various operating systems. Choose the appropriate package for your operating system (e.g, Windows, macOS, Linux) and click on the download link.

Once the Docker installation package is downloaded, run the installer and follow the on-screen instructions to complete the installation process. The installation steps may vary depending on your operating system.

After the installation is complete, you can verify if Docker is successfully installed by opening a terminal or command prompt and typing the following command:

docker version

Once you have completed the Docker installation and verified its functionality, you are ready to proceed with the project.

Setting Up the Node.js Application

I prepared a very simple node JS dummy project and you don’t need to know node to follow along. You can simply download this dummy project.

You find it attached and it contains four files here, a server.js file, which contains our main node application code.

https://github.com/DevCloudLabs/nodejs-app-starting-setup

Make sure you install this Docker extension because this will help you with writing Docker called Docker instructions. It’s not a must have but it will make your life easier.

So now we got this Dockerfile and what do we put into this file now?

The first instruction in the Dockerfile is typically “FROM.” This instruction specifies the base image upon which your custom image will be built. It allows you to layer your image on top of an existing image. In this case, the Dockerfile is using the base image named “node.”

The “WORKDIR” instruction sets the working directory within the image where subsequent instructions will be executed. In this case, the working directory is set to “/app” using the command “WORKDIR /app.”

The Dockerfile includes the “COPY” command to copy files from your local machine to the image. The syntax used is “COPY .” In this case, the command “COPY . /app” is used, which copies all files and subfolders (excluding the Dockerfile itself) from the current directory on your local machine to a folder named “app” inside the image.

After copying the files, the Dockerfile includes the instruction to run “npm install.” This command installs the dependencies required by your Node.js application. It ensures that the necessary libraries and packages are installed within the image.

In order to expose the appropriate port for accessing the application running inside the container, the Dockerfile includes the “EXPOSE” instruction. This instruction informs Docker that the container will listen on a specific port, allowing external access. The port number is specified, for example, “EXPOSE 80.”

The “CMD” instruction specifies the command that will be executed when a container is started from the image. In this case, the command “CMD node server.js” is used, indicating that the Node.js server should be started within the container.

Building the Image

Make sure you are in the same project path in order to run these commands.

docker build -t .

To build an image, you use the “docker build” command followed by the path to the directory containing the Dockerfile. For example:

This command tells Docker to build a new image based on the instructions in the Dockerfile located in the current directory (denoted by the dot).

docker image building

When the image is being built, Docker executes the instructions specified in the Dockerfile. This typically includes pulling a base image (such as a pre-built Node image), setting the working directory inside the image, copying application code into the image, running commands (like npm install) to set up the environment, and exposing any necessary ports for communication.

Running a Container

docker run -p 3000:80 -d --name

Once the image is successfully built, you can run a container based on that image using the “docker run” command. For example:

This command starts a container based on the specified image and executes the command specified in the Dockerfile (e.g., starting a Node server). By default, the container runs in the foreground and displays the output of the executed command.

Run in the web browser

Once you have successfully built and run a Docker container based on the image created, you can open your web browser and enter “localhost:3000” in the address bar. By doing so, you will be able to see the application running in your browser. This application is a simple one where you can write a goal, and it will be displayed.

Stop running container

docker stop

To stop the running container, you need to use the command “docker stop ”. Replace “” with the actual name or ID of the container you want to stop. Executing this command will gracefully stop the container and terminate the process running inside it.

Conclusion

By now, you should have a better understanding of how Docker works and its basic concepts. In the next tutorial, we will dive into dockerizing a full stack web application that connects to MongoDB. This tutorial will guide you through the process of containerizing both the front-end and back-end components of the application, allowing you to easily deploy and scale your application using Docker.

If you have any questions or need further clarification on any aspect of Docker or the upcoming tutorial, please feel free to write a comment. I’m here to assist you and provide support throughout your Docker journey. Good luck.

“I wish you success in your Docker exploration and application development endeavors!”