Docker and WSL2: Building containers on Windows without additional tools
Docker has revolutionized how we develop and deploy applications. It allows developers to create, manage, and run containers with minimal overhead. These containers are lightweight, portable, and reliable. However, for years, developers working on Windows have faced challenges using Docker efficiently due to compatibility issues. This is where Windows Subsystem for Linux 2 (WSL2) comes into play.
WSL2 offers a Linux kernel running on a lightweight virtual machine (VM) inside Windows. It enables seamless integration between Windows and Linux, allowing Windows users to enjoy the benefits of Linux tools. When combined with Docker, WSL2 enables building containers on Windows without needing additional tools. This setup simplifies workflows, reduces overhead, and improves performance.
In this article, we will explore how Docker and WSL2 work together. We will also discuss the benefits of using them on Windows, as well as provide a step-by-step guide for setting them up.
What is Docker?
Docker is an open-source platform that allows you to automate the deployment of applications inside containers. These containers hold everything an application needs, including the code, runtime, libraries, and dependencies. By encapsulating everything in a container, Docker ensures that the application will run in any environment without compatibility issues.
Containers are isolated but share the same underlying system resources, making them lightweight and fast. Unlike traditional virtual machines, containers do not require a full operating system. This allows containers to start up quickly and consume fewer resources.
Docker is widely used in development, testing, and production environments. Its portability and consistency across different environments make it an invaluable tool for developers.
What is WSL2?
WSL2 (Windows Subsystem for Linux 2) is a compatibility layer that allows you to run a full Linux kernel on your Windows machine. Unlike the original WSL, which was more of an emulation layer, WSL2 uses actual Linux kernel features. This leads to better performance, compatibility, and functionality.
WSL2 runs on a lightweight virtual machine that integrates with Windows, offering near-native performance. This makes it ideal for developers who want to run Linux tools and commands on their Windows machines. WSL2 is particularly useful for those who need to develop and test applications in a Linux environment.
How Docker and WSL2 Work Together
Before WSL2, Docker on Windows was a challenge. Docker Desktop for Windows traditionally relied on a Hyper-V-based virtual machine to run Linux containers. However, this setup often caused performance issues and limited flexibility.
With WSL2, Docker integrates directly with the Linux kernel. Docker Desktop now supports WSL2 as the backend for running containers, allowing you to run Docker natively on your Windows machine without using Hyper-V. This integration streamlines the process and eliminates the need for additional tools, virtual machines, or dual-boot setups.
WSL2’s tight integration with Windows also enables seamless file sharing between Windows and Linux. You can access your Windows files from the Linux environment and vice versa. This means you can manage your Docker containers from both Windows and Linux terminals without switching between operating systems.
Benefits of Using Docker with WSL2 on Windows
Using Docker with WSL2 on Windows brings several advantages:
- Improved Performance: Docker runs more efficiently on WSL2 compared to the traditional Hyper-V setup. WSL2 offers better performance for file-intensive operations and faster startup times.
- Seamless Integration: With WSL2, you can access your Windows files from Linux and vice versa. This makes it easier to manage and build Docker containers from both environments.
- Lightweight Environment: WSL2 is much lighter than a traditional VM. You can enjoy the benefits of a Linux environment without the heavy resource consumption of Hyper-V.
- No Need for Extra Tools: With WSL2, you no longer need to install additional virtualization tools or Linux distributions. Everything is handled within the Windows operating system.
- Reduced Complexity: Running Docker on WSL2 simplifies your workflow. You no longer need to juggle between Windows and Linux systems or manage virtual machines separately.
Setting Up Docker and WSL2 on Windows
Let’s go through the step-by-step process of setting up Docker and WSL2 on your Windows machine.
Step 1: Enable WSL2 on Windows
- Open PowerShell as Administrator: Search for PowerShell in the Start menu, right-click it, and choose “Run as Administrator.”
- Enable WSL Feature: Run the following command to enable WSL on your machine:
wsl --install
This command will install the default Linux distribution and enable WSL. If WSL2 is not the default version, you can set it as default by running:
wsl --set-default-version 2
3. Restart Your Computer: After WSL is enabled, restart your computer to apply the changes.
Step 2: Install a Linux Distribution
Once WSL2 is enabled, you need to install a Linux distribution. You can choose from various distributions, such as Ubuntu, Debian, or Kali.
- Open the Microsoft Store: Search for “Microsoft Store” in the Start menu.
- Search for a Linux Distribution: Search for “Ubuntu” (or your preferred distribution) in the Microsoft Store.
- Install and Set Up: Click “Install” and wait for the download to complete. Once installed, open Ubuntu from the Start menu and follow the on-screen instructions to set up your username and password.
Step 3: Install Docker Desktop
Now that WSL2 is set up, you can install Docker Desktop.
Verify Installation: To verify that Docker is working correctly, open a WSL2 terminal and run the following command:
Download Docker Desktop: Visit the official Docker website and download Docker Desktop for Windows.
Install Docker Desktop: Run the downloaded installer and follow the on-screen instructions. During the installation, make sure to select the option to use WSL2 as the backend.
Start Docker Desktop: Once the installation is complete, start Docker Desktop. It will automatically detect WSL2 and integrate with it.
Verify Installation: To verify that Docker is working correctly, open a WSL2 terminal and run the following command:
docker --version
- This command should return the Docker version number, indicating that Docker is successfully installed.
Step 4: Build and Run Docker Containers
You can now build and run Docker containers from your WSL2 environment.
- Open a WSL2 Terminal: Open your chosen Linux distribution from the Start menu.
- Pull an Image: Use the following command to pull a Docker image:
docker pull ubuntu
3. Run a Container: Once the image is downloaded, you can run a container with this command:
docker run -it ubuntu bash
- This will start a container running Ubuntu, and you can interact with it using the bash shell.
Conclusion
Docker and WSL2 offer an elegant solution for building and running containers on Windows without needing additional tools. By leveraging WSL2’s lightweight Linux kernel, Docker can run more efficiently and seamlessly on a Windows environment. With improved performance, ease of use, and a simplified workflow, Docker with WSL2 is a game changer for Windows-based developers.
This setup ensures you can enjoy the best of both worlds—Windows for your everyday tasks and Linux for your development environment—without the need for complex configurations.
Thank you for reading the article! If you found the information useful, you can donate using the buttons below:
Donate ☕️ with PayPalDonate 💳 with Revolut