Docker常用操作
1. 介绍
2. 安装、配置与卸载
2.1 脚本安装
获取安装脚本并执行
curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh
2.2 命令安装
2.2.1 Ubuntu
配置安装环境
sudo apt-get install apt-transport-https ca-certificates curl gnupg-agent software-properties-common
添加阿里云的docker GPG密钥
curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
添加阿里镜像源
sudo add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
更新
sudo apt-get update
安装最新版
sudo apt-get install -y docker-ce
安装指定版(5:19.03.6~3-0~ubuntu-bionic版)
sudo apt-get install -y docker-ce=5:19.03.6~3-0~ubuntu-bionic
2.3 配置容器加速
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": [
"https://registry.cn-hangzhou.aliyuncs.com/",
"https://docker.proxy.houdemingxin.com",
"https://docker.m.daocloud.io/",
"https://huecker.io/",
"https://dockerhub.timeweb.cloud",
"https://noohub.ru/",
"https://dockerproxy.com",
"https://docker.mirrors.ustc.edu.cn",
"https://docker.nju.edu.cn",
"https://xx4bwyg2.mirror.aliyuncs.com",
"http://f1361db2.m.daocloud.io",
"https://registry.docker-cn.com",
"http://hub-mirror.c.163.com",
"https://docker.mirrors.ustc.edu.cn"
]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
2.4 添加用户到docker用户组
sudo usermod -aG docker your-user
2.5 卸载
删除安装包
sudo apt-get purge docker-ce
删除镜像、容器、配置文件等内容:
sudo rm -rf /var/lib/docker
2.6 (可选)安装nvidia-container-toolkit
官方教程:https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/latest/install-guide.html
3. 基本操作
3.1 镜像
3.1.1 创建
语法
docker build [OPTIONS] PATH | URL | -
使用DockerFile
Docker在当前路径下
docker build -t hello_word:latest .
3.1.2 导出
命令
docker save [options] images [images...]
示例
docker save -o hello_world.tar hello_world:latest
或
docker save > hello_world.tar hello_world:latest
3.1.3 导入
命令
docker load [options]
示例
docker load -i hello_world:latest
3.1.4 修改名称
命令
docker SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
示例
docker tag hello_world:latest hello:0.1
3.1.5 删除
docker rmi 镜像id 或 镜像名称:版本号
3.2 容器
3.2.1 创建
基于镜像创建容器
docker run [options] [image]
–env 映射环境变量,例如 --env CUDA_HOME=$CUDA_PATH或/usr/local/cuda
-v 映射主机路径
3.2.2 提交(commit)
将容器提交成镜像
docker commit -a "作者" -m "说明信息" 容器id或名称 保存成的镜像名
4. 常用操作
4.1 镜像
4.1.1 删除
# 删除所有<none>镜像
docker rmi $(docker images | grep "none" | awk '{print $3}')
5. 其他
许可协议:
CC BY 4.0