Import Rocky Linux to WSL2 with Docker¶
- Linux PC running VirtualBox - VirtualBox will not run under windows 10 with WSL2, which is needed for later steps. You can also use a dual boot PC, or a live distribution, but make sure you have VirtualBox available.
- Docker Desktop for Windows 10 (or any docker installation)
Required * Windows 10 PC running WSL2 * Internet access
This guide shows the steps to create a tar image for a Docker container, and how to import that image into the Windows Subsystem for Linux (WSL). The steps outlined below are largely taken from Microsoft's Import any Linux distribution to use with WSL and from Docker's Create a base image, and adapted to the new distribution.
Please note that you need Virtual Box (to create the container for yourself) OR Docker to just pull the existing image from Docker Hub. This guide assumes the user is familiar with VirtualBox or Docker, and knows how to perform tasks like installing the VirtualBoxAdditions, and mounting shared drives.
It is also important to be familiar with the limitations of WSL, which will cause some functionality to break, work slowly, or work in unexpected ways. Depending on what you want to accomplish, the resulting distribution may or may not do what you want it to do. There are no guarantees.
Get Rocky Container Image¶
Pull From Docker Hub (on the same PC as your WSL2 install)¶
- From powershell or another WSL2 distro create a rocky container using the version you wish to start with. Replace the tag with your desired tag
docker run --name rocky-container rockylinux/rockylinux:8.4
- Confirm the container exists
docker container list --all | Select-String rocky-container
- Export container as tar
docker export rocky-container -o rocky-container.tar
Note: You don't have to be on the same system as your WSL2 install, you just need to be able to get that tar file to the system.
Create Your Own On Linux PC with VirtualBox¶
- Download the minimal image from Rocky Linux.
- Boot and install Rocky Linux on a new VirtualBox VM. The default settings are fine.
- Install VirtualBoxAdditions on your VM. This will require installing additional packages (as shown in the suggested command):
$ sudo yum install gcc make kernel-devel bzip2 binutils patch libgomp glibc-headers glibc-devel kernel-headers elfutils-libelf-devel tar
- Make a working directory, and copy files to be used for the tar image.
$ mkdir wsl_tar $ cd wsl_tar $ cp -p /etc/yum.conf . $ cp -p /etc/yum.repos.d/Rocky-BaseOS.repo .
- Edit your copy of
yum.confand add this line to it (modify the path as needed):
- Download the script to create a base image from Docker GitHub.
The script is called mkimage-yum.sh.
- Modify the script to create a tar file at the end, instead of starting Docker. Here are the suggested changes to the file:
## Change line 143 to simply create the tar file, without invoking Docker tar --numeric-owner -c -C "$target" . -f <path-to-the-new-tar-file> ## Comment out line 145
- Execute the script using this command (modify the path as needed):
$ sudo ./mkimage-yum.sh -y /home/<your_username>/wsl_tar/yum.conf baseos
- After the script finishes, your new tar file will be in the path you entered in the script above. Mount a shared drive with the host and move the tar file there.
You could also move the file to a USB drive or folder accessible to the Windows 10 PC. After moving the file to an external drive or folder, you won't need the VM anymore. You can delete it or modify it for other purposes.
Import into WSL2¶
- Create a directory to hold the Rocky Linux filesystem.
- In a PowerShell prompt, import Rocky Linux (it's named
rocky_rchere, but you can name it anything you like).
wsl --import rocky_rc <Path to RockyLinuxDirectory from step 1> <Path to tar file from previous sections>
- Verify Rocky Linux is installed with:
wsl -l -v
- Launch Rocky Linux with
wsl -d rocky_rc
- Set up Rocky Linux with the following bash commands (you'll need to be running as root).
yum update yum install glibc-langpack-en -y yum reinstall passwd sudo cracklib-dicts -y newUsername=<your new username> adduser -G wheel $newUsername echo -e "[user]\ndefault=$newUsername" >> /etc/wsl.conf passwd $newUsername
- Exit the bash prompt (Ctrl+D or exit).
- Back in a PowerShell prompt, shutdown all WSL running instances and start Rocky.
wsl --shutdown wsl -d rocky_rc
- Test and enjoy!
If you have Windows Terminal installed, the new WSL distro name will appear as an option on the pull-down menu, which is quite handy to launch it in the future. You can then customize it with colors, fonts, etc.
Even though you need WSL2 in order to perform the steps above, you can use the distro as WSL 1 or 2, by converting it with PowerShell commands.