Earlier, to run a standalone Linux container on a Windows machine, you needed a tool like Docker Desktop. But now, WSL Containers (wslc) are available as part of Windows Subsystem for Linux, enabling you to build and run custom Linux containers without any 3rd-party tool. It’s a command-line tool wslc.exe one can use to easily spawn custom Linux containers. Let’s see how to use it.
In this tutorial, you will learn how to install WSLc and how to use it in different ways to match your needs. Once you get the hang of it, you'll forget all other tools. It's easy to use and is highly customizable.
What Exactly is WSLc?
It's part of Windows Subsystem for Linux to build and run Linux containers from both the command-line and through an API. Here are the major parts of this tool:
- Command-Line interface: It's the preferred manual approach for those who love to work in a command-line environment.
- API and SDK: Developers can use the API to programmatically launch custom Linux containers from within their applications.
WSL vs WSLc: WSL is a full-fledged Linux distribution running in a VM. WSLc is a small, completely isolated, and minimal Linux environment that can be automatically disposed of after use.
Containers are used in cloud environments and in automated testing too.
Prerequisites for Using WSLc
The following are the requirements for using WSLc:
- Windows 11 or Windows 10 version 1903 or higher (Build 18362.1049 or later).
- WSL version 2.9.3 or later.
- Virtualization should be enabled in BIOS/UEFI settings.
Installing WSLc: Step-by-Step Method
Because WSLc is part of WSL, you don't need a separate installation method for it.
- Open PowerShell with admin privileges.
- Update your WSL installation.
wsl --update - Confirm the version.
Ensure the version is 2.9.3 or higher.wsl --version - Test the
wslccommand.
There's also an alias,wslc --helpcontainer.exe, for it. Both function in a similar way.
Running Your First WSLc Containers
Let's start with a basic one Microsoft recommends for the first-time users.
wslc run --rm -it ubuntu:latest bash -c "echo Hello world!"
Let's see what all the parameters and flags mean.
run- grab the image (if required) and launch/run the container.--rm- purge the container when it terminates.-it- For interactive terminal sessions, use this switch.ubuntu:latest- This is the image the container will use.
Users who've already used Docker will find this approach quite familiar.
Running a Web Server
How about running an Nginx server in the background? Now that's something useful!
wslc run -it --rm -d -p 8080:80 --name web nginx
curl localhost:8080
wslc container ps
wslc container stop web
Here's what each of the commands does in the given order.
- The first command starts the WSLc container running an Nginx web server in a detached
(-d)mode. It also maps port 8080 to port 80 within the container. - To test, fetch the Nginx welcome page using
curlin the command-line environment. - The 3rd command
(ps)lists the currently running containers. - The last one stops and purges the container. The latter action is performed due to the use of the
--rmflag.
You can also use the localhost:8080 address in a browser tab to see the Nginx server's welcome page.
Core WSLc Commands You Must Know
Here's a list of commands one must know to fully utilize the power of WSLc containers:
| Command | What it Does |
|---|---|
wslc run <image> |
Run/start a container using a specific image |
wslc image ls |
Displays all the locally available container images |
wslc container ps |
Displays all the currently running containers |
wslc container stop <name> |
Terminate a specific running container |
wslc container restart <name> |
Restart a specific running container |
wslc container cp |
Copy the files between container and Windows host |
wslc build |
Build a Linux container image for future use |
wslc network create <name> |
Create a network container as per your parameters |
wslc volume create <name> |
Create a persisting storage volume containers can use |
I'll recommend trying all of these commands against a test project before using them for the real ones.
Example: Packaging and Running a CLI Tool
Let's say you've built a small CLI tool in Rust. Now you want to run and test it in a clean Linux environment. To get started, you’ll first create a multi-stage Dockerfile. Here's an example:
# Build stage
FROM rust:1.80 AS builder
WORKDIR /app
COPY . .
RUN cargo build --release
# Runtime stage
FROM debian:bookworm-slim
COPY --from=builder /app/target/release/mytool /usr/local/bin/mytool
ENTRYPOINT ["mytool"]
Thereafter, build and run it using a wslc container.
wslc build -t mytool .
wslc run --rm mytool --help
This is how you can test the behaviour of your CLI tool in a minimal and clean Debian environment, right on your Windows machine.
Creating and Managing Persistent Data (Volumes)
Containers are generally disposable and cease to exist once they are terminated. And so does the data inside them. WSLc allows you to create persistent storage volumes that can be used across multiple container runs. Here's an example:
wslc volume create --driver vhd -o SizeBytes=200000000 my-volume
wslc container run -v my-volume:/volume -it debian:latest findmnt /volume
Here, you are creating an approximate 200MB virtual disk-based volume mounted at /volume within a Debian-powered container.
You can also bind a Windows directory to a volume container.
Customizing and Configuring Memory, Storage, and CPU Resources
By default, WSL automatically allocates optimal system resources for containers depending on the configuration of your Windows machine.
If you still feel the need to customize these resource allocation settings, you can access them through the WSL Settings in the Windows main menu.
Depending on the use case, you can adjust the CPU, memory, swap space, storage location, and much more, though I'll recommend sticking with the defaults.
Conclusion
WSLc is a huge leap in the Windows Subsystem for Linux ecosystem, enabling you to create and run lightweight and clean Linux containers.
At the time of this writing, the Docker Compose-style workflow is still on the roadmap. Other than that, WSLc is mature enough with enough features to quickly create custom Linux containers on a Windows machine.