LoginSignup
1
3

More than 1 year has passed since last update.

macOSでlima+dockerの環境構築

Last updated at Posted at 2022-04-23

背景

私用のPCはmac(macbook air 2013 mid 古すぎ!!)。Docker Desktop for Macでは動作が遅かったので、しばらくDockerの利用はあきらめていた。
ファイルシステムが違うとか、そんな理由らしい・・・
最近になって、Limaを使用すると快適に作業できるとかいう記事を見かけたので環境構築してみた。ほとんど参考記事そのまま。

参考記事

環境

  • Intel Mac (Macbook Air 2013 mid)
  • limactl 0.9.1
  • Docker 20.10.14
  • Docker Compose 2.4.1

Lima、Dockerのインストール

これだけ。

brew install lima docker

Limaの設定ファイル

LimaはmacOS上でLinux仮想マシンを提供するツールらしい。似たようなものにvagrantもある。ちょっと違いのかもしれないが、WindowsでいうところのWSLみたいなものか。仮想マシンに対する設定ファイルをlima.yml作成する必要がある。docker用のサンプルも用意されている。とりあえず上記参考記事をほぼコピペで以下のようになった。

lima.yml
arch: "default"
images:
  - 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"
#仮想マシンに振り分けるリソース、貧弱・・・
cpus: 1
memory: "1GiB"
disk: "10GiB"

mounts:
  - location: "~"
    # CAUTION: `writable` SHOULD be false for the home directory.
    # Setting `writable` to true is possible, but untested and dangerous.
    writable: false
  # 作業ディレクトリを指定(ホスト側の作業場所)
  - location: "~/Documents/workspaces"
    writable: true
ssh:
  localPort: 60006
  loadDotSSHPubKeys: true
containerd:
  # Enable system-wide (aka rootful)  containerd and its dependencies (BuildKit, Stargz Snapshotter)
  # Default: false
  system: false
  # Enable user-scoped (aka rootless) containerd and its dependencies
  # Default: true
  user: true
provision:
  # `system` is executed with the root privilege
  - mode: system
    script: |
      #!/bin/sh
      sed -i 's/host.lima.internal.*/host.lima.internal host.docker.internal/' /etc/hosts
  - 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
  # `user` is executed without the root privilege
  - 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 ...

Lima環境構築

仮想マシンの起動

Limaはlimactlとういコマンドで扱うらしい。lima.ymlがあるディレクトリで下記コマンドを実行する。

limactl start ./lima.yml

参考記事にもあるが、lima.ymlを変更した場合は仮想環境を再作成する必要がある。なお、仮想マシン名はymlファイル名となる。今回の場合、「lima」が仮想マシン名となる。

仮想マシンの停止

limactl stop lima

仮想マシンの削除

limactl rm lima

lima.yml編集後に再度、limactl start ./lima.ymlで環境設定ファイルが反映されようだ。

確認

limactl list

以下のような感じで起動している仮想マシンの状態がわかる。

NAME    STATUS     SSH                ARCH      CPUS    MEMORY    DISK     DIR
lima    Running    127.0.0.1:60006    x86_64    1       1GiB      10GiB    /Users/***/.lima/lima

仮想環境に入る場合

nyaao@MBA ~ % limactl shell lima
nyaao@lima-lima:/Users/nyaao$ 

ホストマシンから仮想環境のdockerを使う

ホストマシンにはDockerをインストールしていますが、このままだとlima上のdockerデーモンではなく、ホストマシン上のdockerデーモンを使用することになります。
DOCKER_HOSTの環境変数を上書きすることで、limaのシェルに入ることなく、ターミナルなどからlima上のdockerデーモンを操作できるようになります。
.zshrcに下記を追記しておけば良いようです。

export DOCKER_HOST=unix:///${HOME}/.lima/lima/sock/docker.sock

パソコン再起動時の対応

パソコンを再起動すると仮想環境はstop状態になるようです。.zshrcに下記を追記しておくと再起動時に仮想環境が自動起動します。

limactl start lima

あとがき

まだ環境構築したばかりなので、快適に動作するかわかりませんが、とりあえずできました。
Macbook Airが古すぎるので効果は感じられないかも、M1 Macほしいな・・・
M1を購入して1ヶ月ほどlima環境でdockerを使用しています。
今のところ、全く問題ないです。

追記

以前にDocker Desktopを使用しているとdocker-composeでのpull時に以下のエラーが出るっぽい。
error getting credentials - err: exec: "docker-credential-desktop": executable file not found in $PATH,

こちらによるとrm -fr ~/.dockerをしておくと良いみたい。

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3