Mac使用Colima运行Docker

Container runtimes on macOS (and Linux) with minimal setup

Colima是一个免费的开源容器运行时,它使用QEMU在虚拟机中运行Docker容器。它是由Lima Project创建的,Lima项目是一群致力于创建工具以方便在 macOS上运行容器化应用程序的开发人员。

Lima (Linux virtual machines (on macOS, in most cases)) 项目由一群MacBook开发人员用户于2019年启动,出于对macOS缺乏良好的容器运行时和工具的不满而DIY。当时,在 macOS 上运行Docker容器的唯一选择是适用于macOS的Docker Desktop,它需要大中型公司的许可证。Lima项目着手为macOS创建 Docker Desktop的免费开源替代方案,以提供更好的性能和更多功能。

优势

  • 免费和开源:一个免费和开源项目,这意味着它可以免费使用和修改。
  • 高性能:C使用QEMU在虚拟机中运行Docker容器,可以提供比其他容器运行时更好的性能。
  • 支持多容器运行时:支持Docker和Containerd作为运行时。
  • 支持Kubernetes: Colima支持Kubernetes,这是管理容器化应用程序的强大工具。
  • 易于使用:简单的CLI界面,易于安装和使用。
  • 兼容所有版本macOS: Colima与所有版本的macOS兼容。M1 Mac架构支持。

Colima由以下组件组成:

  • Colima :主要的Colima组件负责启动和管理QEMU虚拟机。

  • Lima:Lima是一个允许您从命令行管理 QEMU 虚拟机的工具。

  • Docker:Colima使用Docker来运行容器化应用程序。

  • Kubernetes:Colima 支持Kubernetes,因此可以使用它来运行由Kubernetes管理的容器化应用程序。

Installation

1
2
# Homebrew
brew install colima

安装Docker

1
brew install docker

启动

  1. 查看docker容器
1
2
docker ps
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

这是因为没有启动colima, 启动colima就可以。

  1. 启动colima服务
1
2
3
4
5
6
7
colima start
INFO[0000] starting colima
INFO[0000] runtime: docker
INFO[0000] starting ...                                  context=vm
INFO[0024] provisioning ...                              context=docker
INFO[0024] starting ...                                  context=docker
INFO[0027] done

Colima 会使用 QEMU 虚拟机;

  1. 启动后再查看
1
2
docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

启动成功

启动时指定参数

  • create VM with 1CPU, 2GiB memory and 10GiB storage.
1
2
3
4
5
colima start --cpu 1 --memory 2 --disk 10

colima list
PROFILE    STATUS     ARCH      CPUS    MEMORY    DISK     RUNTIME    ADDRESS
default    Running    x86_64    1       2GiB      10GiB    docker
  • create VM with Rosetta 2 emulation. Requires v0.5.3 and MacOS >= 13 (Ventura)
1
colima start --arch aarch64 --vm-type=vz --vz-rosetta
  • mounting volumes
1
colima start --mount $HOME/project:/project:w

使用配置启动

启动时修改配置

1
colima start --edit

启动成功后,使用docker运行一个Portainer面板:

1
docker run -d -p 18000:8000 -p 19443:9443 -p 19000:9000 --name portainer --restart=always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer-ce:latest

starts and setup Containerd

1
colima start --runtime containerd

starts and setup Kubernetes

1
2
3
brew install kubectl

colima start --kubernetes

指定名字为helm-test,使用kubernetes运行时,并指定资源配置

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
colima start helm-test --kubernetes --cpu 2 --memory 4 --disk 10
INFO[0000] stopping colima [profile=helm-test]
WARN[0000] error retrieving runtimes: error retrieving current runtime: empty value
INFO[0000] stopping ...                                  context=vm
INFO[0001] done
INFO[0004] starting colima [profile=helm-test]
INFO[0004] runtime: docker+k3s
INFO[0005] starting ...                                  context=vm
INFO[0029] provisioning ...                              context=docker
INFO[0030] starting ...                                  context=docker
INFO[0031] provisioning ...                              context=kubernetes
INFO[0047] starting ...                                  context=kubernetes
INFO[0058] updating config ...                           context=kubernetes
INFO[0059] Switched to context "colima-helm-test".       context=kubernetes
INFO[0060] done

重复启动会失败忽略

1
2
colima start helm-test --kubernetes -m2
WARN[0000] already running, ignoring

查看当前启动的服务列表

1
2
3
4
colima list
PROFILE      STATUS     ARCH      CPUS    MEMORY    DISK     RUNTIME       ADDRESS
default      Running    x86_64    2       4GiB      10GiB    docker
helm-test    Running    x86_64    2       4GiB      10GiB    docker+k3s

使用kubectl查看kubernetes服务

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
kubectl get svc
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.43.0.1    <none>        443/TCP   2m10s

kubectl get ns
NAME              STATUS   AGE
kube-system       Active   3m2s
kube-public       Active   3m2s
kube-node-lease   Active   3m2s
default           Active   3m2s

