下载镜像
 
创建Dockerfile
FROM ubuntu:24.04
LABEL maintainer="ubuntu-ollama"
EXPOSE 21
ARG DEBIAN_FRONTEND=noninteractive
 
# Install: dependencies, clean: apt cache, remove dir: cache, man, doc, change mod time of cache dir.
RUN apt-get update \
    && apt-get install -y --no-install-recommends \
       software-properties-common \
       rsyslog systemd systemd-cron sudo \
    && apt-get clean \
    && rm -Rf /usr/share/doc && rm -Rf /usr/share/man \
    && rm -rf /var/lib/apt/lists/* \
    && touch -d "2 hours ago" /var/lib/apt/lists
RUN sed -i 's/^\($ModLoad imklog\)/#\1/' /etc/rsyslog.conf
RUN rm -f /lib/systemd/system/systemd*udev* \
  && rm -f /lib/systemd/system/getty.target
ENV ASPNETCORE_URLS=http://+:21
VOLUME ["/sys/fs/cgroup", "/tmp", "/run"]
CMD ["/lib/systemd/systemd"]


构建镜像
docker build -t ubuntu-ollama:20 .



# 先拉取一个基础镜像
docker pull ubantu:20.04
# 运行该镜像
docker run -itd --name ubuntu-ollama --detach -p 21434:11434  --privileged --volume=/dp/docler/file/ubantu-ollama/cgroup:/sys/fs/cgroup:ro ubuntu-ollama:20 /bin/bash
docker exec -it --privileged=true  -u root ubuntu-ollama  /bin/bash
# 安装对应的服务依赖环境

apt-get update \
    && apt-get install -y --no-install-recommends \
       software-properties-common \
       rsyslog systemd systemd-cron sudo \
    && apt-get clean \
    && rm -Rf /usr/share/doc && rm -Rf /usr/share/man \
    && rm -rf /var/lib/apt/lists/* \
    && touch -d "2 hours ago" /var/lib/apt/lists
# 注释$ModLoad imklog 开头的行    
sed -i 's/^\($ModLoad imklog\)/#\1/' /etc/rsyslog.conf
# 清理和简化容器内的 systemd 环境
 #rm -f /lib/systemd/system/systemd*udev*  && rm -f /lib/systemd/system/getty.target

 
更新环境
apt update

apt upgrade
apt install sudo

sudo apt install g++ make

sudo apt install curl
sudo apt install vim
sudo useradd -r -s /bin/false systemd
sudo apt install systemctl

sudo systemctl daemon-reload
如果提示/usr/bin/systemctl:1628: SyntaxWarning: invalid escape sequence '\w'
找到\w改成\\w
如果提示ERROR:systemctl: dbus.service: Executable path is not absolute, ignoring: @/usr/bin/dbus-daemon @dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation --syslog-only
打开/lib/systemd/system/dbus.service 把@符号删除


sudo apt install systemd

下载文件(太慢的话看下一下)
curl -fsSL https://ollama.com/install.sh -o ollama_install.sh
使用github文件加速替换github下载地址
sed -i 's|https://ollama.com/download/ollama-linux|https://gh.llkk.cc/https://github.com/ollama/ollama/releases/download/v0.5.7/ollama-linux|g' ollama_install.sh
对文件中下载链接地址进行替换
增加可执行权限
chmod 777 ollama_install.sh
执行sh下载安装
sh ollama_install.sh


如果太慢可以 不下载
export OLLAMA_MIRROR="https://ghproxy.cn/https://github.com/ollama/ollama/releases/latest/download"
curl -fsSL https://ollama.com/install.sh | sed "s|https://ollama.com/download|$OLLAMA_MIRROR|g" | sh
 


修改监听IP

vim /etc/systemd/system/ollama.service

[service]下添加一行

Environment="OLLAMA_HOST=0.0.0.0:11434"


sudo systemctl daemon-reload

sudo systemctl restart ollama
sudo systemctl status ollama



ollama serve         #启动ollama
ollama create        #从模型文件创建模型
ollama show          #显示模型信息
ollama run           #运行模型
ollama pull          #从注册表中拉取模型
ollama push          #将模型推送到注册表
ollama list          #列出模型
ollama cp            #复制模型
ollama rm            #删除模型

ollama help          #获取有关任何命令的帮助信息

开机启动

apt install systemctl
# 重新加载 systemd 以识别新服务
sudo systemctl daemon-reload
# 启动服务
sudo systemctl start ollama.service
# 使服务开机自启
sudo systemctl enable ollama.service
# 检查服务状态
sudo systemctl status ollama.service

简化版本开机启动没有生效
创建/etc/start.sh
sudo systemctl start ollama.service
chmod +x start.sh

制作新镜像
# 完成后退出容器
exit
# 找到对应容器CONTAINER ID
docker ps -a
# 通过CONTAINER ID将容器提交为一个新的镜像
docker commit ubuntu-ollama ubuntu-ollama:21
#在运行Dockerfile文件将服务注册为容器启动时开启

创建开机启动镜像
FROM ubuntu-ollama:21
LABEL maintainer="ubuntu-ollama"
EXPOSE 21
ARG DEBIAN_FRONTEND=noninteractive
VOLUME ["/sys/fs/cgroup", "/tmp", "/run"]
CMD ["/lib/systemd/systemd"]
ENTRYPOINT ["/etc/start.sh"]

docker build -t ubuntu-ollama:22 .
docker run -itd --name ubuntu-ollama --detach -p 21434:11434  --privileged --volume=/dp/docler/file/ubantu-ollama/cgroup:/sys/fs/cgroup:ro ubuntu-ollama:22 /bin/bash
docker exec -it --privileged=true  -u root ubuntu-ollama  /bin/bash