Introduction of file events in windows using WSL2 and Bind Mounts
Posted By : Lokesh Singh | 02-Feb-2021
1. Introduction.
When working on Docker with windows we have to manage the file system for docker because Docker natively does not support the Windows File system and we have to additionally use additional tools to run docker on windows and with those tools, there are some functionalities like "Bind Mounts" that need extra configurations to work on docker. Here is a brief introduction to why we want to use docker and how we can run docker on windows and share the windows file system with docker Container. and solve the issues related to "Bind Mounts".
Here is a brief technical terminology we used in this blog.
1. Docker - Docker is a container technology: A tool for creating and managing container
2. Container - A standardized unit of software.
3. Volumes - Volumes are folders on your host machine's hard drive that are mounted ("made available", mapped) into containers.
4. Bind Mount - We set up a folder/path on our host machine for persistence.
Also Read: Eager, Lazy and preloading
Why would we want independent, standardized "application packages"?
1. Different Development & Production Environments - We want to build and test in exactly (!) the same environment as we later run on our app in
2. Different Development Environment within a Team / Company - Every team member should have the exactly (!) same environment when working on the same project
3. Clashing Tools / Versions Between Different Projects - When switching between projects, tools used in project A should not clash with tools used in project B.
The Problem with the native approach
Environment: the runtimes, languages, frameworks you need for development often not the same with Production Environment, Development Environment often not the same within a team or different Tools & Libraries required for a different project
We want reliable & Reproducible Environments.
1. We Want to have the exact same environment for development and production - this ensures that it worked exactly as tested
2. It should be easy to share a common development environment/setup with (new) employees and colleagues.
3. We don't want to uninstall and re-install local dependencies and runtimes all the time
The solution for that can be either we can use Virtual Machines / Virtual Operating Systems or You can run Docker containers
Also Read: How to upgrade Jenkins and Reset Admin Password
Containers Vs. Virtual Machines.
Docker Container |
Virtual Machines |
Low Impact on OS, Very fast minimal disk space Usage | Bigger Impact on OS, slower, higher disk space usage |
Sharing, re-building, and distribution is easy | Sharing, re-building, and distribution can be challenging |
Encapsulated apps/environments instead of "Whole machines" | Encapsulating "whole machines" instead of just apps/environments |
2. Docker and WSL Setup on Windows.
To Use Docker on windows you need Microsoft Windows 10 Professional or Enterprise 64-bit or Windows 10 Home 64-bit with WSL 2. Docker recently added support for windows for certain windows version and using WSL2.
WSL 2 gives the capability to access Linux file systems in the Windows Subsystem for Linux. This enables you to access file systems that aren't natively supported but now you can access your Linux files with windows!
You can follow these steps on the official Microsoft Documentation to install WSL.
3. Implementation
When Working on Windows and WSL2, you can encounter issues when it comes to working with "Bind Mounts".
Specifically, file change events might not be propagated to the container -i.e. processes running inside of the container (like "nodemon") are not made aware of file changes on our host machine.
Also Read: Introduction to PagerDuty
There are three main workarounds and solutions.
1. Quick & Dirty
This Solution is quick and not the preferred way but it does the job.
For example in the application that is using Nodemon (A utility that will monitor for any changes in your code and automatically restart the server), you can switch from the "notify me" mode to the "I look for changes myself mode" by changing the starting script in the package.json file like this:
{ "scripts" : 'nodemon -L server.js" }
For the React application (where we also want to listen to file changes), you can pull a similar trick. Simply add an environment variable to your docker run command:
docker run -e CHOKIDAR_USEPOLLING=true ..other-options <react-image-id>
2. Mount your Windows Paths into WSL2
This solution might require an update to the latest Windows Version because windows recently added support for WSL2.
Once you got that, You can follow these steps to access the Linux filesystem in windows and WSL2.
1. To mount a disk, open a Powershell window with administrator privileges and run:
wsl --mount <DiskPath>
2. To list the available disk in windows, run:
wmic diskdrive list brief
As a result, file changes will be forwarded into the container.
3. Embrace WSL2
This is theoretically the best solution, but it's also an advanced solution.
"Embrace WSL2" means, that you should use WSL2 as your default terminal/command prompt (instead of the regular Windows command prompt).
Do all your work from inside WSL2 (and therefore from inside Linux) and you'll not face any issues.
What does that mean?
Launch your Linux distribution (and therefore WSL2) via the windows starting menu.
This opens up a Linux shell that allows you to work with the Linux distribution which was installed on your Windows system. You also got full access to its internal file system there.
Navigate around via the ls(list content of the current directory) and cd(change directory) commands.
You can create a new directory via the mkdir command
For example, you can create a docker-practice directory like this:
cd~
mkdir docker-practice
cd docker-practice
4. Conclusion
Now when comes to working with these folders (e.g. to create your Dockerfile and your project files in there) in development you can easily access the Windows file system using docker.
Also Read: GIT IGNORE
Why You Should Choose Oodles For SaaS Product Development?
We are a 360-degree software development company that provides cross-platform SaaS app development services to address varied software project requirements. We have an experienced team of Java, PHP, and Python developers who use advanced frameworks, tools, and SDKs to build scalable web and mobile applications with custom features. For more detail, reach us out at [email protected].
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Lokesh Singh
He is Hard Working and Punctual and keen to learn new technologies. He works as both Full Stack Developer and MEAN Stack Developer on both frontend and backend technologies.