Unlock Efficiency: Mount Local Directories in WSL2-Ubuntu 20.04 with Docker

Unlock Efficiency: Mount Local Directories in WSL2-Ubuntu 20.04 with Docker

Unlock Seamless Development: Leverage Docker on WSL2-Ubuntu 20.04 with Local Directory Mounting for Streamlined Code Harmony.

Bridging the Gap between Local Development and Docker Containers

Living with a frustrating wall between your local development and Docker containers? Imagine seamless code flow, instant container updates, and persistent data beyond containers' transient existence. Buckle up, WSL2-Ubuntu 20.04 users, because mounting local directories unlocks this magical realm!

Imagine seamless code flow, instant container updates, and persistent data beyond containers' transient existence.

Docker and WSL2: A Match Made in Developer Heaven

Break free from dev-container walls! Docker & WSL2, your passport to seamless code flow & instant updates.

Docker: Your Containerization Kingpin

Docker, the containerization kingpin, packages your app and dependencies for consistent runtime anywhere. Smooth deployment, rapid development, and repeatable testing become a reality. Your app flawlessly runs on your laptop, server, or even your friend's potato computer!

WSL2: Bridging Windows and Linux

WSL2 acts as your gateway to running Linux directly on Windows, granting access to the Linux ecosystem, including Docker. It's a bridge between Windows and Linux, allowing you to leverage the best of both worlds!

Dissolving Boundaries: The Magic of Mounting Local Directories

But why stop creating containers? Mounting local directories dissolves the boundary between your machine and containers. Edit code in your IDE, and voila! Changes appear in your running container for instant testing. No more file copying – it's like live editing!

Live edit code in your IDE, boom! Changes in your container. Forget file copying, mount & unlock magic.

The Magic Wand: Mounting Local Directories Unleashes Superpowers

Seamless Editing & Testing

Edit code directly in your IDE, and bam! Changes instantly appear in your running container, ready for testing and debugging. No more clunky file copying – it's like editing live!

Persistent Data Storage

No more data loss when your container vanishes. Mount your data directory, and your files, databases, and configurations remain safe through container restarts and coffee spills.

Simplified File Sharing

Collaborate with teammates on different platforms effortlessly. Mount your codebase, and everyone can access it within their Docker containers, regardless of their operating system. Sharing made it effortless!

Beyond the Basics: Unlocking a Treasure Trove of Possibilities

This is just the tip of the iceberg. Mounted directories unlock a treasure trove of possibilities for workflow customization, environment configuration, and more. Get ready to dive into a world of flexibility and efficiency!

Mounted directories, your key to workflow customization, environment control & beyond. Dive into a world of efficiency!

Ready to Dive In? Setting Up Docker with WSL2 on Windows

Prerequisites

Before diving into Docker and WSL2 integration, ensure your system meets the requirements. These include having Windows 10 (1903+) or Windows 11, a 64-bit processor with Second Level Address Translation (SLAT), a minimum of 4GB RAM, and virtualization enabled in the BIOS.

Installing Docker on Windows

  1. Download Docker: Visit Docker's official website and download the Docker Desktop for Windows.

  2. Install Docker: Double-click the installer, accept the license agreement, choose "WSL 2 Integration" during installation, provide your credentials, and adjust resource allocation if necessary.

  3. Enable WSL2 Integration: Follow the prompts to enable WSL2 integration. This ensures that Docker uses WSL2 as its backend for running Linux containers.

Verifying WSL2 Installation

Open a Command Prompt or PowerShell with administrative privileges and run the command wsl --list --verbose. This command verifies the status of WSL2 and displays a list of installed Linux distributions.

Unlock dev paradise! Prerequisites, install steps, launch Ubuntu. Let's explore Linux dev & containerization!

Verifying and Installing Ubuntu 20.04 on WSL2

  1. Check for Ubuntu 20.04: Run wsl --list --verbose to see if Ubuntu 20.04 is already installed.

  2. Install Ubuntu 20.04: Open the Microsoft Store, search for "Ubuntu 20.04 LTS," click "Get," and wait for the installation to complete.

