一、安装docker
二、获取镜像
三、创建容器
四、安装NVIDIA Container TOOLKIT
五、管理docker存储路径
六、使用过程中遇到的错误
以下是Ubuntu18.04安装步骤,其他系统安装可以参照官网
$ sudo apt-get update
$ sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
software-properties-common
$ curl -fsSL https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
# 官方源
# $ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
$ sudo add-apt-repository \
"deb [arch=amd64] https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu \
$(lsb_release -cs) \
stable"
# 官方源
# $ sudo add-apt-repository \
# "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
# $(lsb_release -cs) \
# stable"
$ sudo apt-get update
$ sudo apt-get install docker-ce
$ sudo service docker start
$ sudo groupadd docker // 建立 docker 用户组
$ sudo usermod -aG docker $USER //将当前用户加入 docker 组
$ docker run hello-world
dockerfile
文件自己编写dockerfile
文件,根据该文件生成镜像
docker build --build-arg USER_ID=$UID -t 镜像名:版本号 .
dockerhub
直接从dockerhub上拉去镜像
docker pull 镜像名:版本号
docker run --privileged --net=host --gpus all -it --shm-size=8gb --env="DISPLAY" --volume="/tmp/.X11-unix:/tmp/.X11-unix:rw" --name=topdetectron detectron2:v0
# Centos 需添加/sbin/init 不然systemctl没法用
docker run --privileged --net=host -it --shm-size=8gb --env="DISPLAY" --volume="/tmp/.X11-unix:/tmp/.X11-unix:rw" --name=topdetectron detectron2:v0 /sbin/init
如果容器创建时网络模式为
--net=host
,则容器和宿主机共享network namespace
,无需添加端口映射, 只要在ssh中设置好不和主机冲突的端口号即可,否则下面两步都需要执行。
apt-get install openssh-server
apt-get install openssh-client
vim /etc/ssh/sshd_config
# 修改下面内容
PubkeyAuthentication yes
PermitRootLogin yes
PasswordAuthentication yes
# Ubuntu18.04
/etc/init.d/ssh restart
# Centos7
systemctl start sshd
假设需要将某个容器的端口22映射到宿主机的端口1022
# 查看运行的container
docker container ls
# 关闭container
docker container stop [container id or name]
# 停止docker服务
sudo systemctl stop docker
# 进入容器配置文件夹
vim /var/lib/docker/containers/
# 选择对应的container id后,修改hostconfig.json 和 config.v2.json两个文件
hostconfig.json
# 修改PortBindings内容
"PortBindings":{"22/tcp":[{"HostIp":"","HostPort":"1022"}]}
config.v2.json
# 在"Cofig":{}中添加"ExposedPorts"
"ExposedPort":{"22/tcp":{}}
启动docker服务,进入容器,重启ssh服务
启动容器:
docker container start 容器名/ID
进入容器:docker exec -it 容器名/ID
查看所有容器:docker ps -a
查看运行中容器:docker container ls
删除容器:docker rm 容器名/ID
新建一个安装脚本nvidia-container.sh
脚本内容为
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
测试显卡是否可用
docker run -it --rm --gpus all ubuntu nvidia-smi
Ubuntu18.04中docker中容器和镜像默认存储在/var/lib/docker
下(可用df -h
命令查看),时间一长很容易把存储空间占满,这里将默认路径更改为/data
目录下。
sudo cp -r /var/lib/docker /data/
sudo mv /var/lib/docker /var/lib/docker.bak
sudo ln -s /data/docker /var/lib/docker
rm -rf /var/lib/docker.bak
gtk failed to load module canberra-gtk-module
sudo apt install libcanberra-gtk-module libcanberra-gtk3-module
LIBDBUSMENU-GLIB-WARNING: Unable to get session bus
sudo apt-get install dbus-x11
原文:https://www.cnblogs.com/xiaxuexiaoab/p/14587353.html