3
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

MacでDockerが遅いのをLimaで解決する(M1/Intel)

Posted at

MacでDockerが遅いという有名な問題、Limaに乗り換えることでネイティブ程度の速度が出た。
ということでDocker DeskTopを捨てました。

この手順でやると以下の感じになります

  • Mac上で今までどおり docker コマンドや docker-compose を利用できる
  • VSCodeのRemote Containerでも当然利用可能 
  • 特にFile IOが大きく改善するので体感的にめっちゃ早くなる

Limaに乗り換えの情報は色々あるのだけど、なかなかうまくいかなかったので
自分がやった手順を公開。

現在のdockerを捨てる

おそうじ。

以下でコンテナを全部止めて

docker stop $(docker ps -aq)
docker system prune

で全部消せる。イメージが多過ぎたりすると固まる。
Docker Desktopを再起動して、何度か実行すればOK

おそうじが終わったらdocker-desktopをアンインストールする

Lima セットアップ

Limaを使うとUbuntuの仮想マシンをスパッと作ってくれる。
その中にDocker Serverを立てる

参考

インストール

brew install lima

Lima VMを作成

Limaの設定を default.yaml というファイルで作成する
このファイルはLima 公式から持ってこれるが、一部カスタマイズをする

default.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=unix://$HOME/docker.sock
# $ docker ...

# This example requires Lima v0.7.3 or later
images:
  # Hint: run `limactl prune` to invalidate the "current" cache
  - location: "https://cloud-images.ubuntu.com/impish/current/impish-server-cloudimg-amd64.img"
    arch: "x86_64"
  - location: "https://cloud-images.ubuntu.com/impish/current/impish-server-cloudimg-arm64.img"
    arch: "aarch64"


# 重要。Mac側のディレクトリをcontainerにマウントした後、container側から書き込むために設定する
# マウントして書き込みを許可するMAC上のパスをセットして、writableをtrueにする。
# 今回は例として、Home DirectoryとHome Directoryのworkディレクトリを指定している
mounts:
  - location: "~"
    writable: true
  - location: "/tmp/lima"
    writable: true
  - location: "~/work"
    writable: true
ssh:
  localPort: 60006
  # Load ~/.ssh/*.pub in addition to $LIMA_HOME/_config/user.pub , for allowing DOCKER_HOST=ssh:// .
  # This option is enabled by default.
  # If you have an insecure key under ~/.ssh, do not use this option.
  loadDotSSHPubKeys: 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
    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


# 重要。Mac側のdockerコマンドでLima上のDockerサーバーに接続するため、docker.sockをMac側に晒す
portForwards:
  - guestSocket: "/run/user/{{.UID}}/docker.sock"
    hostSocket: "{{.Home}}/docker.sock"

ポイントは以下2点

  • mounts の location部分で docker volumeマウントするホスト側のディレクトリを指定して書き込み可能にしておく
    • これをしておかないとコンテナ側でマウントされたファイルの更新ができない。
  • portForwards で docker.sock をホストマシン(Mac)に露出する
    • これをすることで、Mac上から Lima VM上の docker.sock を直接接続できる。

色々試したけど、docker.sock を晒す方法が一番簡単でうまく動作した。

Lima上にゲストVMを作成する。

limactl start ./default.yaml

起動したらdockerの動作確認

lima docker version

mac osにdocker cliをインストール

mac上で docker コマンドを使えるように、docker のクライアントだけインストールする
例では#HOME/bin に配置しているけどPATH通っていればどこでもいい

# docker cli
# Intel Macの場合
curl -OL https://download.docker.com/mac/static/stable/x86_64/docker-20.10.11.tgz

# M1 Macの場合
curl -OL https://download.docker.com/mac/static/stable/aarch64/docker-20.10.11.tgz

mkdir -p $HOME/bin
tar xzvf docker-20.10.11.tgz
mv docker/docker $HOME/bin/.
chmod 755 $HOME/bin/docker

Lima VMのdockerとMacのdockerを接続

~/.zprofile に以下の記述を追加

Macのdocker cliからLima VMの docker.sock につなぐようにする

export DOCKER_HOST=unix://$HOME/docker.sock

設定を反映

source $HOME/.zprofile

これでmac上からdockerを操作できる

docker version

うまくいっていればDocker ServerとしてLima上のDockerにつながる

Macにdocker-compose v2をインストール

バイナリからインストールする。
Intel Macの場合とM1 Macの場合でバイナリが違う

mkdir -p ~/.docker/cli-plugins/

# Intel Mac
curl -SL https://github.com/docker/compose/releases/download/v2.1.1/docker-compose-darwin-x86_64 -o ~/.docker/cli-plugins/docker-compose

# M1 Mac
curl -SL https://github.com/docker/compose/releases/download/v2.1.1/docker-compose-darwin-aarch64 -o ~/.docker/cli-plugins/docker-compose


chmod +x ~/.docker/cli-plugins/docker-compose
docker compose version

これで docker compose up ができる

その他

docker compose uperr: exec: "docker-credential-osxkeychain": executable file not found in $PATH

これが出た場合、 ~/.docker/config.json を削除することで解消する

この構成でやる時の制限

Limaの制限でホスト側の1から1023番ポートまでの特権ポートは利用できない。
例えば、docker run する時のポート指定で 80:80 はダメで 8000:80 とかにする必要がある

3
10
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
3
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?