MacOSM1/M2安装Lima

发布于 2023-04-06  1207 次阅读


目标:MacOSM1/M2下安装和启动Lima,构建和Win一致的内核环境,为后面使用Docker做准备

0.碎碎念

为什么会有这一篇文章呢,是因为本人十分好奇MacOS,然后上了一个终极大当(不是),在某一个夜晚,冲动的夜晚,剁手买了M2的Mac。

然后自然是到了喜闻乐见的安装环境。先是使用了Mac下面的Docker Desktop,通过DockerFile+DockerCpmpose安装Oracle19C的时候反复出现了错误,经过多次排查安装方式是绝对没有问题的,在阿里云Centos8和腾讯云Ubuntu22.04分别安装成功!!!安装成功的那一瞬间我的内心是崩溃的,一度产生卖掉Mac的冲动,本着既然已经买了就算要卖,也要把这个问题解决的信念,反复搜索,偶然在GitHub的一些Issues让我看到了一个回答,M1/M2芯片目前是无法通过Docker安装Oracle的。

这是一个M1/M2永远都不可能安装成功的错误(目前)!!!原因是因为Oracle19C运行需要使用到系统的底层内核AMD4才可以,然而苹果下的Docker任何版本都是ARM64,这就导致目前Oracle根本没有办法在Mac M2/M1芯片上正常运行。

后面经历了将近一周的排坑我使用了Github上的一个项目Lima(此处)和Colima(此处),感谢Lima!!不然我的Mac就要转手卖掉了(Mac是非常好的学习机!!强制学习)。


1.Lima

1.安装Lima

# 通过MacOS终端安装Lima
brew install lima

2.创建Lima实例

通过docker_lima.yaml文件,创建一个Docker模板的实例


limactl start --name=default /usr/local/share/lima/examples/docker_lima.yaml

docker_lima.yaml文件文件值得注意的地方


# 挂载设置
mounts:
- location: "~" # 挂载目录,这是挂载整个Users目录
  writable: true # 是否能在Lima容器里编辑文件

docker_lima.yaml文件


# Example to use Docker instead of containerd & nerdctl
# $ limactl start ./docker.yaml
# $ limactl shell docker docker run -it -v $HOME:$HOME --rm alpine

# To run `docker` on the host (assumes docker-cli is installed):
# $ export DOCKER_HOST=$(limactl list docker --format 'unix://{{.Dir}}/sock/docker.sock')
# $ docker ...

# This example requires Lima v0.8.0 or later
images:
# Try to use release-yyyyMMdd image if available. Note that release-yyyyMMdd will be removed after several months.
- location: "https://cloud-images.ubuntu.com/releases/22.04/release-20230210/ubuntu-22.04-server-cloudimg-amd64.img"
  arch: "x86_64"
  digest: "sha256:4401cf7e994842f11398a54d0159b689b2fcace166be6147013f128ddb15875e"
- location: "https://cloud-images.ubuntu.com/releases/22.04/release-20230210/ubuntu-22.04-server-cloudimg-arm64.img"
  arch: "aarch64"
  digest: "sha256:d044311b6e2d838f1175a67a5f7fa6bc0936f8001180df539025ec6587c33d28"
# Fallback to the latest release image.
# Hint: run `limactl prune` to invalidate the cache
- location: "https://cloud-images.ubuntu.com/releases/22.04/release/ubuntu-22.04-server-cloudimg-amd64.img"
  arch: "x86_64"
- location: "https://cloud-images.ubuntu.com/releases/22.04/release/ubuntu-22.04-server-cloudimg-arm64.img"
  arch: "aarch64"
arch: "x86_64"
mounts:
- location: "~"
  writable: true
  # 9p:
  #   # 对于可写的共享目录, cache 推荐类型为 mmap, 不写好像默认 fscache
  #   cache: "mmap"
# containerd is managed by Docker, not by Lima, so the values are set to false here.
containerd:
  system: false
  user: false
provision:
- mode: system
  # This script defines the host.docker.internal hostname when hostResolver is disabled.
  # It is also needed for lima 0.8.2 and earlier, which does not support hostResolver.hosts.
  # Names defined in /etc/hosts inside the VM are not resolved inside containers when
  # using the hostResolver; use hostResolver.hosts instead (requires lima 0.8.3 or later).
  script: |
    #!/bin/sh
    sed -i 's/host.lima.internal.*/host.lima.internal host.docker.internal/' /etc/hosts
- mode: system
  script: |
    #!/bin/bash
    set -eux -o pipefail
    command -v docker >/dev/null 2>&1 && exit 0
    export DEBIAN_FRONTEND=noninteractive
    curl -fsSL https://get.docker.com | sh
    # NOTE: you may remove the lines below, if you prefer to use rootful docker, not rootless
    systemctl disable --now docker
    apt-get install -y uidmap dbus-user-session
- mode: user
  script: |
    #!/bin/bash
    set -eux -o pipefail
    systemctl --user start dbus
    dockerd-rootless-setuptool.sh install
    docker context use rootless
probes:
- script: |
    #!/bin/bash
    set -eux -o pipefail
    if ! timeout 30s bash -c "until command -v docker >/dev/null 2>&1; do sleep 3; done"; then
      echo >&2 "docker is not installed yet"
      exit 1
    fi
    if ! timeout 30s bash -c "until pgrep rootlesskit; do sleep 3; done"; then
      echo >&2 "rootlesskit (used by rootless docker) is not running"
      exit 1
    fi
  hint: See "/var/log/cloud-init-output.log". in the guest
hostResolver:
  # hostResolver.hosts requires lima 0.8.3 or later. Names defined here will also
  # resolve inside containers, and not just inside the VM itself.
  hosts:
    host.docker.internal: host.lima.internal
portForwards:
- guestSocket: "/run/user/{{.UID}}/docker.sock"
  hostSocket: "{{.Dir}}/sock/docker.sock"
message: |
  To run `docker` on the host (assumes docker-cli is installed), run the following commands:
  ------
  docker context create lima-{{.Name}} --docker "host=unix://{{.Dir}}/sock/docker.sock"
  docker context use lima-{{.Name}}
  docker run hello-world
  ------

3.使用Lima

输入lima即可启动default容器,启动后的容器,启动后的容器和正常的Debian没有区别

lima

4.当lima下面的docker想使用科学上网

注意:MacOS Socks需要开启局域网模式。


# 查看宿主机IP地址
ipconfig getifaddr en0


# Docker容器内设置socks
export http_proxy="http://192.168.50.75:7890"
export https_proxy="http://192.168.50.75:7890"
export all_proxy="socks5://192.168.50.75:7890"


# 还原socks
unset http_proxy
unset https_proxy
unset all_proxy



届ける言葉を今は育ててる
最后更新于 2023-05-18