Launching Ubuntu 20.04 in WSL2

  1. Open the Start Menu and search for "Ubuntu 20.04."

  2. Select the WSL2 Terminal to launch the Ubuntu 20.04 environment.

Congratulations! You've set up Docker with WSL2 integration and launched your Ubuntu 20.04 environment. Now, let's explore the exciting world of Linux development and containerization!

Now, let's dive into the exciting realm of directory mounting within Docker containers. This process is your gateway to seamlessly connect your working directory to a Docker container, offering two distinctive approaches. You can either mount a directory directly from your local Windows machine or tap into a folder nestled within the WSL2 environment. The choice is yours, and the possibilities are boundless!

Local Directories mounting in a Docker Container on WSL2-Ubuntu 20.04

Mount Windows dirs in your container! Find directory, translate path, run container & verify. Easy code flow!". Identify the Directory

Identify the Directory:

  • Locate the directory on Windows. In this case, your folder is inside C/Documents/dsatcl.

Right-click on the folder, select "Properties," and copy its full path.

C:/Documents/dsatcl

Translate the Path:

  • Open a WSL2 terminal and use wslpath to convert the Windows path to a Linux-compatible path.

      /mnt/c/Documents/dsatcl
      wslpath 'C:/Documents/dsatcl'
    

Run the Container with a Mounted Directory:

  • Execute the docker run command to launch a container with the specified volume mount, connecting the local directory to the container path.

      docker run -it -v //wsl$/Ubuntu/home/username/C/Documents/dsatcl:/app_code datasciencetoolbox/dsatcl2e
    

Verify Inside the Container:

Use ls /app_code inside the container to view the mounted directory's contents and confirm that the directory is successfully mounted.

ls /app_code

By following these steps, you'll seamlessly mount the specified Windows directory into the datasciencetool/dsatcl2e Docker container within the Ubuntu environment in WSL2.

Dynamic Directory Mounting in Docker Container on WSL2-Ubuntu 20.04

Flexible mounting! Pull image, run container with dynamic host dir mount. Ensure integration & edit files seamlessly.

  1. Pull Docker Image: Start by pulling the required Docker image from the Docker Hub repository. Use docker pull to download the required Docker image from the Docker Hub repository.

     docker pull datasciencetoolbox/dsatcl2e:latest
    
  2. Run Docker Container with Volume Mount: Launch a Docker container and mount the current host directory into the container's /wd/dsatcl directory using the docker run command. This dynamic approach ensures flexibility across different systems. Launch a Docker container with the -v flag dynamically fetching the absolute path of the current working directory using "$(pwd)".

     docker run --rm -it -v "$(pwd)":/wd/dsatcl datasciencetoolbox/dsatcl2e
    

Ensure to replace datasciencetoolbox/dsatcl2e with the appropriate image if needed. This command dynamically fetches the absolute path of the current working directory, simplifying the integration process.

  1. Verify Integration: Confirm the successful integration by navigating within the Docker container to the mounted directory (/wd/dsatcl). Ensure bidirectional data access for a seamless connection between the host and the container.

By following these steps with dynamic path resolution, you enhance portability and eliminate manual path adjustments when working with the integration on WSL2-Ubuntu 20.04.

Working with the Mounted Directory

  1. Access: Navigate to the mount point (e.g., /app_code) inside the container. When you mount a directory into a container, it essentially means that you are making a specific directory from your host machine accessible inside the container. In this case, the example is given as "/app_code." This subpoint suggests that you can navigate to this directory from within the container to access the files and resources located there.

  2. Edit Files: Changes made reflect on your Windows machine.

    Since you have mounted a directory, any changes you make to files within that directory from within the container will be reflected on your Windows machine as well. This is because the mounted directory creates a shared space between your host (Windows machine) and the container, allowing seamless editing and synchronization of files.

  3. Run Applications: Build and run applications within the container.

    With the mounted directory, you can not only edit files but also execute applications from within the container. This is particularly useful for development scenarios where you may want to build and run code inside a container, leveraging the dependencies and configurations specified within that container.

  4. Use Tools: Access tools and libraries from your local machine within the container.

    The mounted directory allows you to access tools and libraries installed on your Windows machine directly from within the container. This means that if you have specific development tools or libraries installed locally, you can use them in the container environment without having to duplicate or install them separately within the container.

  5. Monitor Changes: Be aware of changes made in either environment due to synchronization.

    Since the directory is mounted, changes made in either the container or on your Windows machine will be synchronized between the two environments. This subpoint emphasizes the importance of being aware of these changes. For example, if you edit a file in the container, you should be mindful that the same changes will be reflected in the corresponding file on your Windows machine, and vice versa.

