Share WSL rootless Podman instance with Windows

post-thumb

Share WSL rootless Podman instance with Windows

Background

Podman Desktop is the easiest way to start using Podman with windows. However currently Podman Desktop creates a new WSL instance with podman and connects windows to it. While it is pretty convenient for most of the users, this still requires another WSL instance and maybe not so convenient when you already have a preconfigured WSL dev environment.

So in this post I will share the setup I have been using to share the rootless podman running in a WSL instance with Windows.

Prerequisites

We will be using systemd for setting up rootless podman in WSL. So the only prerequisite for this setup to work is that the WSL version installed on your windows system should support systemd.

Systemd support was added to WSL in version: 0.67.6 . You can check the wsl version via the following command:


wsl -l -v # Should be 0.67.6 or above. 

If your WSL version is below the above mentioned version then you can check for WSL updates via wsl –update command.

Furthermore, systemd is not enabled in WSL by default and you can enable it at boot by creating a WSL configuration file /etc/wsl.conf within your WSL instance.


# /etc/wsl.conf file content

[boot]
systemd=true

Restart your WSL instance and you can check systemd status after reboot using this command:

systemctl list-unit-files --type=service

Install Podman on WSL

You can follow the podman installation steps for installing podman on your WSL instance.

For Ubuntu/Debian, installation via apt currently only supports v3.4.4 of podman and it is pretty outdated. However there is an updated version available via an unofficial source. You can read more about it here and here .

sudo apt install podman # installs podman 3.4.4

# For a more updated version: https://www.reddit.com/r/podman/comments/10wvjjp/how_i_backported_podman_4_on_ubuntu_2204/
# https://launchpad.net/~quarckster/+archive/ubuntu/containers

sudo add-apt-repository ppa:quarckster/containers
sudo apt update

After installation you can confirm podman installation by running following commands:


podman version # shows podman version

podman info # shows information about podman instance. Of particular interest are remoteSocket.Path and security.rootless=true.

Connect to podman service in WSL using SSH

We will be using ssh to connect to podman service running in WSL from windows.

Follow the following commands to setup ssh in WSL if it is not already setup:


# Install and enable ssh in Ubuntu/Debian. Please search online for other Linux distributions. 
sudo apt install openssh-server
sudo systemctl enable --now ssh.service
sudo systemctl start --now ssh.service

Generate ssh keys and add it to WSL authorized list:


# Generate and export ssh keys
export WINDOWS_HOME=/mnt/c/Users/<username>
ssh-keygen -b 2048 -t rsa -f $WINDOWS_HOME/.ssh/id_rsa_podman -q -N ""

# Add to authorized list for ssh connection
cat $WINDOWS_HOME/.ssh/id_rsa_podman.pub >> ~/.ssh/authorized_keys

Enable podman rootless service:


# Enable podman rootless service
systemctl --user enable --now podman.socket
systemctl --user start --now podman.socket

# Enable systemd services to continue to work even after user log offs
sudo loginctl enable-linger $USER

# Check podman remote information
podman --remote info

# Check podman socket fullpath. Needed while adding connection in windows.
ls $XDG_RUNTIME_DIR/podman/podman.sock

Finally from windows terminal, add the podman connection and set it as default. You will need podman cli for this step to work and the easiest way to install it in Windows is using Podman Desktop .


# Add podman connection using ssh key and podman socket path
podman system connection add wsl-podman --identity C:\Users\<username>\.ssh\id_rsa_podman ssh://<username>@localhost/run/user/<userid>/podman/podman.sock 
# output from $XDG_RUNTIME_DIR/podman/podman.sock should replace /run/user/<userid>/$XDG_RUNTIME_DIR/podman/podman.sock

# set the new connection as default if not set already
podman system connection default wsl-podman

# Check if evrything works
podman info
podman images

This should connect window with WSL podman. If you face connection issues then check if the WSL instance is running.

Known issue: Ubuntu WSL

Due to a bug in WSL2 for Ubuntu or maybe other distributions as well, after restarting WSL Ubuntu instance the file /run/user//bus is nuked and as a result systemctl –user returns file not found error.

The fix for this is to run the following command after restarting WSL VM every time:


sudo systemctl restart user@<userid>

A better fix is to disable wslg within WSL, if the graphical Linux apps are not being used as it is this particular mount that is causing the above mentioned issue. wslg can be disable globally by adding the .wslconfig file at %USERPROFILE% location in windows:

[wsl2]
guiApplications=false

That’s all folks. I hope you were able to make your podman instance in WSL work with windows.