0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

LimaとDocker

Last updated at Posted at 2024-06-01

はじめに

最近、働く環境が変わったのですが、そこである課題にぶつかりました。
なんとDocker Desktopが使えないという...。Docker Desktopを無料で使用するには

個人利用もしくはスモールビジネス(従業員数250人未満かつ年間売上高1000万ドル未満(訳注:1ドル150円換算で15億円)、教育機関、非商用のオープンソースプロジェクトでは無料で利用できる
https://www.docker.com/blog/updating-product-subscriptions/

という条件があるそうです。つまりそれに当てはまらない企業だと有料みたいです。知りませんでした。そこで、「そこをケチるか」と思いつつも、Dockerを無料で使いたいということで、弊社ではLimaを利用し、その上にDockerを立ち上げる方法を採用していました。本記事では、その設定方法や運用について簡単にまとめていきます。

Limaとは

簡単に説明するとmacOS上で仮想マシンを実行できるツールです。Limaを使用して簡単にmadOSユーザーはLinux環境を構築でき、その上でDockerなどのコンテナを動かすことができます。

Limaのセットアップ

まずhomebrewからlimaをinstallします

brew install lima

次にYAMLファイルを作成していくのですがDockerの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 ...

# 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: false
  - location: "/tmp/lima"
    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"

https://github.com/lima-vm/lima/blob/v0.7.4/examples/docker.yaml

YAMLファイルを作成したら、次に仮想マシンを起動します

limactl start ./docker.yaml

YAMLファイル名がそのままインスタンス名になるので注意して下さい。
起動が完了したら、以下のコマンドで仮想マシンに入ることができます。

limactl shell docker

で仮想マシンに入ることができます。
ホストマシンから直接Dockerコマンドを実行できるように

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

を設定しておきます。

最後に

Docker Desktopしか使ったことがなかった自分にとってLimaは初め少し大変でした。
シンプルに知識不足でわからないことや、rootless Dockerの権限エラーが頻発したりしました(今もしている)。
しかし確かにコストを抑えて柔軟に開発していけるなという実感もだんだんと湧いてきて良い経験でした。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?