Working with a mounted directory provides a seamless way to share and synchronize files between your host machine and the container, facilitating collaborative development and ensuring consistency across different environments.

Additional Tips

Permissions

  • Ensure the container user has appropriate permissions for the mounted directory.

  • Explanation: Make sure that the user within the container has the necessary permissions to read from or write to the mounted directory. This ensures proper access to the shared data.

Performance

  • Consider named volumes for frequently used directories for better performance.

  • Explanation: When dealing with frequently accessed directories, using named volumes (persistent and named storage) can enhance performance compared to using regular mounts. Named volumes offer better caching and optimization.

Clean Up

  • Stop the container to disconnect the mount when finished.

  • Explanation: Always stop the container properly when you are done using the mounted directory. This ensures a clean disconnection of the mount and prevents any potential data corruption or loss.

Advanced Topics

After this, you are ready to explore advanced docker concepts.

Dive into the Next Level with Named Volumes:

You can unlock the potential of Docker's Named Volumes—an innovative storage solution that crafts persistent data volumes tied to your customized labels.

Why it Matters: Named volumes revolutionize data handling in Docker by providing a steadfast mechanism for storing and exchanging data between containers. Even when containers take a pause or bid farewell, your data remains intact.

What Sets it Apart: These volumes, autonomously managed by Docker, dance to their tune, indifferent to the ebb and flow of container lifecycles.

Embrace Precision with Bind Mounts:

You may delve into the world of Bind Mounts, offering a refined approach to connecting containers directly with specific paths on the host system.

The Purpose: Granting you meticulous control over data exchange, Bind Mounts empowers real-time synchronization and on-the-fly file manipulations between your container and host.

Why It's Unique: Changes made to bind-mounted files ripple through both the container and the host system instantly, ensuring seamless collaboration.

Unleash Collaboration with Multi-host Development:

You may also embark on a journey into the realm of Multi-host Development—a game-changer that facilitates shared directories across numerous Docker hosts.

Why it's a Game-Changer: Multi-host Development transforms collaborative efforts by allowing developers to work together on projects. Shared access to identical data volumes across diverse Docker hosts opens up a world of possibilities.

The Standout Feature: Flexibility takes center stage as Multi-host Development seamlessly bridges the gap, enabling data sharing and collaboration among developers operating on different machines or environments.

Resources: Fuel Up Your Docker Adventure!

Ready to dive deeper into the exciting world of mounted directories and containerized magic? Here's your treasure map to the ultimate resources:

Docker Desktop with WSL2

Your map and compass for navigating the land of seamless local development and containerized bliss. Find it here: https://docs.docker.com/desktop/wsl/use-wsl/.

Docker Volumes

Discover the secrets of persistent data and conquer the fear of container evanescence. Explore the wonders here: https://docs.docker.com/storage/volumes/.

WSL2 File System Interoperability

Master the language of inter-dimensional mounts and unlock hidden pathways between Windows and Linux. Speak fluently with this guide: https://learn.microsoft.com/en-us/windows/wsl/file-permissions.

With these resources in your arsenal, you're ready to embark on a thrilling adventure like no other – a journey where code flows freely, updates happen instantly, and data persists beyond the ephemeral realm of containers. So, buckle up, fellow adventurer, and prepare to unleash the power of mounted directories!

Bonus Tip: Don't forget to check out the community forums and discussions for insider tips, friendly advice, and tales from other brave explorers!

Are you prepared to unlock the full potential of mounting local directories? Secure your seat, grab your virtual compass, and join us on this thrilling Docker adventure!

Let's embark on this exciting journey together! 🚀