Closed10

M1 Mac(Ventura) + lima + docker + docker compose

ut61zut61z

docker をinstall

docker/cli@v26.0.2 をaquaでインストール

ut61zut61z

VMの設定ファイルを作成する
docker用のtemplateをちょっと修正して作成
もとにしたファイル
https://github.com/lima-vm/lima/blob/master/examples/docker.yaml

修正したファイル

docker-vz.yaml
# A template 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 template 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-20240308/ubuntu-22.04-server-cloudimg-amd64.img"
  arch: "x86_64"
  digest: "sha256:42dcf9757e75c3275486b397a752fb535c7cd8e5232ee5ee349554b7a55f1702"
- location: "https://cloud-images.ubuntu.com/releases/22.04/release-20240308/ubuntu-22.04-server-cloudimg-arm64.img"
  arch: "aarch64"
  digest: "sha256:0f5f68b9b74686b8a847024364031e2b95e4d3855e5177a99b33d7c55e45907f"
# 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"

vmType: "vz"
networks:
  - vzNAT: true
mountType: "virtiofs"
mounts:
- location: "~"
- location: "/tmp/lima"
  writable: true
- location: "~/xxx/yyy"
  writable: 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
    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
  ------

変更した差分は

+vmType: "vz"
+networks:
+  - vzNAT: true
+mountType: "virtiofs"
...

+mounts:
...
+- location: "~/xxx/yyy"
+  writable: true
ut61zut61z

作成したファイルを使って limactl create する

 limactl create docker-vz.yaml

選択肢が表示されるが Proceed with the current configuration を選択する

limactl create docker-vz.yaml
? Creating an instance "docker-vz"  [Use arrows to move, type to filter]
> Proceed with the current configuration
  Open an editor to review or modify the current configuration
  Choose another template (docker, podman, archlinux, fedora, ...)
  Exit
ut61zut61z
limactl start docker-vz

を実行する

結構時間がかかる

limactl list でSTATUSが Running になっていればVMの起動に成功している

limactl list
NAME         STATUS     SSH                VMTYPE    ARCH       CPUS    MEMORY    DISK      DIR
docker-vz    Running    127.0.0.1:57886    vz        aarch64    4       4GiB      100GiB    ~/.lima/docker-vz
ut61zut61z

ホストからゲスト(VM)のDocker Engineに接続するための設定

docker context create lima-docker-vz --docker "host=unix:///Your/home_directory/.lima/docker-vz/sock/docker.sock"
docker context use lima-docker-vz
ut61zut61z

準備OK
dockerをrunしてみる

docker run --rm hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
478afc919002: Pull complete 
Digest: sha256:a26bff933ddc26d5cdf7faa98b4ae1e3ec20c4985e6f87ac0973052224d24302
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (arm64v8)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

🎉🎉🎉

ut61zut61z

docker compose も使えるように設定

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

v2.26.1 はそのときの最新のバージョンを指定する
https://github.com/docker/compose
(バージョンアップをどう管理するかは考えないとな)

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

で実行権限を追加

docker compose version
Docker Compose version v2.26.1

🎉🎉🎉

このスクラップは2024/05/02にクローズされました