originally posted here: https://twitter.com/ArghZero/status/1455376756530036736
finally found a good nag-free alternative to Docker Desktop for m1 macs! (spoiler: it's lima https://github.com/lima-vm/lima).
everything i want out of mac docker: runs containers with the standard docker cli, docker-compose works as expected, and port forwards and shared vols do too!
getting it to work takes a bit of tinkering, here's how to do it:
install lima (brew install lima
), then set up a config. lima uses cloud-init yamls to configure its vms. there's a docker example, but it uses rootless and sets shares readonly, which caused issues for me.
this modified version of their example fixes those issues: https://gist.github.com/nikvdp/78e8c6746b22e6145df03d3085b13ead#file-docker-yaml
download it and save it somewhere, then have lima fire up qemu:
limactl start ./docker.yaml
this may take a while on the first run because it needs to download a vm image and install docker into it. but once it's done you'll have a docker-capable (aarch64) linux vm running on your mac!
now we just need to configure it...
the first step is to add your user on the vm to the linux group and reboot the vm:
limactl shell docker sudo gpasswd -a $(whoami) docker
limactl stop docker
limactl start docker
once the vm is back up, you can shell in and make sure docker works from inside the vm. an easy way to test is to run this:
limactl shell docker docker ps
if you don't get any error msgs docker works on the linux side.
now we just need to tell macOS docker tools how to use it
docker is based on a client-server architecture, so what we're actually doing here is telling the mac docker client how to reach the docker server running in our new linux vm. docker normally uses unix sockets to communicate. luckily macOS supports these, and ssh can tunnel them!
this monster of a cmd creates a ~/docker.sock "file" on your mac that tunnels cmds issued to ~/docker.sock to the real docker.sock in the vm:
ssh -f -N -p 60006 -i ~/.lima/_config/user -o NoHostAuthenticationForLocalhost=yes -L "$HOME/docker.sock:/var/run/docker.sock" 127.0.0.1
once that's in place, we just need to tell macos docker commands to use it. this requires setting the DOCKER_HOST
env var like so:
export DOCKER_HOST="unix:///$HOME/docker.sock"
with that done you should be able to run docker ps
on your mac and get the usual output.
so at this point you technically have docker working on your mac, but it's kinda of a pain to use since you have to run that monstrosity of an ssh command and the export DOCKER_HOST
command from each term where you want to use docker.
next step, make it convenient! 👇
you can drop this (hacktastic) fn into your ~/.zshrc or ~/.bashrc and call it. it will bring up the ssh tunnel if needed and set DOCKER_HOST correctly, yielding something pretty close to the old docker desktop for mac experience, minus the update prompts! https://gist.github.com/nikvdp/78e8c6746b22e6145df03d3085b13ead#file-lima-connect-sh
one caveat: if you use any GUI-based docker tools they probably won't automatically pick up the DOCKER_HOST variable. you can often still get them to work by doing echo $DOCKER_HOST | pbcopy
and pasting the output into the app's cfg tho!
happy nag-free dockering!