Skip to content

Instantly share code, notes, and snippets.

@leoh0
Last active February 7, 2024 02:20
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leoh0/3b3c908f0e931d7c620d273c77ea3757 to your computer and use it in GitHub Desktop.
Save leoh0/3b3c908f0e931d7c620d273c77ea3757 to your computer and use it in GitHub Desktop.
mac 에서 docker-desktop 을 lima로 대체하기 lima-docker.sh 실행

mac 에서 docker-desktop을 대체하기

https://gist.github.com/leoh0/3b3c908f0e931d7c620d273c77ea3757

docker-desktop 를 2022년 1월 31일까지 대체해야 하기에 다들 여러 방법을 사용하실 것 같은데 windows에서는 사실 wsl2 를 사용하면서 docker 를 그대로 깔아서 쓸 수 있다는 것 때문에 큰 제약없이(eg. wsl이 결국 브릿지를 거치기 때문에 vpn등 사용시 mtu 조절이 필요할 수 있는 제약이 있음)사용 가능한데 맥은 사실 linux 스택을 그대로 사용할 수 없기때문에 vm을 올려야 하는 상태입니다.

불편한 점

하지만 이에 따라 여러가지 불편한 점들이 있습니다.

공통 문제

공통적으로는 vm 사용시 mac에서 vpn을 사용하면 nat 처리나 mtu 사이즈 조절을 신경써야 될 수도 있습니다. https://gist.github.com/leoh0/ef5de7405be1421c0baab63038679967

docker-machine 이용시

https://medium.com/crowdbotics/a-complete-one-by-one-guide-to-install-docker-on-your-mac-os-using-homebrew-e818eb4cfc3

docker-desktop 이전 세대에 사용했던 docker-machine 등을 이용할 수 있는데 docker-machine 같은경우 virtualbox를 하이퍼바이져로 사용하는데에는 큰 문제가 적을 수도 있지만 이 외의 타 하이퍼바이저는 여러가지 제약이 있습니다. hyperkit 같은 경우 동작하지 않는다던지 아니면 disk share 등에 제약이 있습니다. 하지만 일반적으로 virtualbox 보다 xhyve(hyperkit) 나 hvf 가 지원되는 qemu 를 사용하는 것이 더욱 효율적이기에 선택이 꺼려질 수 있습니다. 또한 docker-machine 자체의 코드가 오래되고 업데이트되지 않아서 예전 boot2docker를 사용해서 19.03 버전 이후를 사용하지 못한다던지 등의 문제들이 있습니다.

minikube 이용시

https://arnon.me/2021/09/replace-docker-with-minikube/

minikube의 노드 생성 코드는 docker-machine이 더이상 관리되지 않음에 따라 이것을 카피하여 계속 유지보수 하였기 때문에 비교적 좋게 잘 구동됩니다. hypervisor 나 docker version 문제에 대해서는 비교적 자유롭다고 할 수 있습니다. 다만 minikube를 사용시 쿠버네티스 컴포넌트들이 추가적으로 깔리고 이것을 시작하고 제거한다던지 조금 번거로움들이 남아 있다라고 할 수 있고, 엄연히 container host가 메인이라기 보다 k8s 사용성에 초점이 맞춰져 있기때문에 불편함들이 있습니다. 또한 mount 가 별개로 관리해야하는 등 좀 불편한 점들이 있습니다.

결론

이런 문제들때문에 현재 간단하고 좀 편한 방법을 찾는도중 containerd for Mac 으로 유명한 lima로 docker 버전을 테스트 했을때 기존의 대부분의 동작들이 거의 완벽하게 호환되는 것을 확인했습니다. 그리고 9p virtfs 나 vsock 같은 문제들이 qemu에서 해결되면 mac에서 여러 성능과 사용성이 더 개선될 것으로 보입니다. 그래서 앞서 링크한 lima와 docker를 사용해보시고 더 좋은 방법이 있으면 공유해주세요 :) 긴 글 읽어주셔서 감사합니다.

