Advertisement

ollama自定义模型容器部署(cpu版)

阅读量:

docker镜像封装

docker安装(略…)
拉取ollama镜像
复制代码
    docker pull ollama/ollama  # 约1.98G
ollama容器
复制代码
    # 前一个11435是宿主机要对外暴露的tcp端口,可自定义修改
    docker run -d -p 11435:11434 --name ollama ollama/ollama
准备模型注册文件

文件名称为Modelfile,如可以使用 vim Modelfile,写入数据如下

复制代码
    FROM Qwen2.5-3B-Instruct-Q4_K_L.gguf
    
    # set the temperature to 1 [higher is more creative, lower is more coherent]
    PARAMETER temperature 0.7
    PARAMETER top_p 0.8
    PARAMETER repeat_penalty 1.05
    PARAMETER top_k 20
    
    TEMPLATE """{{ if .Messages }}
    {{- if or .System .Tools }}<|im_start|>system
    {{ .System }}
    {{- if .Tools }}
    
    # Tools
    
    You are provided with function signatures within <tools></tools> XML tags:
    <tools>{{- range .Tools }}
    {"type": "function", "function": {{ .Function }}}{{- end }}
    </tools>
    
    For each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:
    <tool_call>
    {"name": <function-name>, "arguments": <args-json-object>}
    </tool_call>
    {{- end }}<|im_end|>
    {{ end }}
    {{- range $i, $_ := .Messages }}
    {{- $last := eq (len (slice $.Messages $i)) 1 -}}
    {{- if eq .Role "user" }}<|im_start|>user
    {{ .Content }}<|im_end|>
    {{ else if eq .Role "assistant" }}<|im_start|>assistant
    {{ if .Content }}{{ .Content }}
    {{- else if .ToolCalls }}<tool_call>
    {{ range .ToolCalls }}{"name": "{{ .Function.Name }}", "arguments": {{ .Function.Arguments }}}
    {{ end }}</tool_call>
    {{- end }}{{ if not $last }}<|im_end|>
    {{ end }}
    {{- else if eq .Role "tool" }}<|im_start|>user
    <tool_response>
    {{ .Content }}
    </tool_response><|im_end|>
    {{ end }}
    {{- if and (ne .Role "assistant") $last }}<|im_start|>assistant
    {{ end }}
    {{- end }}
    {{- else }}
    {{- if .System }}<|im_start|>system
    {{ .System }}<|im_end|>
    {{ end }}{{ if .Prompt }}<|im_start|>user
    {{ .Prompt }}<|im_end|>
    {{ end }}<|im_start|>assistant
    {{ end }}{{ .Response }}{{ if .Response }}<|im_end|>{{ end }}"""
    
    # set the system message
    SYSTEM """You are Qwen, created by Alibaba Cloud. You are a helpful assistant."""
    
    
    
![](https://ad.itadn.com/c/weblog/blog-img/images/2025-08-16/hsK3HeJyfvjoCwBqA05uTgO9idSr.png)
复制模型文件到ollama容器
复制代码
    # 提前下载好Qwen2.5-3B-Instruct-Q4_K_L.gguf
    # https://hf-mirror.com/bartowski/Qwen2.5-3B-Instruct-GGUF/blob/main/Qwen2.5-3B-Instruct-Q4_K_L.gguf
    docker cp Qwen2.5-3B-Instruct-Q4_K_L.gguf ollama:/root
    # 复制模型注册文件
    docker cp Modelfile ollama:/root
注册模型到ollama服务中
复制代码
    docker exec -it ollama bash  # 进入容器
    cd /root
    ls  # 查看当前目录 应该存在 Qwen2.5-3B-Instruct-Q4_K_L.gguf 和 Modelfile
    ollama create qwen2.5_3b -f Modelfile  # 模型名称这里可以自定义
    ollama list  # 查看注册模型 应该可以看到qwen2.5_3b
模型

ollama服务已正式开通,请您遵循官方指导文档规定调用预定义的API接口进行操作。具体操作示例可供参考。

复制代码
    # linxu可以使用以下curl命令
    curl --location --request POST 'http://127.0.0.1:11435/v1/chat/completions' \
    --header 'Content-Type: application/json' \
    --data '{
    "model": "qwen2.5_3b",
    "messages": [
        {
        "role": "user",
        "content": "你好"
        }
    ],
    "stream": false
    }'
    
    # windows可以在cmd中使用以下curl命令
    curl --request POST "http://127.0.0.1:11435/v1/chat/completions" --header "Content-Type: application/json" --data-raw "{\"model\": \"qwen2.5_3b\",\"messages\": [{\"role\": \"user\",\"content\": \"你好\"}],\"stream\": false}"
    
    
    
![](https://ad.itadn.com/c/weblog/blog-img/images/2025-08-16/TAmz6dWYNKVUcpaJtbn50fr9MoXv.png)
存储当前容器(快照)
复制代码
    docker commit ollama qwen2.5-3b_image:v1  # v1为自定义版本号
    docker save -o qwen2.5-3b_image_image_v1.tar qwen2.5-3b_image:v1  # 保存为镜像文件
使用zip压缩与解压缩(可选)
复制代码
    zip qwen2.5-3b_image_image_v1.zip qwen2.5-3b_image_image_v1.tar  # 将tar压缩成zip文件,减小体积
    unzip qwen2.5-3b_image_image_v1.zip  # 将zip解压缩成tar文件
文件切割(可选)
复制代码
    split -b 3G qwen2.5-3b_image_image_v1.tar qwen2.5-3b_image_  # 切割成 my_ollama_image_aa、my_ollama_image_ab、my_ollama_image_ac
    cat qwen2.5-3b_image_* > qwen2.5-3b_image_image_v1.tar  # 将 my_ollama_image_* 多个文件合并成 my_ollama_image_v1.tar
    # windows cmd命令
    # copy /b qwen2.5-3b_image_aa + qwen2.5-3b_image_ab + qwen2.5-3b_image_ac qwen2.5-3b_image_image_v1.tar

一键部署

加载快照镜像文件

复制代码
    docker load -i qwen2.5-3b_image_image_v1.tar  # 在terminal或cmd(windows)等终端中命令
快照容器
复制代码
    # 前台测试
    docker run --rm -p 11435:11434 --name my_ollama qwen2.5-3b_image:v1
    
    # 后台
    docker run -d -p 11435:11434 --name my_ollama qwen2.5-3b_image:v1
注意事项

每隔三个工作分钟后进行一次ollama模型接口查询(可选),以防止模型进入卸载状态或发生冷启动问题,并提升响应速度

附:

常见命令
启动Ollama服务
从模型文件创建模型
显示当前可用的模型信息
运行当前配置的模型
从注册表中拉取指定版本的预训练权重
将本地训练好的参数推送到Ollama注册表中
列出所有可用的预训练权重版本
复制指定位置的参数文件到本地存储目录
删除指定位置的参数文件(注意:此操作无法恢复)
获取关于当前命令行参数的帮助信息

GPU 利用 为了使用 GPU 而需要安装 NVIDIA Container Toolkit:https://hub.docker.com/r/ollama/ollama

在Huggingface上获取Ollama兼容的GGUF模型作为推荐资源提供给前同事许多专家已经整理了大部分主流开源模型对应的GGUF量化版本并将这些资源整理成专门的网站以便大家快速访问。很多用户可能对如何高效地利用这些量化权重还存在疑问因此这里详细说明一下操作流程:首先请访问该网站找到目标预训练语言模型对应的GGUF权重包然后按照指引进行下载即可完成安装与配置过程

全部评论 (0)

还没有任何评论哟~