Blog Post

ITOps Talk Blog
2 MIN READ

Using WSL 2 on Windows Server 2022 to run Linux containers

ViniciusApolinario's avatar
Sep 13, 2022

Windows Subsystem for Linux 2 (WSL 2) is one of the most popular features for developers on Windows 10 and 11. It has recently been made available on Windows Server 2022. With this addition, you can now run Linux containers on WSL 2 on Windows Server 2022 for development and testing purposes.

 

Preparing your Windows Server 2022

WSL 2 on Windows Server 2022 has a different requirement than Windows 10 and 11. Before you get started with WSL 2, make sure your OS is up-to-date with the latest patches. WSL 2 requires at least the June 2022 patch Tuesday release, but it’s always a good practice to ensure the latest updates are installed. After that, all pre-requisites for WSL 2 to work on Windows Server 2022 can be implemented with:

wsl --install

Now you should have a fully functional Linux environment on your Windows Server 2022 host.

 

Install Docker on WSL 2 on Windows Server 2022

To install Docker on Windows Server 2022, you need to follow the instructions for the specific distribution you chose on the previous step. Here are the instructions for Ubuntu, according to Docker’s documentation page. On your Windows Server 2022, click the Start button, then click on the Ubuntu icon:

This will launch the WSL 2 console. You can now run commands on the Ubuntu Linux image as you would on a regular Linux environment. To get started, let’s update the apt package index and install packages to allow apt to use a repository over HTTPS:

sudo apt-get update
 sudo apt-get install \
    ca-certificates \
    curl \
    gnupg \
    lsb-release

Now, let’s add Docker’s official GPG key:

sudo mkdir -p /etc/apt/keyrings
 curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg

Use the following command to set up the repository:

echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

Install the Docker engine:

sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin

Now let’s verify the installation has been successfully completed:

sudo service docker start
sudo docker version

You should see the details of the Docker engine installed. That’s it. You can now run the regular Docker commands you are familiar with. In addition, you can run regular Windows containers on the same host:

 

Conclusion

WSL 2 can be used on Windows Server 2022 to run Linux containers on Windows Server 2022 for development and testing purposes. Please let us know your thoughts on this scenario on the comments below.

Published Sep 13, 2022
Version 1.0

29 Comments

  • 保哥 Will's avatar
    保哥 Will
    Copper Contributor

    Hi Jim,

    I already patched everything for my Windows Server 2022 and restart the computer and Docker many time. Same result.

     

  • restart your docker service and try again,

    sudo service docker restart

  • 保哥 Will's avatar
    保哥 Will
    Copper Contributor

    I can't run the command on my Windows Server 2022 after been installed followed by this article.

     

    will@WIN-K04FV8RCGD9:~$ sudo docker run -d -p 80:80 nginx
    7f0fceb4ca7f44230bd086906414bd9b2d3a833b020ee98ea5de62459328f998
    docker: Error response from daemon: driver failed programming external connectivity on endpoint bold_banach (86af9fcaf5fe4f4dc35dea902803fdb56e66407aab153127a28b8deb181442d3):  (iptables failed: iptables --wait -t nat -A DOCKER -p tcp -d 0/0 --dport 80 -j DNAT --to-destination 172.17.0.2:80 ! -i docker0: iptables: Invalid argument. Run `dmesg' for more information.
     (exit status 1)).

     

  • Thanks Mike for the pointer. Keep in mind WSL 2 on Server Core is not supported at this time (as you mentioned) although the team is looking into it.

  • Hi Gabriel, WSL 2 currently doesn't work on Server Core. The WSL team is aware of that, and they are working on a fix. We don't have a timeframe on which this this fix will be available at this moment.