ubuntu20 用docker配置detectron2

本文介绍了如何在Docker 19.03及以上版本中配置GPU支持,包括安装步骤和NVIDIA容器工具的安装。同时详细讲解了如何使用Dockerfile构建Detectron2镜像,并探讨了挂载目录以实现代码修改和持久化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


前言

记录并汇总一些基础的操作和知识。


一、docker ≥ 19.03

Docker 19.03之后,增加了对gpus选项的支持,docker里面使用nvidia也不需要额外的安装nvidia-docker。
但是本机还是要装nvidia驱动,就是不需要cuda了。
卸载旧版本:

sudo apt-get remove docker docker-engine docker.io containerd runc

安装

sudo apt-get update
sudo apt-get install \
     apt-transport-https \
     ca-certificates \
     curl \
     gnupg-agent \
     software-properties-common

添加密钥

- curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

添加仓库

sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"

测试

sudo docker run hello-world

镜像加速配置:阿里云
https://help.aliyun.com/document_detail/60750.html

安装nvidia-container-toolkit

distribution=$(. /etc/os-release;echo $ID$VERSION_ID)  
curl -s -L https://nvidia.github.io/nvidia-docker/gpgkey | sudo apt-key add -  
curl -s -L https://nvidia.github.io/nvidia-docker/$distribution/nvidia-docker.list | sudo tee /etc/apt/sources.list.d/nvidia-docker.list  

sudo apt-get update && sudo apt-get install -y nvidia-container-toolkit  
sudo systemctl restart docker  

无法连接https://nvidia.github.io的解决方法:
(1)在http://tool.chinaz.com/dns?type=1&host=nvidia.github.io&ip=
查询ip修改host,把nvidia.github.io指向上面查到的ip

sudo gedit /etc/hosts
sudo /etc/init.d/network-manager restart

测试

sudo docker run --rm --gpus all nvidia/cuda:10.1-cudnn7-devel nvidia-smi

参考来自:https://blog.csdn.net/qq_36335426/article/details/111308213
https://blog.csdn.net/xie_daicheng/article/details/109592234

二、detectron2

1.从dockerfile构建image

cd docker/
# Build:
#可使用pip -i https://pypi.tuna.tsinghua.edu.cn/simple加速依赖包的安装,在dockerfile里修改
docker build --build-arg USER_ID=$UID -t detectron2:v0 .
#opencv-python默认没有装,可以自己加

# Launch:
#若无nvidia-container-toolkit会报错
#搞不懂这个--volume 
docker run --gpus all -it \
  --shm-size=8gb --env="DISPLAY" --volume="/tmp/.X11-unix:/tmp/.X11-unix:rw" \
  --name=detectron2 detectron2:v0

# Grant docker access to host X server to show images
xhost +local:`docker inspect --format='{{ .Config.Hostname }}' detectron2`

想直接把容器目录挂载出来还不行- -,真是醉了
挂载再另一个新文件夹就没事,搞不懂
可能是覆盖了,cp出来倒是可以。。。

2.挂载目录方便储存和操作

下载一份源码然后挂载到容器内,可直接修改和复制,容器内的目录也会实时刷新,删除退出容器,文件不会丢失。

detectron2默认安装在/home/appuser里可修改底层代码并保存为新的image

-v /home/***/detectron2:/home/***
本机路径:容器路径(只能使用绝对路径)
***根据情况自己选择,默认的容器内的源码不懂怎么挂载不出来,好像会被覆盖,导致为空,网上查不到。。

文件权限修改:
进入文件夹内-R递归

sudo chmod 777 * -R

总结

不用配环境真香,还方便打包,迁移环境,还有k8s看着也挺香的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值