我想创建一个 Dockerfile,在其中运行 Ollama 并内置 Mistral 模型。现在,我只实现了这一点:当我运行 Ollama 时,它会在一个 Dockerfile 中下载 Mistral(我首先使用 Docker compose,但最终设法使用一个 Dockerfile)。
我想知道,是否可以将 Mistral 模型永久内置到 Ollama 图像中?这是我当前的解决方案:
entrypint.sh
#!/bin/sh
/bin/ollama serve &
# Wait for the server to start
sleep 5
# Execute the curl command
curl -X POST -d '{"name": "mistral"}' http://127.0.0.1:11434/api/pull
# Wait indefinitely to keep the container running
tail -f /dev/null
和
ollama
图像(Dockerfile):
# Use a base image for the application service
FROM ollama/ollama:0.1.37
# Expose port 11434 (assuming the application listens on this port)
EXPOSE 11434
# Define a volume for storing Ollama data
VOLUME /root/.ollama
# Install curl (assuming it's not already installed in the base image)
RUN apt-get update && apt-get install -y curl
# Define volumes
VOLUME ollama_data
# Copy the entrypoint script into the image
COPY entrypoint.sh /usr/local/bin/entrypoint.sh
# Make the script executable
RUN chmod +x /usr/local/bin/entrypoint.sh
# Set the entrypoint to the script
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
它工作得很好,但它下载了 Mistral 模型。我只想在图像构建过程中下载一次。可以吗?
您可以下载 Mistral 模型并将其在镜像构建过程中存储在适当的位置(根目录)。
更新了
Dockerfile
将下载Mistral模型并将其存储在/root/.ollama/models/
中,不需要每次容器启动时都下载。
# Use a base image for the application service
FROM ollama/ollama:0.1.37
# Expose port 11434 (assuming the application listens on this port)
EXPOSE 11434
# Install curl and jq (assuming they're not already installed in the base image)
RUN apt-get update && apt-get install -y curl jq
# Define a volume for storing Ollama data
VOLUME /root/.ollama
# Download the Mistral model during the build process
RUN mkdir -p /root/.ollama/models \
&& curl -X POST -d '{"name": "mistral"}' http://127.0.0.1:11434/api/pull \
&& mv $(curl -s http://127.0.0.1:11434/api/models | jq -r '.[].path') /root/.ollama/models/
# Copy the entrypoint script into the image
COPY entrypoint.sh /usr/local/bin/entrypoint.sh
# Make the script executable
RUN chmod +x /usr/local/bin/entrypoint.sh
# Set the entrypoint to the script
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
如果该 API 调用检索数据并将其存储在本地,那么应该可以在 Dockerfile 中运行它。
RUN
命令不会持久保存正在运行的进程,因此它应该可以启动临时服务器,运行 curl
命令,并让普通容器关闭序列清理进程。 这一切都需要在单个 RUN
命令中发生,例如
RUN ollama serve & \
curl --retry 10 --retry-connrefused --retry-delay 1 http://localhost:11434/ && \
curl -X POST -d '{"name": "mistral"}' http://localhost:11434/api/pull
就 Dockerfile 命令而言,这需要位于 RUN apt-get install curl
之后,以及影响数据目录的任何
VOLUME
指令之前。 如果基础镜像 Dockerfile 本身声明了一个 VOLUME
那么这可能是不可能的(对于 这个镜像尤其是它似乎不可能)。 (您可能根本不需要或不想要
VOLUME
指令。)
一旦进入 Dockerfile,您就可以摆脱自定义入口点脚本,只需设置主映像的 CMD
即可运行服务器。 基础镜像已经将 ollama serve
作为默认命令运行,因此您可以完全删除
ENTRYPOINT
和 CMD
行。 EXPOSE
线也在基础图像中。 您也许可以将 Dockerfile 减少到仅FROM ollama:0.1.48
RUN apt-get update && \
DEBIAN_FRONTEND=noninteractive \
apt-get install --no-install-recommends --assume-yes \
curl
RUN ollama serve & \
curl --retry 10 --retry-connrefused --retry-delay 1 http://localhost:11434/ && \
curl -X POST -d '{"name": "mistral"}' http://localhost:11434/api/pull