Start Podman on WSL2 in 4 steps
Tags: webdev, beginners, productivity, tutorial
Published: 2022-01-21 08:06:04

Everyone works with Linux or Mac in every development project nowadays. And with that way, Podman on these systems might be easy to get started and we only keep focusing on what can we deliver. 

But the question is if you have to work with a Windows environment and WSL2 underline, then what can we do if we still want to use Podman (replace for Docker) on that box? 

I have read a lot of articles to bring up the Podman to Windows + WSL2 but to be honest, we have to handle a lot of exceptional cases. It might work or might not, and I don't really sure. 

So that I write down some tips and tricks to get it to work on my Windows 11 + WSL2 (Ubuntu 20) so that everyone else can have a reference to set up the containerized environment to develop the container driven application. 

Get starting with 4 simple steps that I have to run on every daily basis to have Podman run with me first. Subsequently, we will deep dive into how can we install the package and works to Podman on Windows

# Step 1: Start the Podman machine

Let's run your Ubuntu box (WSL2) and type

```bash
> podman machine start
```

You will get an error like

```bash
INFO[0000] waiting for clients...
INFO[0000] listening tcp://0.0.0.0:7777
INFO[0000] new connection from @ to /mnt/wslg/runtime-dir/podman/qemu_podman-machine-default.sock
Waiting for VM ...
Could not access KVM kernel module: Permission denied
qemu-system-x86_64: failed to initialize KVM: Permission denied
Error: dial unix /mnt/wslg/runtime-dir/podman/podman-machine-default_ready.sock: connect: connection refused
ERRO[0003] cannot receive packets from @, disconnecting: cannot read size from socket: EOF
```

Okay, after searching around Google, I have found the solution to make it work

```bash
> sudo chmod 666 /dev/kvm
```

Then, run the command above again

```bash
> podman machine start
```

It should work, and you should see the message as follows

```bash
INFO[0000] waiting for clients...
INFO[0000] listening tcp://0.0.0.0:7777
INFO[0000] new connection from @ to /mnt/wslg/runtime-dir/podman/qemu_podman-machine-default.sock
Waiting for VM ...
Machine "podman-machine-default" started successfully
```

# Step 2: Start ssh service on Ubuntu

Type the command

```bash
$ sudo service ssh start
```

You should see

```bash
 * Starting OpenBSD Secure Shell server sshd
```

# Step 3: Podman to bind the Unix socket so that the remote client can connect to

Run the command below

```bash
> podman system service --time=0 unix:///mnt/wslg/runtime-dir/podman/podman.sock
```

# Step 4: Connect to Podman by using Podman remote client from Windows 11 

```bash
> podman system connection add wsl --identity C:\Users\<your username>\.ssh\id_rsa_localhost ssh://<your username>@localhost/mnt/wslg/runtime-dir/podman/podman.sock
```

Then

```bash
> podman images
```

You should see

```bash
REPOSITORY                         TAG                    IMAGE ID      CREATED        SIZE
docker.io/library/postgres         11                     798225d02ad1  4 weeks ago    290 MB
docker.io/library/postgres         12                     afee8d9d57fc  4 weeks ago    379 MB
mcr.microsoft.com/dotnet/sdk       6.0                    e86d68dca8c7  4 weeks ago    725 MB
mcr.microsoft.com/dotnet/core/sdk  3.1                    e192c0c19af6  4 weeks ago    723 MB
docker.io/library/rabbitmq         3.8-management-alpine  a2fdbd753a1f  5 weeks ago    178 MB
docker.io/library/golang           1.17.3-buster          08f6df476f80  6 weeks ago    906 MB
docker.io/openpolicyagent/opa      0.35.0                 4a700ccbe0c6  7 weeks ago    72.9 MB
docker.io/library/postgres         11-alpine              4566dfb4cdeb  7 weeks ago    205 MB
docker.io/library/postgres         12-alpine              7c35a1e9b20d  7 weeks ago    207 MB
docker.io/library/hello-world      latest                 feb5d9fea6a5  3 months ago   19.9 kB
docker.io/library/redis            6.2.5-alpine           f6f2296798e9  4 months ago   33.5 MB
docker.io/openzipkin/zipkin        2.22.1                 ecad87b22fa0  14 months ago  148 MB
docker.io/mailhog/mailhog          v1.0.1                 4de68494cd0d  17 months ago  401 MB
```

That is what I have to do every do day when I want to get started in the Podman development environment.

# Setup Podman on Ubuntu 20 (WSL2) step by step

## Preparation your WSL2 with Ubuntu 20

