# A template to use Docker (rootful) instead of containerd & nerdctl
# $ limactl start ./docker-rootful.yaml
# $ limactl shell docker-roootful 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-rootful --format 'unix://{{.Dir}}/sock/docker.sock')
# $ docker ...

minimumLimaVersion: 2.0.0

base:
- template:_images/ubuntu-lts
- template:_default/mounts

# 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
- mode: yq
  path: /etc/systemd/system/docker.socket.d/override.conf
  format: ini
  expression: .Socket.SocketUser="{{.User}}"
- mode: yq
  path: "/etc/docker/daemon.json"
  expression: |
    .features.cdi = true |
    .features.containerd-snapshotter = {{.Param.containerdSnapshotter}}
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
  ------
  {{- if .Instance.Config.VMOpts.vz.rosetta.enabled}}
  Rosetta is enabled in this VM, so you can run x86_64 containers on Apple Silicon.
  You can use Rosetta AOT Caching with the CDI spec:
  - To run a container, add `--device=lima-vm.io/rosetta=cached` to your `docker run` command:
    ------
    docker run --platform=linux/amd64 --device=lima-vm.io/rosetta=cached ...
    ------
  - To build an image, add `# syntax=docker/dockerfile:1-labs` at the top of your Dockerfile,
    and use `--device=lima-vm.io/rosetta=cached` in the `RUN` command:
    ------
    # syntax=docker/dockerfile:1-labs
    FROM ...
    ...
    RUN --device=lima-vm.io/rosetta=cached <your amd64 command>
    ------
  See: https://lima-vm.io/docs/config/multi-arch/#rosetta-aot-caching
  {{- end}}
param:
  containerdSnapshotter: true
