🐳

Docker for Mac から Lima に切り替える

2021/09/28に公開

Docker for Mac から他のツールに切り替えないといけなくなった際に、ドヤ顔出来るようにシュミレーションを行った時のログです。

Lima とは?

Windows 上で Linux を動作させる WSL があるように、MacOS 上でも Linux を動作させる MacOS 版 WSL として開発されたのが Lima です(雑)。

Lima と Docker をインストールする

Docker がインストール済みの場合は、Lima のみをインストールする。

$ brew install lima docker

Lima の設定ファイルを設置する

公式が配布している設定ファイルを参考にlima-hoge.ymlを設置します。

# VMで利用するリソースを追加で設定
cpus: 2
memory: "4GiB"
disk: "30GiB"

images:
  - location: "https://cloud-images.ubuntu.com/hirsute/current/hirsute-server-cloudimg-amd64.img"
    arch: "x86_64"
  - location: "https://cloud-images.ubuntu.com/hirsute/current/hirsute-server-cloudimg-arm64.img"
    arch: "aarch64"
mounts:
  - location: "~/projects/hoge" # フルパスで指定する必要がある(相対パスが指定出来ない)
    writable: true
ssh:
  localPort: 60006
  loadDotSSHPubKeys: true
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
      systemctl disable --now docker
      apt-get install -y uidmap
  - mode: user
    script: |
      #!/bin/bash
      set -eux -o pipefail
      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

VMを起動する

$ limactl start lima-hoge.yml
? Creating an instance "lima-hoge" Proceed with the default configuration
INFO[0001] Attempting to download the image from "https://cloud-images.ubuntu.com/hirsute/current/hirsute-server-cloudimg-amd64.img" 
INFO[0002] Using cache "/Users/xxxxx/Library/Caches/lima/download/by-url-sha256/e1fed960ebd29619676c7ab7535bc83f7fb2ad71739edb6fde4e17bce0b61a47/data" 
INFO[0002] [hostagent] Starting QEMU (hint: to watch the boot progress, see "/Users/xxxxx/.lima/lima-hoge/serial.log") 
INFO[0002] SSH Local Port: 60006                        
INFO[0002] [hostagent] Waiting for the essential requirement 1 of 4: "ssh" 
INFO[0012] [hostagent] Waiting for the essential requirement 1 of 4: "ssh" 
INFO[0032] [hostagent] The essential requirement 1 of 4 is satisfied 
INFO[0032] [hostagent] Waiting for the essential requirement 2 of 4: "sshfs binary to be installed" 
INFO[0053] [hostagent] The essential requirement 2 of 4 is satisfied 
INFO[0053] [hostagent] Waiting for the essential requirement 3 of 4: "/etc/fuse.conf to contain \"user_allow_other\"" 
INFO[0056] [hostagent] The essential requirement 3 of 4 is satisfied 
INFO[0056] [hostagent] Waiting for the essential requirement 4 of 4: "the guest agent to be running" 
INFO[0056] [hostagent] The essential requirement 4 of 4 is satisfied 
INFO[0056] [hostagent] Mounting "/Users/xxxxx/projects/hoge" 
INFO[0057] [hostagent] Waiting for the optional requirement 1 of 1: "user probe 1/1" 
INFO[0057] [hostagent] Forwarding "/run/user/501/lima-guestagent.sock" (guest) to "/Users/xxxxx/.lima/lima-hoge/ga.sock" (host) 
INFO[0057] [hostagent] Not forwarding TCP 127.0.0.53:53 
INFO[0057] [hostagent] Not forwarding TCP 0.0.0.0:22    
INFO[0057] [hostagent] Not forwarding TCP [::]:22       
INFO[0105] [hostagent] The optional requirement 1 of 1 is satisfied 
INFO[0106] READY. Run `limactl shell lima-hoge` to open the shell. 
$ limactl ls
NAME          STATUS     SSH                ARCH      DIR
lima-hoge    Running    127.0.0.1:60006    x86_64    /Users/xxxxx/.lima/lima-hoge

