docker+gpu部署模型

操作环境
1 | $ screenfetch |
安装NVIDIA Container Toolkit
Installing the NVIDIA Container Toolkit
下面是安装详细步骤
安装
Configure the production repository:
1
2
3
4curl -fsSL https://nvidia.github.io/libnvidia-container/gpgkey | sudo gpg --dearmor -o /usr/share/keyrings/nvidia-container-toolkit-keyring.gpg \
&& curl -s -L https://nvidia.github.io/libnvidia-container/stable/deb/nvidia-container-toolkit.list | \
sed 's#deb https://#deb [signed-by=/usr/share/keyrings/nvidia-container-toolkit-keyring.gpg] https://#g' | \
sudo tee /etc/apt/sources.list.d/nvidia-container-toolkit.listOptionally, configure the repository to use experimental packages:
1
sed -i -e '/experimental/ s/^#//g' /etc/apt/sources.list.d/nvidia-container-toolkit.list
Update the packages list from the repository:
1
sudo apt-get update
Install the NVIDIA Container Toolkit packages:
1
2
3
4
5
6export NVIDIA_CONTAINER_TOOLKIT_VERSION=1.17.8-1
sudo apt-get install -y \
nvidia-container-toolkit=${NVIDIA_CONTAINER_TOOLKIT_VERSION} \
nvidia-container-toolkit-base=${NVIDIA_CONTAINER_TOOLKIT_VERSION} \
libnvidia-container-tools=${NVIDIA_CONTAINER_TOOLKIT_VERSION} \
libnvidia-container1=${NVIDIA_CONTAINER_TOOLKIT_VERSION}
配置
Configure the container runtime by using the
nvidia-ctk
command:1
sudo nvidia-ctk runtime configure --runtime=docker
The
nvidia-ctk
command modifies the/etc/docker/daemon.json
file on the host. The file is updated so that Docker can use the NVIDIA Container Runtime.Restart the Docker daemon:
1
sudo systemctl restart docker
Rootless mode
To configure the container runtime for Docker running in Rootless mode , follow these steps:
1. Configure the container runtime by using the nvidia-ctk
command:
1 | nvidia-ctk runtime configure --runtime=docker --config=$HOME/.config/docker/daemon.json |
2. Restart the Rootless Docker daemon:
1 | systemctl --user restart docker |
3. Configure `/etc/nvidia-container-runtime/config.toml` by using the `sudo nvidia-ctk` command:
1 | sudo nvidia-ctk config --set nvidia-container-cli.no-cgroups --in-place |
配置apt代理(optional)
由于使用了github.io镜像,提高速度配置apt代理
之前在服务器上面安装了trojan客户端
- 启动trojan
1 | $ cat ~/trojan.sh |
- 配置apt代理
socks5h代表使用代理服务器的域名解析
1 | $ sudo tee /etc/apt/apt.conf.d/proxy.conf <<EOF |
不用了过后可以sudo mv /etc/apt/apt.conf.d/proxy.conf /etc/apt/apt.conf.d/proxy.conf.bak
model_api.py、Dockerfile
Model_api.py
1 | import os |
Dockerfile
1 | FROM python:3.12 |
启动镜像
镜像打包
–progress=plain参数可以显示详细过程
1 | docker build -t model-api:1.0.0 . --progress=plain |
容器运行
1 | docker run --gpus all -d -p 80:8000 --name model-api model-api:1.0.0 |
查看容器状态
1 | $ docker exec -it model-api bash |
显示True说明成功了,当然也可以在容器中使用nvidia-smi来查看gpu状态
- 标题: docker+gpu部署模型
- 作者: Chris Harris
- 创建于: 2025-08-29 00:59:57
- 更新于: 2025-09-29 01:00:33
- 链接: https://s4g.top/2025/08/29/docker-gpu部署模型/
- 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。