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
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
- Navigate to the location where the project is stored in Windows System from WSL2 by using the path /mnt/<Project location> as shown below:
- Once we reach the location where the dockerFile is stored then enter the command docker build . as shown in the figure:
- 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
Post a Comment