在windows上使用docker运行macOS


概述

近日在github上发现一个项目,可以在docker上运行macOS。

项目地址:https://github.com/sickcodes/Docker-OSX

通过这个项目,我们可以很方便的借助wsl和docker在windows上运行macOS。可以用于CI/CD、IOS开发或者简单的体验macOS使用。

官网页面上列出了一些特性:

  • use iPhone OSX KVM on Linux using usbfluxd!
  • macOS Monterey VM on Linux!
  • Folder sharing-
  • USB passthrough (hotplug too)
  • SSH enabled (localhost:50922)
  • VNC enabled (localhost:8888) if using ./vnc version
  • iMessage security research via serial number generator!
  • X11 forwarding is enabled
  • runs on top of QEMU + KVM
  • supports Big Sur, custom images, Xvfb headless mode
  • you can clone your container with docker commit

项目还支持在k8s中部署

具体的安装步骤详见上边的项目地址,下边列举一些简要的步骤

前置条件

  1. 安装wsl2(自带wslg)
  2. 安装windows docker,并且使用wsl2作为docker的后端
  3. BIOS开启虚拟化,以及wsl2开启kvm和嵌套虚拟化支持
  4. 20GB+++ disk space for bare minimum installation (50GB if using Xcode)

wsl安装

详见https://docs.microsoft.com/zh-cn/windows/wsl/install
安装后,可以起一个ubuntu24.04的wsl2

wsl --install -d Ubuntu-24.04

修改wsl2的配置(C:/Users//.wslconfig),开启嵌套虚拟化支持

[wsl2]
nestedVirtualization=true

docker windows安装

https://docs.docker.com/desktop/install/windows-install/

安装后,确定docker使用我们刚安装的wsl作为后端

General -> “Use the WSL2 based engine”;
Resources -> WSL Integration -> “Enable integration with my default WSL distro”,

安装wsl必要的软件

sudo apt install qemu qemu-kvm libvirt-clients libvirt-daemon-system bridge-utils virt-manager libguestfs-tools

确保kvm是可用的

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

有如上提示表明正常

步骤

简单启动一个MacOS Sonoma

version: '3.8'

services:
docker-osx:
image: sickcodes/docker-osx:sonoma
environment:
DISPLAY: ${DISPLAY:-:0.0}
#NOPICKER: false
GENERATE_UNIQUE: true
GENERATE_SPECIFIC: true
DEVICE_MODEL: iMacPro1,1
CPU: Haswell-noTSX
CPUID_FLAGS: "kvm=on,vendor=GenuineIntel,+invtsc,vmware-cpuid-freq=on"
MASTER_PLIST_URL: "https://raw.githubusercontent.com/sickcodes/osx-serial-generator/master/config-custom-sonoma.plist"
#WIDTH: 2560
WIDTH: "1920" # 设置屏幕分辨率
HEIGHT: "1080"
#HEIGHT: 1600
ports:
- "50922:10022"
devices:
- "/dev/kvm:/dev/kvm"
volumes:
- /mnt/wslg/.X11-unix:/tmp/.X11-unix
- ${PWD}/output.env:/env
- ${PWD}/mac_hdd_ng.img:/home/arch/OSX-KVM/mac_hdd_ng.img
stdin_open: true
tty: true
docker-compose up -d

启动后,会开启一个wslg的窗口,可以看到macOS的安装页面。跟随以下安装步骤:

Boot the macOS Base System (Press Enter)

Click Disk Utility

Erase the BIGGEST disk (around 200gb default), DO NOT MODIFY THE SMALLER DISKS. – if you can’t click erase, you may need to reduce the disk size by 1kb

(optional) Create a partition using the unused space to house the OS and your files if you want to limit the capacity. (For Xcode 12 partition at least 60gb.)

Click Reinstall macOS

耗时大约一个小时,需要多次重启;因为安装过程需要联网下载文件,需要保证网络是通畅稳定的,否则可能会失败。

对上述步骤的磁盘进行持久化

通过docker cp 命令将容器内的磁盘拷贝到宿主机上,然后在docker-compose.yml中挂载这个磁盘。

假设docker macos容器名为macos,将镜像中的磁盘拷贝到docker-compose.yaml的同级目录下(这里为.)

docker cp macos:/home/arch/OSX-KVM/mac_hdd_ng.img .

待拷贝完成后,修改docker-compose.yaml文件,将mac_hdd_ng.img挂载到容器中

version: '3.8'
services:
docker-osx:
image: sickcodes/docker-osx:sonoma
ports:
- "50922:10022"
devices:
- "/dev/kvm:/dev/kvm"
environment:
DISPLAY: ${DISPLAY:-:0.0}
GENERATE_UNIQUE: true
CPU: Haswell-noTSX
CPUID_FLAGS: "kvm=on,vendor=GenuineIntel,+invtsc,vmware-cpuid-freq=on"
MASTER_PLIST_URL: "https://raw.githubusercontent.com/sickcodes/osx-serial-generator/master/config-custom-sonoma.plist"
volumes:
- /mnt/wslg/.X11-unix:/tmp/.X11-unix
tty: true
stdin_open: true

再次启动容器

docker-compose up -d

操作没问题的话,就可以看到我们之前安装的macOS系统了。


Author: deepwzh
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint policy. If reproduced, please indicate source deepwzh !
  TOC