# attempt to unobtrusively create an ssh tunnel to a lima vm's docker socket # and set DOCKER_HOST to use it if pasting into your ~/.bashrc or ~/.zshrc # remember to call the fn by putting `lima-connect-docker` somewhere below # where this fn is pasted in! lima-connect-docker() { [[ "$(uname)" -ne "Darwin" ]] && return 0 local lima_socket_path="/tmp/lima-docker.sock" lima-check-docker-socket-up() { local DOCKER_HOST export DOCKER_HOST="unix://$lima_socket_path" ps ax | grep ssh | grep lima | grep -q "$lima_socket_path" && docker ps &> /dev/null } lima-check-up() { ps ax | grep limactl | grep -q docker } lima-start-socket-tunnel() { ssh -f -N -p 60006 -i ~/.lima/_config/user \ -o NoHostAuthenticationForLocalhost=yes \ -L "$lima_socket_path:/var/run/docker.sock" \ 127.0.0.1 } if ! lima-check-docker-socket-up; then ps ax | grep ssh | grep lima | grep docker.sock | awk '{print $1}' | xargs kill rm "$lima_socket_path" lima-start-socket-tunnel fi if lima-check-up && lima-check-docker-socket-up && [[ -r ~/docker.sock ]]; then export DOCKER_HOST="unix://$lima_socket_path" fi # clean up unset -f lima-check-docker-socket-up unset -f lima-check-up unset -f lima-start-socket-tunnel }