完整教程:机器学习入门,用Lima在macOS免费搭建Docker环境,彻底解决镜像与收费难题!

news/2025/9/21 17:03:27/文章来源:https://www.cnblogs.com/yxysuanfa/p/19103826

国内用户必看】用Lima在macOS免费搭建Docker环境,彻底解决镜像与收费难题!

为了在不同操作系统有一致操作体验,我选择使用docker技术安装ollama,这样子还有一个好处,即使玩坏了,删除重建即可,所以首先需要在开发机上安装docker,从2024年开始在国内使用docker难度越来越大,主要有2大原因:

  1. docker开始收费了,办公电脑公司不让在安装docker桌面版了
  2. 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"

这个过程会:

  1. 下载 Ubuntu 基础镜像
  2. 在虚拟机内自动安装 Docker、Docker Compose 和其他相关工具
  3. 配置文件共享和端口转发
  4. 可能需要几分钟时间,取决于你的网络速度

步骤 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.

ollama&Phi-3:mini安装步骤

这部分之前已经整理过了,详情见:https://alioo.blog.csdn.net/article/details/150591424

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/908892.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

go语言中的基本数据类型

go语言中的基本数据类型package mainimport ("fmt" )func main() {// 整型var a int = 10var b int8 = -8var c uint16 = 65535var d int64 = 1234567890// 浮点型var e float32 = 3.14var f float64 = 2.71…

实用指南:rsync +生产级 lsyncd 实时同步方案

实用指南:rsync +生产级 lsyncd 实时同步方案2025-09-21 16:51 tlnshuju 阅读(0) 评论(0) 收藏 举报pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: …

锁定Nvidia驱动版本

在 Ubuntu 系统中,NVIDIA 显卡驱动通常通过系统的包管理器(如 apt)进行管理和更新。要防止 NVIDIA 驱动程序自动更新,你可以锁定当前安装的驱动版本,这样即使系统进行了更新,驱动程序也会保持在当前版本。以下是…

第二十一章-sql 注入-union 联合注入 (1)

用户须知1.免责声明:本教程作者及相关参与人员对于任何直接或间接使用本教程内容而导致的任何形式的损失或损害,包括但不限于数据丢失、系统损坏、个人隐私泄露或经济损失等,不承担任何责任。所有使用本教程内容的个…

Android开发参考

WorkManager https://www.cnblogs.com/octsun/category/2471458.html

求出e的值

//题意:利用公式e = 1 + 1/1! + 1/2! + 1/3! + ... + 1/n! 求e ; //输入:只有一行,该行包含一个整数n(2<=n<=15),表示计算e时累加到1/n!。 //输出:输出只有一行,该行包含计算出来的e的值,要求打印小数…

CSP-S模拟24

前言: 没写完的话就先咕着,先滚去学文化课了。 \(T1:\) 炒币 \(T2:\) 凑数 \(T3:\) 同构 \(T4:\) 重建

今年CSP...

我要晋级,我要晋级,我要晋级,我要晋级考的依托。J组,阅读程序第二道,第二层for循环的";"号没看到。大概86.5~88.5左右,还是江苏,晋级有点悬啊。能考88.5还是有点意外,以前都没真正意义上做过一张试卷…

实用指南:VGG改进(9):融合Axial Attention的VGG16架构

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

0voice-2.1.1-io多路复用select/poll/epoll

select之前的模式:\(1\) 请求 , \(1\) 线程好处:代码逻辑简单 缺点:不利于并发, \(1 \ k\) 并发量左右select 提供文件集合 fd_set,集合的大小

Transformer与ViT

前言: Transformer 结构非常重要,需要认真学习一遍 李沐老师课程 Transformer 论文 Transformer 代码 Transformer 自测题目 [Transformer 博客](Transformer/BERT/实战 | 冬于的博客 (ifwind.github.io)) 一.Trans…

comfUI背后的技术——VAE - 实践

comfUI背后的技术——VAE - 实践pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco&qu…

实用指南:Maven、Spring Boot、Spring Cloud以及它们的相互关系

实用指南:Maven、Spring Boot、Spring Cloud以及它们的相互关系2025-09-21 16:28 tlnshuju 阅读(0) 评论(0) 收藏 举报pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !imp…

【57页PPT】智慧高效的方案智慧医院信息化整体规划设计方案(附下载方式)

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

WordPress开放嵌入自动发现功能中的XSS漏洞分析

本文详细分析了WordPress中通过开放嵌入自动发现功能存在的XSS漏洞,包括postMessage()机制的安全问题、Safari浏览器的特殊行为以及完整的漏洞复现步骤,揭示了广泛使用的平台仍可能存在安全风险。WordPress开放嵌入自…

第二次软工作业

第二次软工作业软件工程第二次作业_个人项目 Github连接: mocheen/se_homework: homework ](https://github.com/mocheen/se_homework) 这个作业属于哪个课程 https://edu.cnblogs.com/campus/gdgy/Class12Grade23Comp…

20250921 模拟赛 T4 题解

Description https://zhengruioi.com/problem/3343?cid=1976 Solution 容易发现区间 LIS 满足四边形不等式,所以最终的答案关于划分段数是凸的。 设 \(d_i=f_i-f_{i-1}\)。那么由于 \(\sum d_i=n\) 且 \(d_i\) 不增,…

1.3 课前问题列表

1.什么样的方法应该用static修饰?不用static修饰的方法往往具有什么特性?Student的getName应该用static修饰吗? 1.通常是工具类方法、单例模式中获取单例对象的方法等应该用static修饰 2.不用static修饰的方法特性:…

NOIP 模拟赛十一

贪心+打表+数据结构+DPA. 倒序贪心即可。点击查看#include <bits/stdc++.h> #define lep(i, a, b) for (int i = a; i <= b; ++i) #define rep(i, a, b) for (int i = a; i >= b; --i) #define il inline …