查看kubernetes运行时启动后的镜像和容器

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
docker ps -a
CONTAINER ID   IMAGE                              COMMAND                   CREATED         STATUS         PORTS     NAMES
785c59542d32   rancher/mirrored-metrics-server    "/metrics-server --c…"   2 minutes ago   Up 2 minutes             k8s_metrics-server_metrics-server-67c658944b-kqjgk_kube-system_86610a87-3816-4da6-9a68-7e49e8588a7c_0
4c4551704157   rancher/mirrored-coredns-coredns   "/coredns -conf /etc…"   2 minutes ago   Up 2 minutes             k8s_coredns_coredns-6799fbcd5-pp8sr_kube-system_ed8eaa59-eaa6-4dd4-a3be-1cae57bd5fd0_0
01f1e74d85fb   rancher/local-path-provisioner     "local-path-provisio…"   2 minutes ago   Up 2 minutes             k8s_local-path-provisioner_local-path-provisioner-84db5d44d9-7qlrp_kube-system_1906322a-f01a-4174-a728-c622d958b212_0
e71408df6a74   rancher/mirrored-pause:3.6         "/pause"                  3 minutes ago   Up 3 minutes             k8s_POD_metrics-server-67c658944b-kqjgk_kube-system_86610a87-3816-4da6-9a68-7e49e8588a7c_0
6a3c855075bf   rancher/mirrored-pause:3.6         "/pause"                  3 minutes ago   Up 3 minutes             k8s_POD_local-path-provisioner-84db5d44d9-7qlrp_kube-system_1906322a-f01a-4174-a728-c622d958b212_0
32a27eaf5489   rancher/mirrored-pause:3.6         "/pause"                  3 minutes ago   Up 3 minutes             k8s_POD_coredns-6799fbcd5-pp8sr_kube-system_ed8eaa59-eaa6-4dd4-a3be-1cae57bd5fd0_0

docker images -a
REPOSITORY                         TAG       IMAGE ID       CREATED         SIZE
rancher/local-path-provisioner     v0.0.24   b29384aeb4b1   13 months ago   40.1MB
rancher/mirrored-metrics-server    v0.6.3    817bbe3f2e51   13 months ago   68.9MB
rancher/mirrored-coredns-coredns   1.10.1    ead0a4a53df8   15 months ago   53.6MB
rancher/mirrored-pause             3.6       6270bb605e12   2 years ago     683kB

修改配置

1
vim ~/.colima/helm-test/colima.yaml

修改配置,如下:

1
2
3
4
5
6
7
8
9
# Number of CPUs to be allocated to the virtual machine.
cpu: 2

# Size of the disk in GiB to be allocated to the virtual machine.
# NOTE: changing this has no effect after the virtual machine has been created.
disk: 60

# Size of the memory in GiB to be allocated to the virtual machine.
memory: 2

修改保存后,重启 colima restart helm-test

注意:修改配置后重启colima服务,docker镜像和容器实例会被删除,需要重新拉取。

停止

停止 Docker 只需要停止 Colima 虚拟机即可:

1
colima stop

停止和删除指定实例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
colima stop helm-test
INFO[0000] stopping colima [profile=helm-test]
WARN[0000] error retrieving runtimes: error retrieving current runtime: empty value
INFO[0000] stopping ...                                  context=vm
INFO[0001] done

colima list
PROFILE      STATUS     ARCH      CPUS    MEMORY    DISK     RUNTIME    ADDRESS
default      Running    x86_64    1       2GiB      10GiB    docker
helm-test    Stopped    x86_64    2       2GiB      60GiB

colima delete helm-test
are you sure you want to delete colima [profile=helm-test] and all settings? [y/N] y
INFO[0004] deleting colima [profile=helm-test]
INFO[0005] done

注意:重启或停止删除colima服务,docker镜像和容器实例会被删除。

运行Docker容器

启动一个nginx容器

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
docker run --rm -d --name nginx -p 8080:80 nginx:latest
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
b0a0cf830b12: Pull complete
8ddb1e6cdf34: Pull complete
5252b206aac2: Pull complete
988b92d96970: Pull complete
7102627a7a6e: Pull complete
93295add984d: Pull complete
ebde0aa1d1aa: Pull complete
Digest: sha256:ed6d2c43c8fbcd3eaa44c9dab6d94cb346234476230dc1681227aa72d07181ee
Status: Downloaded newer image for nginx:latest
56b934f1c033978c66c931ad1ccafe543813a2dd2db4e2978a204dcd2cf1ed0c

docker ps
CONTAINER ID   IMAGE          COMMAND                   CREATED          STATUS          PORTS                                   NAMES
56b934f1c033   nginx:latest   "/docker-entrypoint.…"   29 seconds ago   Up 30 seconds   0.0.0.0:8080->80/tcp, :::8080->80/tcp   nginx

访问nginx测试

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
curl -I http://localhost:8080
HTTP/1.1 200 OK
Server: nginx/1.25.5
Date: Sat, 04 May 2024 05:15:34 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Tue, 16 Apr 2024 14:29:59 GMT
Connection: keep-alive
ETag: "661e8b67-267"
Accept-Ranges: bytes

参考