LoginSignup
12
13

More than 1 year has passed since last update.

Apple Silicon Mac(M1、M2) で Lima を使った Linux VMs with Rosetta 上で intel x86-64のdocker container イメージを実行する

Last updated at Posted at 2023-02-28

はじめに

M2 Macも発売され、そろそろintel Macで開発している方々も少数派となってきているのではないでしょうか。
M1、M2といったApple Silicon MacのCPUはARMアーキテクチャを採用している一方、多くの開発ではプロダクション環境はx86-64 (amd64)であり、Docker周りの環境構築に一工夫必要な場合も多いのではないでしょうか。また、Docker Desktopの有償化に伴い、所属している組織によっては開発環境のコンテナセットアップの方法を見直しているところもあるかと思います。

この記事では、以下の方法でApple Silicon Mac上でintel x86-64のDockerコンテナーを実行する手順を紹介します。

  • LimaというMac上でLinux VMを構築するツールを使って、
  • macOSのVirtualization.framework による仮想化で ARM Linux VMを立ち上げ、
  • ARM Linux VM 上でRosetta2 により intel x86-64 のコンテナイメージを実行する

環境

  • Apple MacBook Pro 13, M1, 2020

    • 16GB Memory
    • macOS Ventura 13.2.1
  • Lima version 0.14.2

Limaとは

Lima とは、macOS上でLinux仮想マシンを起動するツールです。WindowsにおけるWSL相当のものと言えます。
Lima上のLinux VM の上に、Docker や Podman でコンテナを実行していきます。

Virtualization.Framework とは

macOSのVirtualization.Frameworkは、macOS Bug Sur(macOS 11)から追加された、Macで仮想マシン(VM)を作成・管理するための高レベルAPIです。

Limaの仮想化は、通常QEMU をベースとして仮想マシンを作成します。QEMUは、オープンソースの汎用的なエミュレーター・仮想化ソフトウェアです。
QEMUはソフトウェアエミュレーションを行うため非常に遅くなりがちですが、Virtualization.FrameworkはApple Silicon上ではハードウェアアクセラレーションを利用して高速に動作します。
Limaでは、v0.14.0からこのVirtualization.Frameworkの仮想化が追加されており、Virtualization.Frameworkベースの仮想マシンが作成可能になっています。
参考:https://github.com/lima-vm/lima/blob/master/docs/multi-arch.md

Rosetta 2とは

Rosetta 2は、従来のIntel Mac用のバイナリをArmベースのM1 Macでも動くようにさせるためのエミュレーションソフトです。
Virtualization.FrameworkとRosetta2は、macOS 13 Venturaの新機能により連携することができます。この機能では、Virtualization.Frameworkを使ってLinux VMを作成し、Rosetta2を使ってx86-64 Linuxバイナリを実行することができます。これにより、Apple Silicon Macでもx86-64 Linuxアプリケーションを高速に実行できるようになります。

LimaとRosetta 2の組み合わせ

ここにあるように、LimaでVirtualizationFramework ベースの仮想マシン(vmType:vz) を作成し、仮想マシン上でRosetta2によるエミュレーションを行うことで高速にx86-64 Linuxバイナリを実行することが可能です。以下では、その方法を解説します。

最新版の Lima をインストール

Limaはbrew でインストールできます。

brew install lima

インストール済みの場合、最新版にアップデートしてください。

brew upgrade lima

仮想マシンの設定ファイルを作成