limactl の説明

$ limactl -h
NAME:
   limactl - Lima: Linux virtual machines

USAGE:
   limactl [global options] command [command options] [arguments...]

VERSION:
   0.6.4

COMMANDS:
   start               Start an instance of Lima. If the instance does not exist, open an editor for creating new one, with name "default"
   stop                Stop an instance
   shell               Execute shell in Lima
   copy, cp            Copy files between host and guest
   list, ls            List instances of Lima.
   delete, remove, rm  Delete an instance of Lima.
   validate            Validate yaml files
   prune               Prune garbage objects
   completion          Show shell completion
   help, h             Shows a list of commands or help for one command

GLOBAL OPTIONS:
   --debug        debug mode (default: false)
   --help, -h     show help (default: false)
   --version, -v  print the version (default: false)

limactl shell lima-hogeでVMにログイン出来るが、export LIMA_INSTANCE=lima-hogeをすれば、limaでログインやコマンドを実行出来る。

$ limactl shell lima-hoge
xxxxx@lima-lima-hoge:/Users/xxxxx/projects/hoge$ exit
logout
$ 
$ export LIMA_INSTANCE=lima-hoge
$ lima
xxxxx@lima-lima-hoge:/Users/xxxxx/projects/hoge$ exit
logout
$ 
$ lima docker compose version
Docker Compose version v2.0.0-rc.3

Docker Compose V2 を利用する

Docker Compose V2 は、まだデフォルトでは有効になっていない為、V2を利用出来るようにする。

$ export LIMA_INSTANCE=lima-hoge
$ lima
xxxxx@lima-lima-hoge:/Users/xxxxx/projects/hoge$ mkdir -p ~/.docker/cli-plugins/
xxxxx@lima-lima-hoge:/Users/xxxxx/projects/hoge$ curl -SL https://github.com/docker/compose/releases/download/v2.0.0-rc.3/docker-compose-linux-amd64 -o ~/.docker/cli-plugins/docker-compose
xxxxx@lima-lima-hoge:/Users/xxxxx/projects/hoge$ chmod +x ~/.docker/cli-plugins/docker-compose
xxxxx@lima-lima-hoge:/Users/xxxxx/projects/hoge$ exit

普段と同じようにビルドする

$ lima docker compose build
$ lima docker compose up -d

VMを停止、削除する

$ limactl ls
NAME          STATUS     SSH                ARCH      DIR
lima-hoge    Running    127.0.0.1:60006    x86_64    /Users/xxxxx/.lima/lima-hoge
$
$ limactl stop lima-hoge
INFO[0000] Sending SIGINT to hostagent process 33747    
INFO[0000] Waiting for the host agent and the qemu processes to shut down 
INFO[0000] [hostagent] Received SIGINT, shutting down the host agent 
INFO[0000] [hostagent] Shutting down the host agent     
INFO[0000] [hostagent] Unmounting "/Users/xxxxx/projects/hoge" 
INFO[0000] [hostagent] Shutting down QEMU with ACPI     
WARN[0000] [hostagent] connection to the guest agent was closed unexpectedly 
INFO[0000] [hostagent] Sending QMP system_powerdown command 
INFO[0002] [hostagent] QEMU has exited                  
$
$ limactl delete lima-hoge
INFO[0000] The QEMU process seems already stopped       
INFO[0000] The host agent process seems already stopped 
INFO[0000] Removing *.pid *.sock under "/Users/xxxxx/.lima/lima-hoge" 
INFO[0000] Removing "/Users/xxxxx/.lima/lima-hoge/ga.sock" 
INFO[0000] Deleted "lima-hoge" ("/Users/xxxxx/.lima/lima-hoge")
$
$ limactl ls
WARN[0000] No instance found. Run `limactl start` to create an instance. 
NAME    STATUS    SSH    ARCH    DIR
$

Discussion