# Example 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 ...
# Hint: To allow `docker` CLI on the host to connect to the Docker daemon running inside the guest,
# add `NoHostAuthenticationForLocalhost yes` in ~/.ssh/config , and then run the following commands:
# $ export DOCKER_HOST=ssh://localhost:60006
# $ docker ...
# If ssh:// ... does not work, try the following commands:
# $ ssh -f -N -p 60006 -i ~/.lima/_config/user -o NoHostAuthenticationForLocalhost=yes -L $HOME/docker.sock:/run/user/$(id -u)/docker.sock 127.0.0.1
# $ export DOCKER_HOST=unix://$HOME/docker.sock
# $ docker ...
images:
# Hint: run `limactl prune` to invalidate the "current" cache
- location: "https://cloud-images.ubuntu.com/impish/current/impish-server-cloudimg-amd64.img"
arch: "x86_64"
- location: "https://cloud-images.ubuntu.com/impish/current/impish-server-cloudimg-arm64.img"
arch: "aarch64"
mounts:
# Export files from container
- location: "~"
writable: true
- location: "/tmp/lima"
writable: true
- location: "/var/folders"
writable: true
ssh:
localPort: 60006
# Load ~/.ssh/*.pub in addition to $LIMA_HOME/_config/user.pub , for allowing DOCKER_HOST=ssh:// .
# This option is enabled by default.
# If you have an insecure key under ~/.ssh, do not use this option.
loadDotSSHPubKeys: 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
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
VER=$(curl -s https://api.github.com/repos/docker/compose/releases/latest | awk -F'"' '/"name": "v/{print $4}')
curl -L "https://github.com/docker/compose/releases/download/${VER}/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
- 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
portForwards:
- guestSocket: "/run/user/{{.UID}}/docker.sock"
hostSocket: "{{.Dir}}/sock/docker.sock"
message: |
To run `docker` on the host (assumes docker-cli is installed):
$ export DOCKER_HOST=unix://{{.Dir}}/sock/docker.sock
$ docker ...
#!/usr/bin/env bash
# 시작하기전 가급적 docker를 삭제(아래 /usr/local/bin/docker 가 겹침)
# brew uninstall docker
# brew uninstall docker-compose
# !!주의!! m1 에서는 필수 (intel mac은 10.10 이전이여서 hvf가 안될시에도 tcg(emulation)으로도 이용가능)
# 만약 기존 qemu가 있으면 삭제 후 hvf 가 패치된 qemu 설치
# brew uninstall --ignore-dependencies qemu
# brew install simnalamburt/x/qemu-hvf
# lima 설치
brew install lima
# lima 에서 docker host로 사용할 머신 yaml 생성
# 만약 container 내에서 빌드한 데이터를 export 하려면 ~를 writable로 마운트 하는게 좋음 하지만 위험이 있음
# 이 스크립트에서는 docker-compose 도 추가로 설치함
curl -o ./docker.yaml https://gist.githubusercontent.com/leoh0/3b3c908f0e931d7c620d273c77ea3757/raw/80b16e4f93c7712b0ec596f55985ac51e0f3a083/docker.yaml
# 이것이 싫으면 기본 lima에서 제공하는 yaml 사용
# curl -o ./docker.yaml https://raw.githubusercontent.com/lima-vm/lima/62159c9be1d8f00b3d7d7879ac87dec0d4ed65a7/examples/docker.yaml
# docker host를 위한 docker machine 생성
limactl start --tty=false ./docker.yaml
# docker 커맨드 생성
cat << "EOF" > /usr/local/bin/docker
#!/usr/bin/env bash
exec limactl shell docker docker "$@"
EOF
chmod +x /usr/local/bin/docker
# docker 커맨드 생성
cat << "EOF" > /usr/local/bin/docker-compose
#!/usr/bin/env bash
exec limactl shell docker docker-compose "$@"
EOF
chmod +x /usr/local/bin/docker-compose
# test
docker ps
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment