Containerizing Node JS Application using Docker installed in Windows Subsystem For Linux 2(WSL2)

 Today we will be looking at how to containerize Node js application in WSL2 enabled in a Windows 10 Operating System. There are some prerequisite setup needs to be done before we proceed to containerize the application.

First and foremost we need to enable WSL2 in windows 10 system. You can refer here to see the steps needed to enable WSL2. Once WSL2 is enabled then we need to install docker in WSL2 so that we can build the image.

Once these things are done then we can start to containerize the application by creating a Dockerfile in the application. This Dockerfile describes the steps needed to be done by the docker deamon to build an image.

How to Create DockerFile in Visual Studio Code

We can easily create a DockerFile in the VS Code by installing the extension for the docker. Once the Docker extension is installed open the command Bar by pressing Ctrl + Shift + P and select the option docker : add docker file.

After selecting the option to add the docker file you will  be prompted to enter the required details such as the type of application you are trying to containerize, The package.json file which needs to be used and also the ports needs to be exposed by the container.

Suggested Read : Docker File Keywords

Sample Docker file is as shown below:

Docker File

   

Once the DockerFile is created we can switch to the WSL2 to build an image out of the DockerFile as we are trying to use the docker installed in WSL2.

Open the WSL2 window and switch to the super user mode by typing in the command

sudo -i

This will allow user to directly enter further commands without adding sudo. Once we are in the super user mode, try to enter the docker ps command to see if the docker daemon is alive. If we are getting the prompt that the docker daemon is not alive then start the docker daemon by entering the command dockerd in another WSL2 window. This will start the docker daemon process.

How to Access the Docker File present in Windows PC from WSL2

We can access the DockerFile stored in system from WSL2 as follows:

  • Navigate to the location where the project is stored in Windows System from WSL2 by using the path /mnt/<Project location> as shown below:
                    
Accessing DockerFile from WSL2

  • Once we reach the location where the dockerFile is stored then enter the  command docker build . as shown in the figure:

Building Docker image


just ignore the error shown this is due to the legacy version of npm.

  • Once the image is successfully created spawn the container from the image with the command docker run -p <Host Port>:<Container Port> <Image Name>. 
  • Once the container is running we can check whether we are able to access the application with the command curl http://localhost:<host port>. If we are getting successful response from within WSL then we can try from the host Windows Machine by using the same URL Format.



Comments

Popular posts from this blog

Creating RESTful Minimal WebAPI in .Net 6 in an Easy Manner! | FastEndpoints

Mastering Concurrency with Latches and Barriers in C++20: A Practical Guide for Students

Graph Visualization using MSAGL with Examples