国内用户必看】用Lima在macOS免费搭建Docker环境,彻底解决镜像与收费难题!
为了在不同操作系统有一致操作体验,我选择使用docker技术安装ollama,这样子还有一个好处,即使玩坏了,删除重建即可,所以首先需要在开发机上安装docker,从2024年开始在国内使用docker难度越来越大,主要有2大原因:
- docker开始收费了,办公电脑公司不让在安装docker桌面版了
- docker国内镜像一夜之间都不能用了,docker官方镜像在国内也是无法访问的
下面是在 macOS 上使用 limactl 安装和配置 Docker 的完整步骤。这个方法比 Docker Desktop 更轻量且完全免费。
准备工作
确保你的 macOS 系统已更新到较新版本
确保已安装 Homebrew(macOS 包管理器)
lima&docker安装步骤
步骤 1: 安装 Lima
打开终端(Terminal),执行以下命令:
# 使用 Homebrew 安装 Lima
brew install lima
# 验证安装是否成功
limactl --version
步骤 2: 启动 Docker 实例
Lima 使用模板来创建预配置的虚拟机。我们将使用官方提供的 Docker 模板:
# 创建并启动一个名为 "docker" 的实例(使用非root用户模式,推荐)
limactl start --name=docker template://docker
注:除了可以使用官方模板也可以自定义配置文件,启动命令&配置文件&执行过程日志示例如下
# 与上面的命令 limactl start --name=docker template://docker 二选一执行即可
limactl start ~/work/docker/lima-default.yaml
arch: "aarch64"
# 基本系统配置
images:
- location: "https://cloud-images.ubuntu.com/releases/24.04/release/ubuntu-22.04-server-cloudimg-amd64.img"
arch: "x86_64"
- location: "https://cloud-images.ubuntu.com/releases/24.04/release/ubuntu-22.04-server-cloudimg-arm64.img"
arch: "aarch64"
# 系统配置脚本
provision:
- mode: system
script: |
#!/bin/bash
set -eux -o pipefail
# 安装 Docker
curl -fsSL https://get.docker.com | sh
# 将用户添加到 docker 组
usermod -aG docker "${LIMA_USER}"
# 安装 Docker Compose 插件
apt-get install -y docker-compose-plugin
# 挂载配置
mounts:
- location: "~"
mountPoint: "/home/${LIMA_USER}.linux"
# 端口转发(关键!让主机可以访问虚拟机内的 Docker)
portForwards:
- guestSocket: "/var/run/docker.sock"
hostSocket: "${LIMA_HOST_HOME}/.lima/docker/sock/docker.sock"
# 其他配置
ssh:
localPort: 60022
containerd:
system: false
user: false
? Creating an instance "docker" Proceed with the current configuration
INFO[0001] Replacing "http_proxy" value "socks5h://127.0.0.1:13659" with "socks5h://192.168.5.2:13659"
INFO[0001] Replacing "https_proxy" value "socks5h://127.0.0.1:13659" with "socks5h://192.168.5.2:13659"
INFO[0001] Starting the instance "docker" with VM driver "vz"
INFO[0001] Attempting to download the image arch=aarch64 digest= location="https://cloud-images.ubuntu.com/releases/24.04/release/ubuntu-24.04-server-cloudimg-arm64.img"
INFO[0007] Using cache "/Users/mac/Library/Caches/lima/download/by-url-sha256/002fbe468673695a2206b26723b1a077a71629001a5b94efd8ea1580e1c3dd06/data"
INFO[0007] Converting "/Users/mac/.lima/docker/basedisk" (qcow2) to a raw disk "/Users/mac/.lima/docker/diffdisk"
3.50 GiB / 3.50 GiB [---------------------------------------] 100.00% 1.56 GiB/s
INFO[0009] Expanding to 100GiB
INFO[0010] Attempting to download the nerdctl archive arch=aarch64 digest="sha256:544fa1e518155fcc01a117ea49819d12d96b4dacfb2b62922f9f7956dc9f6dc8" location="https://github.com/containerd/nerdctl/releases/download/v2.1.3/nerdctl-full-2.1.3-linux-arm64.tar.gz"
INFO[0010] Using cache "/Users/mac/Library/Caches/lima/download/by-url-sha256/46d9ee12c9f3f484518470db8093719f2b3ddca7daa6f1741c71e0aeb198db2a/data"
INFO[0010] [hostagent] Replacing "http_proxy" value "socks5h://127.0.0.1:13659" with "socks5h://192.168.5.2:13659"
INFO[0010] [hostagent] Replacing "https_proxy" value "socks5h://127.0.0.1:13659" with "socks5h://192.168.5.2:13659"
INFO[0010] [hostagent] Replacing "http_proxy" value "socks5h://127.0.0.1:13659" with "socks5h://192.168.5.2:13659"
INFO[0010] [hostagent] Replacing "https_proxy" value "socks5h://127.0.0.1:13659" with "socks5h://192.168.5.2:13659"
INFO[0011] [hostagent] hostagent socket created at /Users/mac/.lima/docker/ha.sock
INFO[0011] [hostagent] Starting VZ (hint: to watch the boot progress, see "/Users/mac/.lima/docker/serial*.log")
INFO[0012] SSH Local Port: 60022
INFO[0011] [hostagent] Waiting for the essential requirement 1 of 2: "ssh"
INFO[0011] [hostagent] [VZ] - vm state change: running
INFO[0021] [hostagent] Waiting for the essential requirement 1 of 2: "ssh"
INFO[0031] [hostagent] Waiting for the essential requirement 1 of 2: "ssh"
INFO[0041] [hostagent] Waiting for the essential requirement 1 of 2: "ssh"
INFO[0052] [hostagent] Waiting for the essential requirement 1 of 2: "ssh"
INFO[0062] [hostagent] Waiting for the essential requirement 1 of 2: "ssh"
INFO[0072] [hostagent] Waiting for the essential requirement 1 of 2: "ssh"
INFO[0082] [hostagent] Waiting for the essential requirement 1 of 2: "ssh"
INFO[0092] [hostagent] Waiting for the essential requirement 1 of 2: "ssh"
INFO[0102] [hostagent] Waiting for the essential requirement 1 of 2: "ssh"
INFO[0112] [hostagent] Waiting for the essential requirement 1 of 2: "ssh"
INFO[0122] [hostagent] Waiting for the essential requirement 1 of 2: "ssh"
INFO[0132] [hostagent] Waiting for the essential requirement 1 of 2: "ssh"
INFO[0142] [hostagent] Waiting for the essential requirement 1 of 2: "ssh"
这个过程会:
- 下载 Ubuntu 基础镜像
- 在虚拟机内自动安装 Docker、Docker Compose 和其他相关工具
- 配置文件共享和端口转发
- 可能需要几分钟时间,取决于你的网络速度
步骤 3: 配置 Shell 环境
安装完成后,需要设置环境变量,让 macOS 上的 Docker CLI 能够与 Lima 虚拟机中的 Docker 引擎通信:
# 将以下行添加到你的 shell 配置文件(~/.zshrc 或 ~/.bash_profile)
echo 'export DOCKER_HOST="unix://${HOME}/.lima/docker/sock/docker.sock"' >> ~/.bash_profile
# 使配置生效
source ~/.bash_profile
步骤 4: 验证安装
测试 Docker 是否正常工作:
# 检查 Docker 版本
docker --version
# 运行测试容器
docker run --rm hello-world
# 查看 Docker 系统信息
docker info
如果这些命令都能正常执行并返回信息,而没有报错说无法连接到 Docker 守护进程,那么就说明您的 Shell 环境已经配置成功!
总结
配置注意事项
附加常用命令
limactl list
NAME STATUS SSH VMTYPE ARCH CPUS MEMORY DISK DIR
docker Stopped 127.0.0.1:60022 vz aarch64 4 4GiB 100GiB ~/.lima/docker
limactl delete docker
INFO[0000] The vz driver process seems already stopped
INFO[0000] The host agent process seems already stopped
INFO[0000] Removing *.pid *.sock *.tmp under "/Users/mac/.lima/docker"
INFO[0000] Deleted "docker" ("/Users/mac/.lima/docker")
limactl list
WARN[0000] No instance found. Run `limactl create` to create an instance.
快速重建docker
如果虚拟机安装时出错,需要修改您的lima-docker.yaml文件想重新创建可以依次执行如下命令:
limactl delete -f docker
rm -rf /Users/mac/.lima/docker
limactl start --name=docker --debug ~/work/docker/lima-docker.yaml
替代方案2:使用coliama
步骤 1: 安装必要的工具
安装 Docker 客户端和 Colima
# 更新 Homebrew
brew update
# 安装 Docker 客户端 (命令行工具)
brew install docker
# 安装 Docker Compose (可选但推荐)
brew install docker-compose
# 安装 Colima
brew install colima
步骤 2: 启动 Colima
2.1 使用默认配置启动 Colima
# 简单启动 (使用默认配置)
colima start
2.2 使用自定义配置启动 (推荐)
# 使用自定义配置启动
colima start --cpu 4 --memory 8 --disk 100 --runtime docker
# 注释如下
colima start \
--cpu 4 \ # 分配 4 个 CPU 核心
--memory 8 \ # 分配 8GB 内存
--disk 100 \ # 分配 100GB 磁盘空间
--arch aarch64 \ # 使用 ARM64 架构 (适用于 M1/M2 Mac)
--runtime docker # 使用 Docker 运行时
colima start --profile /Users/mac/.colima/default/colima.yaml
INFO[0000] starting colima
INFO[0000] runtime: docker
INFO[0002] creating and starting ... context=vm
INFO[0017] provisioning ... context=docker
INFO[0018] starting ... context=docker
INFO[0020] done
步骤 3: 验证安装
3.1 检查 Colima 状态
colima status
INFO[0000] colima is running using macOS Virtualization.Framework
INFO[0000] arch: aarch64
INFO[0000] runtime: docker
INFO[0000] mountType: virtiofs
INFO[0000] docker socket: unix:///Users/mac/.colima/default/docker.sock
INFO[0000] containerd socket: unix:///Users/mac/.colima/default/containerd.sock
3.2 检查 Docker 是否正常工作
# 检查 Docker 版本
docker --version
# 检查 Docker 信息
docker info
# 运行测试容器
docker run hello-world
步骤 4: 常用 Colima 命令
4.1 管理 Colima 实例
# 停止 Colima
colima stop
# 暂停 Colima
colima pause
# 恢复 Colima
colima unpause
# 删除 Colima 实例
colima delete
# 查看 Colima 日志
colima logs
步骤 5: 配置 Colima (可选)
5.1 创建 Colima 配置文件
# 创建配置文件目录
mkdir -p ~/.colima
# 创建配置文件 (如果需要高级配置)
# Colima 会自动使用默认配置,通常不需要手动创建配置文件
5.2 使用不同的运行时
# 如果需要使用 containerd 而不是 Docker
colima start --runtime containerd
# 切换回 Docker
colima stop
colima start --runtime docker
步骤 6: 解决常见问题
6.1 如果遇到权限问题
# 将用户添加到 docker 组 (通常不需要,Colima 会自动处理)
sudo usermod -aG docker $USER
# 或者直接使用 sudo (不推荐长期使用)
sudo colima start
6.2 如果启动失败
# 查看详细日志
colima start --debug
# 或者查看日志文件
colima logs
6.3 重置 Colima
# 完全重置 Colima
colima delete
rm -rf ~/.colima
brew reinstall colima
colima start
常见错误
🚫 错误配置示例
❌ 错误:使用环境变量(无效)
export http_proxy=http://127.0.0.1:8080
docker pull hello-world
Docker 不会自动读取 http_proxy 环境变量,除非你通过 daemon.json 显式配置。
✅ 正确:通过 daemon.json 配置
进入虚拟机并修改文件
colima ssh
vi /etc/docker/daemon.json
注:
如果没有vi命令,则先安装一下
默认情况下daemon.json 中没有 proxies节点,下方示例中proxies均属新增内容
{
"registry-mirrors": ["https://<your-mirror>.mirror.aliyuncs.com"],
"proxies": {
"default": {
"httpProxy": "http://127.0.0.1:8080",
"httpsProxy": "http://127.0.0.1:8080",
"noProxy": "localhost,127.0.0.1"
}
}
}
保存后退出虚拟机,重启colima
colima stop
colima start
docker info
docker info|grep -i proxy -A 5
# 下方是docker info|grep -i proxy -A 5执行结果示例
HTTP Proxy: socks5h://192.168.5.2:8080
HTTPS Proxy: socks5h://192.168.5.2:8080
Experimental: false
Insecure Registries:
::1/128
127.0.0.0/8
Live Restore Enabled: false
🧩 正确:Docker 客户端代理设置(非守护进程)
如果只想在 客户端(如 docker build 或 docker pull)使用代理,可通过 HTTP_PROXY 环境变量临时设置:
HTTP_PROXY=http://127.0.0.1:8080
docker pull hello-world
ollama&Phi-3:mini安装步骤
这部分之前已经整理过了,详情见:https://alioo.blog.csdn.net/article/details/150591424