2022년 4월 13일 수요일

Lima + docker server 구성

 

Lima + docker server 구성

Lima를 사용해서 Ubuntu 가상머신을 만듭니다. 그 위에 docker server를 구성합니다. 

설치

brew install lima

Lima VM을 만들기

Lima 설정을 default.yaml 이라고 두고 가상머신을 만듭니다. 

# 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=unix://$HOME/docker.sock
# $ docker ...
cpus: 1
memory: "8GiB"
disk: "80GiB"
arch: x86_64

# This example requires Lima v0.7.3 or later
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:
  - location: "~"
    writable: true
  - location: "~/work"
    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
  - 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: "{{.Home}}/docker.sock"

중요한 설정 2가지

  • mounts에서 location 부분이 docker volume으로 마운트되는 폴더를 지정하는 곳이고, 읽고/쓰기 설정을 바꿀 수 있습니다. 
  • portForwards 가 docker.sock 으로 호스트 머신에서 노출되는 부분입니다. 
    • Mac 호스트에서 Lima VM에 docker.sock으로 직접 연결되지요. 

Lima 가상머신 만들기 

limactl start ./default.yaml

docker 동작을 확인

lima docker version

macOS에 docker cli를 설치

mac에 docker 명령어를 사용하기 위해서 docker cli를 설치합니다. 

# docker cli
# for Intel Mac
curl -OL https://download.docker.com/mac/static/stable/x86_64/docker-20.10.14.tgz

# for M1 Mac
curl -OL https://download.docker.com/mac/static/stable/aarch64/docker-20.10.14.tgz

mkdir -p $HOME/bin
tar xzvf docker-20.10.11.tgz
mv docker/docker $HOME/bin/.
chmod 755 $HOME/bin/docker

Lima VM의 docker와 Mac의 docker cli를 연결

~/.zprofile 에 소켓을 설정합니다. 

export DOCKER_HOST=unix://$HOME/docker.sock
source $HOME/.zprofile

mac에서 가상머신에 docker를 사용할 수 있습니다. docker version

Docker Server가 동작하는지 확인할 수 있습니다. 

lima uname -m

댓글 없음:

댓글 쓰기