LimaのGithubリポジトリには、様々なタイプのVMを作成するための設定ファイルの例が配置されています。(https://github.com/lima-vm/lima/tree/master/examples)
ベースとして、vmType:vz の仮想マシンを作成する設定ファイル https://github.com/lima-vm/lima/blob/master/examples/experimental/vz.yaml を使用します。

vz.yaml
# Example to run ubuntu using vmType: vz instead of qemu (Default)
# This example requires Lima v0.14.0 or later and macOS 13.
vmType: "vz"
rosetta:
  # Enable Rosetta for Linux.
  # Hint: try `softwareupdate --install-rosetta` if Lima gets stuck at `Installing rosetta...`
  enabled: true
  # Register rosetta to /proc/sys/fs/binfmt_misc
  binfmt: true

images:
- 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"

mounts:
- location: "~"
- location: "/tmp/lima"
  writable: true
mountType: "virtiofs"

networks:
# The "vzNAT" IP address is accessible from the host, but not from other guests.
- vzNAT: true

これをベースとして、 仮想マシン内でDockerを使用したいので、Dockerが構築済みの設定を追加します。
今回は余計なトラブルを避けるために、 rootfulな環境である https://github.com/lima-vm/lima/blob/master/examples/docker-rootful.yaml を使用します。
docker-rootful.yaml の内容を先程のvz.yamlに追記して、以下のようなファイルを作成しました。

docker_rosetta.yaml
vmType: "vz"
rosetta:
  # Enable Rosetta for Linux.
  # Hint: try `softwareupdate --install-rosetta` if Lima gets stuck at `Installing rosetta...`
  enabled: true
  # Register rosetta to /proc/sys/fs/binfmt_misc
  binfmt: true
cpus: 4
memory: "8Gib"
# 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-20221201/ubuntu-22.04-server-cloudimg-amd64.img"
  arch: "x86_64"
  digest: "sha256:8a814737df484d9e2f4cb2c04c91629aea2fced6799fc36f77376f0da91dba65"
- location: "https://cloud-images.ubuntu.com/releases/22.04/release-20221201/ubuntu-22.04-server-cloudimg-arm64.img"
  arch: "aarch64"
  digest: "sha256:8a0477adcbdadefd58ae5c0625b53bbe618aedfe69983b824da8d02be0a8c961"
# 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"

mounts:
- location: "~"
  writable: true
- location: "/tmp/lima"
  writable: true
mountType: "virtiofs"

networks:
# The "vzNAT" IP address is accessible from the host, but not from other guests.
- vzNAT: true

# 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
    if [ ! -e /etc/systemd/system/docker.socket.d/override.conf ]; then
      mkdir -p /etc/systemd/system/docker.socket.d
      # Alternatively we could just add the user to the "docker" group, but that requires restarting the user session
      cat <<-EOF >/etc/systemd/system/docker.socket.d/override.conf
      [Socket]
      SocketUser=${LIMA_CIDATA_USER}
    EOF
    fi
    export DEBIAN_FRONTEND=noninteractive
    curl -fsSL https://get.docker.com | sh
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 dockerd; do sleep 3; done"; then
      echo >&2 "dockerd 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: "/var/run/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
  ------

cpuやメモリはご使用の環境に合わせて適当に設定していただければと思います。

仮想マシンの作成・開始

limactl のコマンドで、上記で作成した設定ファイルから仮想マシンのインスタンスを作成・実行します。

limactl start docker_rosetta.yaml

------

INFO[0062] READY. Run `limactl shell docker_rosetta` to open the shell.
INFO[0062] Message from the instance "docker_rosetta":
To run `docker` on the host (assumes docker-cli is installed), run the following commands:
------
docker context create lima-docker_rosetta --docker "host=unix:///Users/{username}/.lima/docker_rosetta/sock/docker.sock"
docker context use lima-docker_rosetta
docker run hello-world
------

インスタンスが作成され、仮想マシンのシェルに入るコマンドや、ホストマシンから仮想マシンのDockerを使用するためにDocker contextを設定するためのコマンドが出力されています。まずは作成されたインスタンスを確認してみましょう。

limactl ls
NAME              STATUS     SSH                VMTYPE    ARCH       CPUS    MEMORY    DISK      DIR
default           Stopped    127.0.0.1:0        qemu      aarch64    4       4GiB      100GiB    ~/.lima/default
docker_rosetta    Running    127.0.0.1:50669    vz        aarch64    4       8GiB      100GiB    ~/.lima/docker_rosetta

vmType:vz で作成されています。シェルに入ってみます。

limactl shell docker_rosetta
lima-dockerrosetta:/Users/{username}/lima$uname -a
Linux lima-dockerrosetta 5.15.0-56-generic #62-Ubuntu SMP Tue Nov 22 19:56:13 UTC 2022 aarch64 aarch64 aarch64 GNU/Linux

aarch64 のLinux仮想マシンが実行されていることが確認できました。

Intel containers on ARM VM on ARM Host

今回の目的である、Intel containers on ARM VM on ARM Host、ARM VM上でIntelコンテナのRosetta2による実行を試してみましょう。

せっかくなのでそれなりの構成のアプリを動かしてみることにします。awesome-compose から適当に探してみましょう。フロントエンドのアプリが動いているものがいいですね。MongoDBも使うことが多いので、react-express-mongodbを使いましょう。一式をダウンロードしておきます。

下記3つのファイルを編集し、明示的にx86-64のコンテナイメージを使用するようにします。

  • react-express-mongodb/backend/Dockerfile
    FROM --platform=linux/x86_64 node:lts-buster-slim AS development
    
  • react-express-mongodb/flontend/Dockerfile
    FROM --platform=linux/x86_64 node:lts-buster AS development
    
  • react-express-mongodb/compose.yaml
        mongo:
            restart: always
            image: mongo:4.2.0
            platform: linux/x86_64
    

docker compose してみましょう。

docker compose up -d

aarch64のVM上でx86-64のコンテナを実行しているので、 The requested image's platform (linux/amd64) does not match the detected host platform (linux/arm64/v8) and no specific platform was requested のWarningが出ています。コンテナの実行状況を確認しましょう。

docker ps
CONTAINER ID   IMAGE                            COMMAND                  CREATED          STATUS         PORTS                                       NAMES
303f0e05febb   react-express-mongodb-frontend   "docker-entrypoint.s…"   12 seconds ago   Up 8 seconds   0.0.0.0:3000->3000/tcp, :::3000->3000/tcp   react-express-mongodb-frontend-1
6fea37a907f6   react-express-mongodb-backend    "docker-entrypoint.s…"   12 seconds ago   Up 8 seconds   3000/tcp                                    react-express-mongodb-backend-1
b18b615441cc   mongo:4.2.0                      "docker-entrypoint.s…"   12 seconds ago   Up 9 seconds   27017/tcp                                   react-express-mongodb-mongo-1

正常に稼働しています。
ポート3000で動いているので、localhost:3000 を確認してみましょう。
image.png
動いています。ToDo管理アプリのようですね。新規ToDoを作成することが可能です。

各コンテナの中に入って、アーキテクチャを確認してみましょう。

docker exec -it {cantainer_id} bash
root@xxxxxxxxxxxxx:/usr/src/app# uname -a
Linux xxxxxxxxxxxxx 5.15.0-56-generic #62-Ubuntu SMP Tue Nov 22 19:56:13 UTC 2022 x86_64 GNU/Linux

x86_64 GNU/Linux が動いていることが確認できました!

まとめ

こちらにあるように、LimaにおけるVirtualization.Frameworkの仮想化は未だ実験的な実装のようです。(2023/02/27現在)
私が試している最中にも、しばらく放置しているとVMのCPU使用率が突然跳ね上がってハングしたりと不安定なところもありました。
Docker Desktop でも、仮想化にVirtualization.Frameworkがデフォルトで使用されるようになり、Rosetta 2によるエミュレーションもサポートされるようになりました。
今後に期待を持って見守りたいと思います。

12
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
13