警告:切勿在没有配置 Docker APT 源的情况下直接使用 apt 命令安装 Docker.
Docker 支持以下版本的 Ubuntu 操作系统:
sudo apt-get remove docker \
docker-engine \
docker.io
由于 apt 源使用 HTTPS 以确保软件下载过程中不被篡改。因此,我们首先需要添加使用 HTTPS 传输的软件包以及 CA 证书。
sudo apt-get update
sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
gnupg \
lsb-release
为了确认所下载软件包的合法性,需要添加软件源的 GPG
密钥。
curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
然后,我们需要向 sources.list
中添加 Docker 软件源
echo \
"deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://mirrors.aliyun.com/docker-ce/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
更新 apt
软件包缓存,并安装 docker-ce
:
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io
sudo systemctl enable docker
sudo systemctl start docker
默认情况下,docker 命令会使用 Unix socket 与 Docker 引擎通讯。而只有 root 用户和 docker 组的用户才可以访问 Docker 引擎的 Unix socket。出于安全考虑,一般 Linux 系统上不会直接使用 root 用户。因此,更好地做法是将需要使用 docker 的用户加入 docker 用户组。
建立docker组:
sudo groupadd docker
将当前用户加入 docker 组:
sudo usermod -aG docker $USER
退出当前终端并重新登录,进行如下测试。
docker run --rm hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
b8dfde127a29: Pull complete
Digest: sha256:308866a43596e83578c7dfa15e27a73011bdd402185a84c5cd7f32a88b501a24
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
若能正常输出以上信息,则说明安装成功。
参考:
国内从 Docker Hub 拉取镜像有时会遇到困难,此时可以配置镜像加速器。国内很多云服务商都提供了国内加速器服务。由于镜像服务可能出现宕机,建议同时配置多个镜像。各个镜像站测试结果请到 docker-practice/docker-registry-cn-mirror-test 查看。
请首先执行以下命令,查看是否在 docker.service 文件中配置过镜像地址。
systemctl cat docker | grep '\-\-registry\-mirror'
如果该命令有输出,那么请执行 systemctl cat docker
查看 ExecStart=
出现的位置,修改对应的文件内容去掉 --registry-mirror
参数及其值,并按接下来的步骤进行配置。
/etc/docker/daemon.json
中写入如下内容(如果文件不存在请新建该文件):{
"registry-mirrors": [
"https://hub-mirror.c.163.com",
"https://mirror.baidubce.com"
]
}
sudo systemctl daemon-reload
sudo systemctl restart docker
docker info
,如果从结果中看到了如下内容,说明配置成功。Registry Mirrors:
https://hub-mirror.c.163.com/
参考:
安装的是nvidia-docker2
设置stable
仓库和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 daemon
sudo systemctl restart docker
测试是否安装成功
sudo docker run --rm --gpus all nvidia/cuda:11.0-base nvidia-smi
如果在终端输出了如下内容,表示安装成功。
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 450.51.06 Driver Version: 450.51.06 CUDA Version: 11.0 |
|-------------------------------+----------------------+----------------------+
| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|===============================+======================+======================|
| 0 Tesla T4 On | 00000000:00:1E.0 Off | 0 |
| N/A 34C P8 9W / 70W | 0MiB / 15109MiB | 0% Default |
| | | N/A |
+-------------------------------+----------------------+----------------------+
+-----------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=============================================================================|
| No running processes found |
+-----------------------------------------------------------------------------+
参考:
docker pull bvlc/caffe:gpu
nvidia-docker run -it bvlc/caffe:gpu /bin/bash #这一步会进入docker!
启动一个docker
https://yeasy.gitbook.io/docker_practice/image/pull
进入容器
https://yeasy.gitbook.io/docker_practice/container/attach_exec
docker删除镜像
https://www.jianshu.com/p/2bb5eca8d04c
清华源
https://mirrors.tuna.tsinghua.edu.cn/help/ubuntu/
八、配置容器内环境
容器后台运行退出命令:CTRL+p+q
后台容器重新进入命令:docker exec -it [container id] /bin/bash