自启动docker环境打包全流程

1.去cuda官网或者镜像网站下载cuda,复制docker pull命令回来运行即可。

官网:https://hub.docker.com/r/nvidia/cuda/tags?page=2&name=11.3

国内镜像网站:docker.io/nvidia/cuda 项目中国可用镜像列表 | 高速可靠的 Docker 镜像资源

一般选择devel版本。

输入docker images 查看所有镜像

2.找到对应remote_detect_hf,启动容器

作用于已启动的正在运行的容器:

1
docker exec -it 容器id /bin/bash

创建并启动一个全新的容器:

1
docker run -p 7896:7896 -it --gpus all remote_detect_hf:1.0 /bin/bashdocker exec -it 容器id /bin/bash

然后进行环境的安装。以python环境为例:

(1)apt upgrade

(2)apt安装python3,pip

(3)安装torch

1
pip install torch==2.6.0 torchvision==0.21.0 torchaudio==2.6.0 --index-url https://download.pytorch.org/whl/cu118

检查torch安装是否成功:

1
2
3
import torch
print(torch.__version__)
print(torch.cuda.is_available())

(4)pip安装程序所需环境

3.(用docker ps -a查看容器的id)将你需要的文件复制到对应文件夹(例如 \app )中:

1
docker exec -it 容器id /bin/bash

docker cp /path/on/host remote_detect_hf:/app

(remote_detect_hf要改成容器的id)

4.可以测试一下(比如运行test.py)能不能执行

5.退出容器,导出刚刚打包好的容器

1
docker commit eda05ad514f8 remote_detect_hf:1.1  (把eda05ad514f8改成容器id)

6.打开dockerfile(包含自启动命令)进行修改,在~/reptile/dockerfile中把remote_detect_hf:1.0改为你取的名字,例如remote_detect_hf:1.1

7.进入该目录

1
cd ~/reptile/dockerfile

8.执行docker build -t detect_hf:1.1 . (注意最后有个点)

9.导出完输入docker images看看有没有创建成功

10.测试:

1
docker run -p 7896:7896 -d --gpus all detect_hf:1.1

查看网页:http://localhost:7896/docs

11.打包输出:

1
docker save 0fdf2b4c26d3 > detect_hf2.tar (0fdf2b4c26d3改为镜像的id

使用时,加载压缩包:

1
docker load < detect_hf1.tar

修改tag:

1
docker tag 镜像id detect_hf:1.3

docker使用技巧

离开但不停止镜像:Ctrl+p(不松手)再按q

停止、删除镜像:

1
docker stop/rm 镜像id

删除容器:

1
docker rmi 容器id 

已经exited的镜像再次开启:

1
2
docker start 镜像id
docker attach 镜像id