Ubuntu系统docker gpu环境搭建

Ubuntu系统dockergpu环境搭建

安装步骤

前置安装

安装指定版本的依赖包
shell 复制代码
sudo apt install libnvidia-container1=1.4.0-1 libnvidia-container-tools=1.4.0-1 nvidia-container-toolkit=1.5.1-1

安装最新的依赖包

shell 复制代码
sudo apt install libnvidia-container1 libnvidia-container-tools nvidia-container-toolkit
用docker官方脚本安装Docker-ce
shell 复制代码
curl https://get.docker.com | sh && sudo systemctl --now enable docker
添加稳定仓库和GPG秘钥
shell 复制代码
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
更新源
shell 复制代码
sudo apt-get update

安装docker

安装nvidia-docker2
shell 复制代码
sudo apt-get install -y nvidia-docker2
重启docker服务
shell 复制代码
sudo systemctl restart docker
阿里云镜像加速

网址: https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors

相关命令

将用户加入到docker组中

不需要使用sudo操控docker

shell 复制代码
sudo gpasswd -a 用户名 docker

查看gpus参数是否成功安装

shell 复制代码
docker run --help | grep -i gpus

运行使用gpu的容器

-it表示进入镜像,后面的/bin/bash表示进入后的shell,如果不需要进入则去掉这两个

shell 复制代码
sudo docker run -it --gpus all --name 容器名 镜像 /bin/bash

docker运行的常用参数

  • -it: 以交互形式进入容器终端
  • -d: 后台运行并返回容器id
  • -p: 指定端口映射,格式为: 主机端口:容器端口
  • -v: 绑定一个卷,即做容器和主机的文件映射,格式为: 主机文件:容器文件
  • --restart=always: 总是重启
  • --dns 8.8.8.8: 指定容器使用的DNS
  • -e username="ritchie": 设置环境变量
  • --env-file=[]: 从指定文件读入环境变量
  • -m :设置容器使用内存最大值
  • --link=[]: 添加链接到另一个容器

查看容器端口的绑定情况

shell 复制代码
docker port 容器 端口

网络

查看网络详情

shell 复制代码
docker network  ls

创建docker网络

shell 复制代码
docekr network create -d bridge(网络类型,还有overlay) 网络名

连接容器

通过--network参数可以指定要加入的网络

shell 复制代码
docker run -itd --name test1 --network test-net ubuntu /bin/bash

docker常用命令

镜像

拉取镜像

shell 复制代码
docker pull 镜像地址:tag

删除镜像

存在使用镜像的容器时不能删除,需要使用-f强制删除

shell 复制代码
docker rmi 镜像名/镜像id

容器

创建容器

shell 复制代码
# 基本命令
docker run 镜像
# 常用命令(启动容器、指定名称、挂载、指定gpu环境并进入容器)
docker run -it -v 本地路径:容器内路径 --name 容器名 --gpus all 镜像名/镜像id /bin/bash

进入容器

shell 复制代码
# 开启新的终端
docker exec -it 容器id/容器名 /bin/bash
# 进入容器使用上次使用的终端
docker attach 容器id/容器名

退出容器

shell 复制代码
# 退出并关闭容器
exit
# 挂起容器(退出但不关闭容器)
ctrl + p + q

启动容器

shell 复制代码
docker start 容器名/容器id

关闭容器

shell 复制代码
docker stop 容器名/容器id

删除容器

shell 复制代码
# 删除已经关闭的容器
docker rm 容器名/容器id
# 强制删除容器
docker rm -f 容器名/容器id

docker相关问题解决方案

使用wsl时docker的容器无法联网

方案一

启动容器时,指定--dns 8.8.8.8 --dns 8.8.4.4或者--dns=114.114.114.114

方案二

永久解决方案: 在/etc/resolv.conf中添加如下代码:

shell 复制代码
nameserver 114.114.114.114
nameserver 8.8.8.8
nameserver 8.8.4.4

第一个是国内的dns,后两个是谷歌提供的dns

ubuntu容器换源

shell 复制代码
sed -i "s/archive.ubuntu.com/mirrors.aliyun.com/" /etc/apt/sources.list
sed -i "s/security.ubuntu.com/mirrors.aliyun.com/g" /etc/apt/sources.list

容器update时nvidia报错NO_PUBKEY A4B469963BF863CC

shell 复制代码
echo 'deb 
https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64 /' > /etc/apt/sources.list.d/cuda.list
gpg --keyserver keyserver.ubuntu.com --recv-keys A4B469963BF863CC
gpg --export --armor A4B469963BF863CC | apt-key add -

设置pip源

shell 复制代码
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
相关推荐
即将头秃的程序媛2 小时前
centos 7.9安装tomcat,并实现开机自启
linux·运维·centos
fangeqin2 小时前
ubuntu源码安装python3.13遇到Could not build the ssl module!解决方法
linux·python·ubuntu·openssl
爱奥尼欧3 小时前
【Linux 系统】基础IO——Linux中对文件的理解
linux·服务器·microsoft
超喜欢下雨天4 小时前
服务器安装 ros2时遇到底层库依赖冲突的问题
linux·运维·服务器·ros2
tan77º4 小时前
【Linux网络编程】网络基础
linux·服务器·网络
风口上的吱吱鼠4 小时前
Armbian 25.5.1 Noble Gnome 开启远程桌面功能
服务器·ubuntu·armbian
笑衬人心。5 小时前
Ubuntu 22.04 + MySQL 8 无密码登录问题与 root 密码重置指南
linux·mysql·ubuntu
IT成长日记5 小时前
【Docker基础】Docker数据卷管理:docker volume inspect及其参数详解
运维·docker·容器·volume·inspect
ladymorgana6 小时前
【Docker】如何设置 `wiredTigerCacheSizeGB` 和 `resources.limits.memory`
运维·docker·容器
生如夏花℡6 小时前
HarmonyOS学习记录3
学习·ubuntu·harmonyos