Install a macOS VM in a WSL-based Docker

Reference: https://www.bilibili.com/video/BV1j2421o77K/

This is an ultimate matryoshka Mac system, which I installed into a Linux subsystem running on Windows inside a Docker container. It's a combination of three major operating systems all at once. We can use this system to try out some Mac-specific software, develop cross-platform software, test compatibility, and more.

In this page, let's take a look at how to set up this interesting system. For this project, I'm using Docker OSX, whichc cows you to quickly start a MacOS environment using Docker. However, this project has fairly high hardware requirements, and a Linux mini-computer's performance may not be sufficient. So this time, I'm going to run Docker on my Windows computer.

First, initial setup for Docker-OSX to run on Windows

Go to C:/Users/<Your_Name>/.wslconfig and add nestedVirtualization=true to the end of the file (If the file doesn't exist, create it). For more information about the .wslconfig file check this link. Verify that you have selected "Show Hidden Files" and "Show File Extensions" in File Explorer options. The result should be like this:

[wsl2]
nestedVirtualization=true

This step is to allow nesting of virtual machines.

⚠️If you are running your WSL, don't forget to run wsl --shutdown to activate the above config.

Second, install a Docker desktop

🔗Downloading links:

Third, modify configuration

Modification

Insure that the options, framed in the figures below, are selected.

select "Use the WSL 2 based engine"
select "Enable integration with my default WSL distro" and your default WSL, then check "Apply & restart"

Verification

Enter docker ps in your WSL to check whether you can use docker in your WSL.

You success if you get the following return:

check if KVM is enabled by using the kvm-ok command. The output should look like this:

INFO: /dev/kvm exists
KVM acceleration can be used

Fourth, install mac OS in docker

Choose the corresponding image of mac OS from here: https://github.com/sickcodes/Docker-OSX?tab=readme-ov-file#technical-details

For an example, I choose Ventura (2.36GB).

  • Code for 🇬🇧:

docker run -it \
    --device /dev/kvm \
    -p 50922:10022 \
    -e "DISPLAY=${DISPLAY:-:0.0}" \
    -v /mnt/wslg/.X11-unix:/tmp/.X11-unix \
    -e GENERATE_UNIQUE=true \
    -e MASTER_PLIST_URL='https://raw.githubusercontent.com/sickcodes/osx-serial-generator/master/config-custom.plist' \
    -e SHORTNAME=ventura \
    sickcodes/docker-osx:latest

# docker build -t docker-osx .
  • Code for 🇨🇳:

docker run -it \
    --name macos \
    --device /dev/kvm \
    -p 50922:10022 \
    -e "DISPLAY=${DISPLAY:-:0.0}" \
    -v /mnt/wslg/.X11-unix:/tmp/.X11-unix \
    -e GENERATE_UNIQUE=true \
    -e "MASTER_PLIST_URL=https://raw.githubusercontent.com/sickcodes/osx-serial-generator/master/config-custom.plist" \
    registry.cn-hangzhou.aliyuncs.com/shrimp-images/docker-osx:ventura

# docker build -t docker-osx .

ERROR: wsl: 检测到 localhost 代理配置,但未镜像到 WSL。NAT 模式下的 WSL 不支持 localhost 代理

SOLUTION: [Reference]

ERROR: curl: (7) Failed to connect to raw.githubusercontent.com port 443 after 16 ms: Couldn't connect to server

SOLUTION1: add the following codes into the loacal host file (sudo vim /etc/hosts)[Reference]:

185.199.108.133 raw.githubusercontent.com
185.199.109.133 raw.githubusercontent.com
185.199.110.133 raw.githubusercontent.com
185.199.110.133 raw.githubusercontent.com

🌟SOLUTION2: set proxy for wsl

docker run -it \
    --name macos \
    --device /dev/kvm \
    -p 50922:10022 \
    -e "DISPLAY=${DISPLAY:-:0.0}" \
    -v /mnt/wslg/.X11-unix:/tmp/.X11-unix \
    -e GENERATE_UNIQUE=true \
    -e "MASTER_PLIST_URL=https://raw.githubusercontent.com/sickcodes/osx-serial-generator/master/config-custom.plist" \
    -e http_proxy=http://<your_proxy_server>:<your_port> \
    -e https_proxy=http://<your_proxy_server>:<your_port> \
    registry.cn-hangzhou.aliyuncs.com/shrimp-images/docker-osx:ventura

# docker build -t docker-osx .

⚠️NOTE: <your_proxy_server> can be accessed after Enable LAN connection on your proxy client (vEthernet (WSL (Hyper-V firewall))).

Last updated