1. 安装docker(引擎):
(https://docs.docker.com/engine/install/ubuntu/)
Install Docker Engine on Ubuntu
To get started with Docker Engine on Ubuntu, make sure you meet the prerequisites, and then follow the installation steps.
Prerequisites
OS requirements
To install Docker Engine, you need the 64-bit version of one of these Ubuntu versions:
Ubuntu Lunar 23.04
Ubuntu Kinetic 22.10
Ubuntu Jammy 22.04 (LTS)
Ubuntu Focal 20.04 (LTS)
Ubuntu Bionic 18.04 (LTS)
Docker Engine is compatible with x86_64 (or amd64), armhf, arm64, and s390x architectures.
Uninstall old versions
Before you can install Docker Engine, you must first make sure that any conflicting packages are uninstalled.
Distro maintainers provide an unofficial distributions of Docker packages in APT. You must uninstall these packages before you can install the official version of Docker Engine.
The unofficial packages to uninstall are:
docker.io
docker-compose
docker-doc
podman-docker
Moreover, Docker Engine depends on containerd and runc. Docker Engine bundles these dependencies as one bundle: containerd.io. If you have installed the containerd or runc previously, uninstall them to avoid conflicts with the versions bundled with Docker Engine.
Run the following command to uninstall all conflicting packages:
for pkg in docker.io docker-doc docker-compose podman-docker containerd runc; do sudo apt-get remove $pkg; done
apt-get might report that you have none of these packages installed.
Images, containers, volumes, and networks stored in /var/lib/docker/ aren’t automatically removed when you uninstall Docker. If you want to start with a clean installation, and prefer to clean up any existing data, read the uninstall Docker Engine section.
Installation methods
You can install Docker Engine in different ways, depending on your needs:
Docker Engine comes bundled with Docker Desktop for Linux. This is the easiest and quickest way to get started.
Set up and install Docker Engine from Docker’s apt repository.
Install it manually and manage upgrades manually.
Use a convenience scripts. Only recommended for testing and development environments.
Install using the apt repository
Before you install Docker Engine for the first time on a new host machine, you need to set up the Docker repository. Afterward, you can install and update Docker from the repository.
Set up the repository
Update the apt package index and install packages to allow apt to use a repository over HTTPS:
sudo apt-get update
sudo apt-get install ca-certificates curl gnupg
Add Docker’s official GPG key:
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg
Use the following command to set up the repository:
echo \
"deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
"$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
Note
If you use an Ubuntu derivative distro, such as Linux Mint, you may need to use UBUNTU_CODENAME instead of VERSION_CODENAME.
Install Docker Engine
Update the apt package index:
sudo apt-get update
Install Docker Engine, containerd, and Docker Compose.
Latest
Specific version
To install the latest version, run:
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
Verify that the Docker Engine installation is successful by running the hello-world image.
sudo docker run hello-world
This command downloads a test image and runs it in a container. When the container runs, it prints a confirmation message and exits.
查看docker版本
docker version
输出以下内容:
Client: Docker Engine - Community
Version: 20.10.9
API version: 1.41
Go version: go1.16.8
Git commit: c2ea9bc
Built: Mon Oct 4 16:08:29 2021
OS/Arch: linux/amd64
Context: default
Experimental: true
Server: Docker Engine - Community
Engine:
Version: 20.10.9
API version: 1.41 (minimum version 1.12)
Go version: go1.16.8
Git commit: 79ea9d3
Built: Mon Oct 4 16:06:37 2021
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.4.11
GitCommit: 5b46e404f6b9f661a205e28d59c982d3634148f8
runc:
Version: 1.0.2
GitCommit: v1.0.2-0-g52b36a2
docker-init:
Version: 0.19.0
GitCommit: de40ad0
2.安装Nvidia Docker
curl https://get.docker.com | sh
sudo systemctl start docker
sudo systemctl enable docker
设置稳定存储库和GPG密钥:
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
在更新包列表后安装nvidia-docker2包(和依赖项)
sudo apt-get update
sudo apt-get install -y nvidia-docker2
设置默认运行时间后,重新启动Docker守护程序完成安装:
sudo systemctl restart docker
3.重要!!!(必须)
docker守护进程启动的时候,会默认赋予名字为docker的用户组读写Unix socket的权限,因此只要创建docker用户组,并将当前用户加入到docker用户组中,那么当前用户就有权限访问Unix socket了,进而也就可以执行docker相关命令
sudo groupadd docker #添加docker用户组 默认情况下好像已经被创建了
sudo gpasswd -a $USER docker #将登陆用户加入到docker用户组中
newgrp docker #更新用户组
docker ps #测试docker命令是否可以使用sudo正常使用
4.拉取 PaddlePaddle 镜像(根据paddle官网提供的命令)
nvidia-docker pull paddlepaddle/paddle:2.4.2-gpu-cuda11.7-cudnn8.4-trt8.4
输出如下:
2.4.2-gpu-cuda11.7-cudnn8.4-trt8.4: Pulling from paddlepaddle/paddle
40dd5be53814: Pulling fs layer
69761e853747: Pulling fs layer
fc447ee06ac9: Pulling fs layer
e1cfed2c8b10: Waiting
35a8d68b06ce: Waiting
7f2a3dc7d39e: Waiting
c9834390d290: Waiting
d73edacf489a: Waiting
c7971ce47315: Pulling fs layer
2da8291b2566: Pulling fs layer
aa9433893559: Waiting
fa914a76149f: Waiting
a443c08ceb94: Waiting
2f9f5dd4c70b: Pull complete
37406ea94abb: Pull complete
5aff29297311: Pull complete
4d7ae3fb3473: Pull complete
1d41e39f7395: Pull complete
cc048442b162: Pull complete
bb4f413d8b59: Pull complete
98ebfdbbea64: Pull complete
52ea893ab1d4: Pull complete
02aefb1947a7: Pull complete
6fd17bb6f094: Pull complete
2c4562ce23fd: Pull complete
c6e1729af58d: Pull complete
50e9bbaccda2: Pull complete
2ea7b26cbb13: Pull complete
3b7745091648: Pull complete
fd62cd917be7: Pull complete
5be733668b3f: Pull complete
71b603065b2a: Pull complete
db72094d1c13: Pull complete
18d85b154f03: Pull complete
49d3f45b5952: Pull complete
a421ef67ed02: Pull complete
84a204ec15dd: Pull complete
f8712675e95b: Pull complete
4d6221659c44: Pull complete
3d896a810942: Pull complete
0a7898386676: Pull complete
599deae959f2: Pull complete
761c1aec740d: Pull complete
6705817a2b30: Pull complete
9560fe759bba: Pull complete
2917473e87a2: Pull complete
2d48f565abbc: Pull complete
0865327c0f8a: Pull complete
d22ed4f9eaa3: Pull complete
ec44ef4c6197: Pull complete
Digest: sha256:ad251d0bd9ba4258f70e964d7745cc2c50dbba2c0106feaed06d3b4bd64be4de
Status: Downloaded newer image for paddlepaddle/paddle:2.4.2-gpu-cuda11.7-cudnn8.4-trt8.4
docker.io/paddlepaddle/paddle:2.4.2-gpu-cuda11.7-cudnn8.4-trt8.4
5. 构建并进入 docker 容器(根据paddle官网提供的命令)
nvidia-docker run --name paddle_docker -it -v $PWD:/paddle registry.baidubce.com/paddlepaddle/paddle:2.4.2-gpu-cuda11.7-cudnn8.4-trt8.4 /bin/bash
--name paddle_docker:设定 Docker 的名称,paddle_docker 是自己设置的名称;
-it:参数说明容器已和本机交互式运行;
-v $PWD:/paddle:指定将当前路径(PWD 变量会展开为当前路径的绝对路径)挂载到容器内部的 /paddle 目录;
registry.baidubce.com/paddlepaddle/paddle:2.4.2-gpu-cuda11.7-cudnn8.4-trt8.4,指定需要使用的 image 名称。可以通过sudo docker images命令查看镜像。
/bin/bash 是在 Docker 中要执行的命令
输出如下:
(base) uvtec@uvtec-MS-7B98:~$ sudo nvidia-docker run --name paddle_docker -it -v $PWD:/paddle registry.baidubce.com/paddlepaddle/paddle:2.4.2-gpu-cuda11.7-cudnn8.4-trt8.4 /bin/bash
λ e04f655ec148 /home
【说明:现在进入了容器的命令行提示符(/bin/bash)。在这个容器中,我们可以执行各种命令和操作。现在您可以在容器中执行您需要的操作。根据您的需求,可以进行以下操作之一:
(1)进行所需的软件安装:
您可以使用适当的包管理器(如apt、pip等)在容器中安装所需的软件或库。
(2)执行您的任务或命令:
根据您的目的,您可以在容器中运行您的特定命令或任务。例如,训练深度学习模型、运行测试脚本等。
请注意,在容器中的操作和更改不会直接影响到主机系统。容器是一个隔离的环境,具有自己的文件系统和资源。
如果您希望退出容器并返回到主机系统的命令行提示符,可以使用exit命令。】
6. Docker 删除容器步骤
sudo docker ps -a(查看现在运行的docker 容器)
结果如下图:
sudo docker stop 容器的ID(停止该容器镜像)
例如:
sudo docker rm 容器的ID(删除容器)
例如:
docker常用命令
启动(已经停止的)容器
docker start 《容器id or 容器名称》
创建并运行新的容器
docker run (选项)《镜像名称》