```bash
> wsl # make sure you have WSL2 run on your machine
```

If you don't have WSL2 on your machine, please go to https://docs.microsoft.com/en-us/windows/wsl/install

Then you can run

```bash
> wsl --list
```

You should see

```bash
Windows Subsystem for Linux Distributions:
Ubuntu-20.04 (Default)
docker-desktop
docker-desktop-data
```

If you don't have `Ubuntu-20.04`, then you can have it by following the guidance at https://www.omgubuntu.co.uk/how-to-install-wsl2-on-windows-10

That's cool. You are ready for this journey!

## Jump into Ubuntu box and prepare Podman stuff

```bash
> export NAME=xUbuntu
> export VERSION_ID=20.04
> export WINDOWS_HOME=/mnt/c/Users/<yourUserNameHere>
```

The follow up with the guidance from Podman site to install it into Ubuntu

```bash
$ apt update
$ sudo sh -c "echo 'deb http://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable/${NAME}_${VERSION_ID}/ /' > /etc/apt/sources.list.d/devel:kubic:libcontainers:stable.list"
> wget -nv https://download.opensuse.org/repositories/devel:kubic:libcontainers:stable/${NAME}_${VERSION_ID}/Release.key -O Release.key
$ sudo apt-key add - < Release.key
$ sudo apt-get update -qq
$ sudo apt-get -qq -y install podman
$ sudo mkdir -p /etc/containers
> echo -e "[registries.search]\nregistries = ['docker.io', 'quay.io']" | sudo tee /etc/containers/registries.conf
```

Run `> podman info`, you should see Podman server information.

## Prepare a user and group for Podman on Ubuntu

```bash
$ sudo groupadd podman -g 2000
$ sudo useradd podman -u 2000 -g 2000
# add your user to podman group
$ sudo usermod -a -G podman $USER
```

## Install OpenSSH on WSL and generate keys

```bash
$ sudo apt-get -qq -y install openssh-server
$ sudo service ssh start
```

Then, we can generate the keys as follows

```
> export WINDOWS_HOME=/mnt/c/Users/<you windows username>/
> ssh-keygen -b 2048 -t rsa -f $WINDOWS_HOME/.ssh/id_rsa_localhost -q -N ""
> mkdir ~/.ssh
> cat $WINDOWS_HOME/.ssh/id_rsa_localhost.pub >> ~/.ssh/authorized_keys
```

Sometimes, you have got the error message `sshd: no hostkeys available -- exiting`, please follow the guidance at https://www.garron.me/en/linux/sshd-no-hostkeys-available-exiting.html to solve it.

## Install `quemu` and `gvproxy`

```bash
> apt install qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils
``` 

When you start Podman, and you have got [the error](https://www.redhat.com/sysadmin/replace-docker-podman-mac-revisited)

```
Error: unable to start host networking: "could not find \"gvproxy\" in one of [/usr/local/opt/podman/libexec /opt/homebrew/bin /opt/homebrew/opt/podman/libexec /usr/local/bin /usr/local/libexec/podman /usr/local/lib/podman /usr/libexec/podman /usr/lib/podman]"
```

Then, you have to install `gvproxy` on your machine. Download it at [`gvproxy`](https://github.com/containers/gvisor-tap-vsock/releases/tag/v0.3.0), then extract and put it into `/usr/local/libexec/podman`. Next step, you open up `~/.config/containers/containers.conf`, and add 

```bash
helper_binaries_dir = ["/usr/local/libexec/podman"]
```

Run the `> podman machine start` again, and you should be able to start this machine.

After finishing these setups, then every time we boost up the WSL2 with Ubuntu 20 distro, we can just follow 4 simple steps in this article to get starting your environment. Happy hacking!

Notes: We have several discussions about how to get Podman works on Windows + WSL2 (Ubuntu 20 distro) at:
- https://github.com/containers/podman/pull/12503
- https://github.com/containers/podman/issues/12056
- https://github.com/dotnet/tye/issues/1255

Other References:
- https://www.devcon5.ch/en/blog/2021/10/14/podman-for-windows/
- https://www.redhat.com/sysadmin/replace-docker-podman-mac-revisited
- https://www.redhat.com/sysadmin/podman-mac-machine-architecture
- https://github.com/containers/podman/discussions/12649
- Cover image is copied from: https://mkdev.me/en/posts/dockerless-part-3-moving-development-environment-to-containers-with-podman

This article is copied from https://hashnode.com/post/start-podman-on-wsl2-in-4-steps-ckyo3xwgk090wels1don39l7k