Mac虚拟机,在Win的Linux子系统的Docker里面运行MacOS,操作系统终极大套娃

3 min read 2 days ago
Published on Jan 04, 2025 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Introduction

This tutorial guides you through the process of running MacOS inside a Docker container on a Windows machine, leveraging the Windows Subsystem for Linux (WSL). This setup allows you to explore Mac-specific software and test cross-platform compatibility, all within a multi-layered operating system environment.

Step 1: Install Windows Subsystem for Linux

  1. Enable WSL:

    • Open PowerShell as an administrator.
    • Run the command:
      wsl --install
      
    • Restart your computer when prompted.
  2. Install a Linux Distribution:

    • Go to the Microsoft Store and choose a Linux distribution (e.g., Ubuntu).
    • Install the selected distribution.
  3. Set Up Your Linux Environment:

    • Open the installed Linux distribution from your start menu.
    • Follow the prompts to create a user account and password.

Step 2: Install Docker in WSL

  1. Install Docker Desktop:

    • Download Docker Desktop from the official Docker website.
    • Run the installer and follow the on-screen instructions.
    • Ensure that you enable the WSL integration during installation.
  2. Verify Docker Installation:

    • Open your WSL terminal.
    • Run the command:
      docker --version
      
    • You should see the Docker version installed.

Step 3: Set Up Docker OSX

  1. Pull the Docker OSX Image:

    • In your WSL terminal, run:
      docker pull sickcodes/docker-osx
      
  2. Run the Docker OSX Container:

    • Create and run a new container using:
      docker run --name docker-osx --privileged -d \
      -e HOST_CLONE=true \
      -e RAM=4G \
      -e CPU=4 \
      -e VIDEO_DRIVER=virtio \
      -p 50922:22 \
      sickcodes/docker-osx
      
    • Adjust the RAM and CPU values according to your system's capabilities.

Step 4: Access MacOS in Docker

  1. Get Container IP Address:

    • Run the command to find the IP of the running container:
      docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' docker-osx
      
  2. Connect via SSH:

    • Use an SSH client or your terminal to connect to your MacOS instance:
      ssh -p 50922 your_username@<container_ip>
      
    • Replace <container_ip> with the IP address obtained in the previous step.

Step 5: Explore MacOS Features

  • Once connected, you can explore MacOS features, install applications, and test software compatibility.

Conclusion

You have successfully set up a MacOS environment running on Docker within the Windows Subsystem for Linux. This complex layering allows for unique development opportunities and testing scenarios. As next steps, consider installing specific Mac applications or experimenting with different configurations to maximize your experience.