Docker on Lima (lima-vm/lima) Setup Guide
In recent months, Lima has become de facto standard software as a free and open source alternative to Docker Desktop for Mac. Several projects (such as Colima, Rancher Desktop, or Finch) for similar purpose also uses Lima as their backend. These projects will make it easier to set up Lima. But some people may not want to introduce extra components other than Lima. This guide will show you how to set up Lima to be alternative to Docker Desktop for Mac, while using Lima only.
🪣 Note. It is a good practice to uninstall Docker Desktop for Mac before proceeding with the following steps.
You can easily install Lima using Homebrew:
$ brew install lima
$ lima -v
limactl version 0.13.0
Also, you need to install Docker CLI:
$ brew install docker
$ docker -v
Docker version 20.10.21, build baeda1f82a
- Download VM template file:
$ curl -O https://raw.githubusercontent.com/lima-vm/lima/master/examples/docker.yaml
- Enable CPU emulation: Modify the template file as follows:
This provisioning script will enable CPU emulation for Docker engine (i.e. whether you are using Intel Mac or Apple Silicon M1 Mac, you can run both
# 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: system + script: | + #!/bin/bash + set -eux -o pipefail + docker run --privileged --rm tonistiigi/binfmt --install all - mode: user script: | #!/bin/bash
linux/amd64
andlinux/arm64
images). - Modify the
mounts
property to make your workspace directory writable. For example:mounts: -- location: "~" -- location: "/tmp/lima" - writable: true +- location" "~/mycode" + writable: true
- Also, modify some other property if you need. The available options for VM template are described in default.yaml.
- cf. my template file is here.
- Run the following command to launch your VM:
$ limactl start ./docker.yaml
- After the VM is successfully started, run the following command to verify that your docker engine is working:
$ DOCKER_HOST=$(limactl list docker -f 'unix://{{.Dir}}/sock/docker.sock') docker info ... Server: ... Operating System: Ubuntu 22.04.1 LTS ...
By default, the unix socket to the Docker Engine running on the Lima VM is deployed under the ~/.lima
directory:
$ limactl list docker -f 'unix://{{.Dir}}/sock/docker.sock'
unix:///Users/yourname/.lima/docker/sock/docker.sock
To access the Docker Engine via this socket from the Docker CLI, the following methods are possible:
- Using the environment variable
DOCKER_HOST
:- This method has already been shown in the step 2.6.
- Using the Docker Context:
- This method is described in the message from docker.yaml.
- Create a symbolic link
/var/run/docker.sock
:- The path
/var/run/docker.sock
is the location of the unix socket referenced by the Docker CLI by default (i.e. for thedefault
context).
- The path
When you configure them correctly, they all work equivalently. However, unfortunately some third-party Docker client does not support the method 1 and 2. So I recommend you to use method 3.
- Save the following contents as
/Library/LaunchDaemons/local.lima-docker.socket-symlink.plist
:<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Label</key> <string>local.lima-docker.socket-symlink</string> <key>ProgramArguments</key> <array> <string>/bin/zsh</string> <string>-c</string> <!-- Note. please change the part of 'your-name' --> <string>/bin/ln -s -f /Users/your-name/.lima/docker/sock/docker.sock /var/run/docker.sock</string> </array> <key>RunAtLoad</key> <true/> <key>KeepAlive</key> <false/> <key>AbandonProcessGroup</key> <true/> </dict> </plist>
- Then, load (enable) it:
sudo launchctl load /Library/LaunchDaemons/local.lima-docker.socket-symlink.plist
- Verify that the symlink
/var/run/docker.sock
exists and its destination is the unix socket to the Docker Engine running on the Lima VM.$ ls -l /var/run/docker.sock lrwxr-xr-x root daemon 46 B Sun Dec 11 13:56:46 2022 /var/run/docker.sock ⇒ /Users/your-name/.lima/docker/sock/docker.sock
✅ Note. The above step install plist to create the symlink /var/run/docker.sock
. This plist (namely, the ln
command) is executed automatically at each Mac startup. To uninstall the plist, run the following command:
$ sudo launchctl unload /Library/LaunchDaemons/local.lima-docker.socket-symlink.plist
$ sudo rm /Library/LaunchDaemons/local.lima-docker.socket-symlink.plist
For more detail, see launchd.
By default, you need to start your Lima VM manually when your Mac boots.
The following steps will configure the Lima VM named docker
to start automatically at each Mac startup.
- Save the following contents as
~/Library/LaunchAgents/local.lima-docker.start-on-boot.plist
:<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Label</key> <string>local.lima-docker.start-on-boot</string> <key>ProgramArguments</key> <array> <string>/bin/zsh</string> <string>-lic</string> <string>( limactl stop -f docker; sleep 5; limactl start docker ) > ~/Library/LaunchAgents/local.lima-docker.start-on-boot.plist.log 2>&1</string> </array> <key>RunAtLoad</key> <true/> <key>KeepAlive</key> <false/> <key>AbandonProcessGroup</key> <true/> </dict> </plist>
- Then, load (enable) it:
$ launchctl load ~/Library/LaunchAgents/local.lima-docker.start-on-boot.plist
Reboot your Mac, then verify your setup.
- Verify that the Lima VM named
docker
is automatically started.$ limactl list NAME STATUS SSH ARCH CPUS MEMORY DISK DIR docker Running 127.0.0.1:49185 aarch64 8 16GiB 100GiB /Users/your-name/.lima/docker
- Verify that the symlink
/var/run/docker.sock
exists.$ ls -l /var/run/docker.sock lrwxr-xr-x root daemon 46 B Sun Dec 11 13:56:46 2022 /var/run/docker.sock ⇒ /Users/your-name/.lima/docker/sock/docker.sock
- Verify that the
docker
command can access to the Docker engine running on the Lima VM.$ docker context ls NAME DESCRIPTION DOCKER ENDPOINT KUBERNETES ENDPOINT ORCHESTRATOR default * Current DOCKER_HOST based configuration unix:///var/run/docker.sock swarm $ docker info ... Server: ... Operating System: Ubuntu 22.04.1 LTS ... $ docker run --rm hello-world ... 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (arm64v8) ... $ docker run --rm --platform linux/amd64 hello-world ... 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (amd64) ...
Congratulations! All the basic setup is now done 😎
You may want to install some additional components:
- Compose
At the moment, thedocker compose
command is provided as a Docker CLI plugin. You can install it as follows:$ mkdir -p ~/.docker/cli-plugins $ curl -fL -o ~/.docker/cli-plugins/docker-compose https://github.com/docker/compose/releases/latest/download/docker-compose-darwin-aarch64 $ chmod +x ~/.docker/cli-plugins/docker-compose $ docker compose version Docker Compose version v2.14.0
- Buildx
At the moment, thedocker buildx
command is provided as a Docker CLI plugin. You can install it as follows:$ mkdir -p ~/.docker/cli-plugins $ curl -fL -o ~/.docker/cli-plugins/docker-buildx https://github.com/docker/buildx/releases/download/v0.9.1/buildx-v0.9.1.darwin-$(uname -m) $ chmod +x ~/.docker/cli-plugins/docker-buildx $ docker buildx version github.com/docker/buildx v0.9.1 ed00243a0ce2a0aee75311b06e32d33b44729689
Great guide.Many thanks for it!