目录
- 1. 各版本平台支持情况
- 2. 在Ubuntu系统下安装docker
- 3. 常见报错
- 4. Docker的镜像源修改
- 5. Docker目录修改
- 6. 在CentOS系统下安装docker
1. 各版本平台支持情况
(1)平台支持情况如下:
- Server 版本
- 桌面版本
2. 在Ubuntu系统下安装docker
(1)确定 CPU。可以看到我们的是 X86_64,是支持的,如果是 arm 一般会显示aarch64:
[xiaomaker@xiaomaker-virtual-machine:docket]$ uname -a
Linux xiaomaker-virtual-machine 6.8.0-49-generic #49~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Wed Nov 6 17:42:15 UTC 2 x86_64 x86_64 x86_64 GNU/Linux
(2)确定操作系统版本,本次我们使用的是Ubuntu 22.04.1:
[xiaomaker@xiaomaker-virtual-machine:docket]$ cat /etc/*release*
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=22.04
DISTRIB_CODENAME=jammy
DISTRIB_DESCRIPTION="Ubuntu 22.04.1 LTS"
PRETTY_NAME="Ubuntu 22.04.1 LTS"
NAME="Ubuntu"
VERSION_ID="22.04"
VERSION="22.04.1 LTS (Jammy Jellyfish)"
VERSION_CODENAME=jammy
ID=ubuntu
ID_LIKE=debian
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
UBUNTU_CODENAME=jammy
(3)卸载旧版本,如果是新购买的云服务器是没有的,比如输入 docker 并没有这个命令,就不需要卸载:
[xiaomaker@xiaomaker-virtual-machine:docket]$ sudo apt-get remove docker docker-engine
docker.io containerd runc
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package docker-engine
(4)卸载历史版本:
#卸载软件
sudo apt-get purge docker-ce docker-ce-cli containerd.io dockerbuildx-plugin docker-compose-plugin docker-ce-rootless-extras -y
#删除目录
sudo rm -rf /var/lib/docker
sudo rm -rf /var/lib/containerd
(5)配置 docker 下载源:
#curl 命令安装
sudo apt install curl -y
#创建 gpg key 目录
sudo mkdir -m 0755 -p /etc/apt/keyrings
#下载 gpg key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor --yes -o /etc/apt/keyrings/docker.gpg
echo \
"deb [arch=$(dpkg --print-architecture) signedby=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
(6)安装:
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y
(7)查看安装结果:
(8)自动启动配置:
#配置加载
sudo systemctl daemon-reload
#启动服务
sudo systemctl start docker
#开启启动
sudo systemctl enable docker
#查看服务状态
sudo systemctl status docker
(9)检查安装结果查看版本:
[xiaomaker@xiaomaker-virtual-machine:docket]$ docker version
Client: Docker Engine - Community
Version: 23.0.1
API version: 1.42
Go version: go1.19.5
Git commit: a5ee5b1
Built: Thu Feb 9 19:46:56 2023
OS/Arch: linux/amd64
Context: default
Server: Docker Engine - Community
Engine:
Version: 23.0.1
API version: 1.42 (minimum version 1.12)
Go version: go1.19.5
Git commit: bc3805a
Built: Thu Feb 9 19:46:56 2023
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.6.18
GitCommit: 2456e983eb9e37e47538f59ea18f2043c9a73640
runc:
Version: 1.1.4
GitCommit: v1.1.4-0-g5fd4c4d
docker-init:
Version: 0.19.0
GitCommit: de40ad0
(9)更详细查看 docker 信息:
[xiaomaker@xiaomaker-virtual-machine:docket]$ docker info
(10)执行 hello-world 可以看到 Hello from Docker,表面 docker 服务正常:
[xiaomaker@xiaomaker-virtual-machine:docket]$ sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest:
sha256:6e8b6f026e0b9c419ea0fd02d3905dd0952ad1feea67543f525c73a0a790fefb
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/
3. 常见报错
(1)报错截图:
(2)查看日志:
#journalctl 是操作系统日志查看命令
#-e 表示从末尾看
#-u 表示看哪个系统组件的,我们的组件是 docker
journalctl -eu docker
(3)查看报错:
(4)执行如下命令修复:
systemctl daemon-reload
systemctl start docker
4. Docker的镜像源修改
(1)对于使用 systemd 的系统(Ubuntu 16.04+、 Debian 8+、 CentOS 7), 在配置文件/etc/docker/daemon.json 中加入:
{
"registry-mirrors": ["https://docker.mirrors.ustc.edu.cn/"]
}
(2)重新启动 dockerd:
sudo systemctl restart docker
(3)参考链接:https://mirrors.ustc.edu.cn/help/dockerhub.html。
5. Docker目录修改
(1)Docker 默认的安装目录为/var/lib/docker,这里面会存放很多很多镜像,所以我们在安装的时候需要考虑这个目录的空间,有三种解决方案。
- 将/var/lib/docker 挂载到一个大的磁盘,这种一般我们能控制挂载目录,像腾讯云这种云厂商在安装 K8s 的节点的时候提供了挂载选项,可以直接挂载这个目录过去。
- 安装之前挂载一个大的磁盘,然后创建一个软链接到/var/lib/docker,这样就自动安装到我们空间比较大的磁盘了。
- 安装了 docker,然后发现忘了配置这个目录,我们需要修改 docker 的配置文件。
#假定我们磁盘的大的目录为 /data
mkdir -p /data/var/lib/docker
# 编辑配置文件
vi /etc/docker/daemon.json
# 输入下面的 json
{
"data-root": "/data/var/lib/docker"
}
# 加载配置
sudo systemctl daemon-reload
# 重启 docker
sudo systemctl restart docker
#查看 docker 状态
sudo systemctl status docker
(2)修改前在/var/lib/docker目录下:
(3)修改后在/data/var/lib/docker目录下:
6. 在CentOS系统下安装docker
(1)卸载旧版本:
sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
(2)卸载历史版本:
#删除机器上的包
sudo yum remove docker-ce docker-ce-cli containerd.io dockerbuildx-plugin docker-compose-plugin docker-ce-rootless-extras
#执行卸载
sudo rm -rf /var/lib/docker
sudo rm -rf /var/lib/containerd
(3)配置仓库:
[xiaomaker@centos1 ~]$ ll /etc/yum.repos.d/
total 40
-rw-r--r--. 1 root root 1664 Nov 23 2020 CentOS-Base.repo
-rw-r--r--. 1 root root 1309 Nov 23 2020 CentOS-CR.repo
-rw-r--r--. 1 root root 649 Nov 23 2020 CentOS-Debuginfo.repo
-rw-r--r--. 1 root root 314 Nov 23 2020 CentOS-fasttrack.repo
-rw-r--r--. 1 root root 630 Nov 23 2020 CentOS-Media.repo
-rw-r--r--. 1 root root 1331 Nov 23 2020 CentOS-Sources.repo
-rw-r--r--. 1 root root 8515 Nov 23 2020 CentOS-Vault.repo
-rw-r--r--. 1 root root 616 Nov 23 2020 CentOS-x86_64-kernel.repo
[xiaomaker@centos1 ~]$ sudo yum install -y yum-utils
[xiaomaker@centos1 ~]$ sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
Loaded plugins: fastestmirror adding repo from: https://download.docker.com/linux/centos/dockerce.repo
grabbing file https://download.docker.com/linux/centos/dockerce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo
[xiaomaker@centos1 ~]$ ll /etc/yum.repos.d/
total 44
-rw-r--r--. 1 root root 1664 Nov 23 2020 CentOS-Base.repo
-rw-r--r--. 1 root root 1309 Nov 23 2020 CentOS-CR.repo
-rw-r--r--. 1 root root 649 Nov 23 2020 CentOS-Debuginfo.repo
-rw-r--r--. 1 root root 314 Nov 23 2020 CentOS-fasttrack.repo
-rw-r--r--. 1 root root 630 Nov 23 2020 CentOS-Media.repo
-rw-r--r--. 1 root root 1331 Nov 23 2020 CentOS-Sources.repo
-rw-r--r--. 1 root root 8515 Nov 23 2020 CentOS-Vault.repo
-rw-r--r--. 1 root root 616 Nov 23 2020 CentOS-x86_64-kernel.repo
-rw-r--r--. 1 root root 1919 Apr 5 07:45 docker-ce.repo
# 配置使用国内源
[xiaomaker@centos1 ~]$ sed -i 's@//download.docker.com@//mirrors.ustc.edu.cn/docker-ce@g' /etc/yum.repos.d/docker-ce.repo
(4)安装最新版本:
sudo yum install -y docker-ce docker-ce-cli containerd.io dockerbuildx-plugin docker-compose-plugin
(5)启动 docker:
#配置加载
sudo systemctl daemon-reload
#启动服务
sudo systemctl start docker
#开启启动
sudo systemctl enable docker
#查看服务状态
sudo systemctl status docker