# 环境要求

我们使用的是 CentOS 7 (64-bit)

目前,CentOS 仅发行版本中的内核支持 Docker。

需要 CentOS 7 或 8 的维护版本。不支持或测试存档版本,并且 Linux 版本 3.10.0+,包括 3.10.0, 要求系统为64位。

查看自己的内核

[root@lswwdapldn ~]# uname -r
3.10.0-1160.62.1.el7.x86_64
1
2

查看版本信息

[root@lswwdapldn ~]# cat /etc/os-release
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"

CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  • centos-extras 库必须启用。默认情况下启用此存储库。
  • overlay2 推荐使用存储驱动。

# 安装Docker

官网安装参考手册:https://docs.docker.com/engine/install/centos/ (opens new window)

# 1、安装相关环境

yum安装gcc相关环境

[root@lswwdapldn /]# yum -y install gcc
[root@lswwdapldn /]# yum -y install gcc-c++ 
1
2

输入以下命令来安装sudo

yum install sudo
1

# 2、卸载旧版本

旧版本的 Docker 被称为 dockerdocker-engine。如果安装了这些,请卸载它们以及相关的依赖项:

sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine
1
2
3
4
5
6
7
8

如果yum报告没有安装这些软件包,那也没关系。只是说明曾经没安装过,不影响任何东西。

卸载docker时,存储在/var/lib/doker/中的映像、容器、卷和网络不会自动删除。

# 3、安装需要的软件包

安装所需的软件包。yum-utils 提供了yum-config-manager ,并且 device mapper 存储驱动程序需要 device-mapper-persistent-data 和 lvm2。

在开始安装Docker之前,使用以下命令更新现有软件包:

sudo yum update
sudo yum upgrade
1
2
[root@lswwdapldn /]# sudo yum install -y yum-utils
1

使用以下命令来设置稳定的仓库。

1、使用官方源地址(比较慢)

$ sudo yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo
1
2
3

2、阿里云(国内、推荐使用)

$ sudo yum-config-manager \
    --add-repo \
    http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
1
2
3

3、清华大学源(国内)

$ sudo yum-config-manager \
    --add-repo \
    https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo
1
2
3

阿里云仓库也是定期从官网仓库进行更新引用,两者仓库量无太大区别,如果配置了阿里云仓库,也要配置阿里云镜像加速,即下载速度源由官网改为阿里云。(这个下面会有介绍)

# 4、安装Docker CE

安装最新版本的 Docker Engine-Community 和 containerd,或者转到下一步安装特定版本

$ sudo yum -y install docker-ce docker-ce-cli containerd.io docker-compose-plugin
1

如果希望自己手动确认一些安装条件,则把 -y 去掉。

Docker 安装完默认未启动。并且已经创建好 docker 用户组,但该用户组下没有用户。

如果安装有问题,请检查一下是否是yum版本太低,更新一下。

# 更新 yum
yum update

# CentOS 7 更新索引
yum makecache fast

# CentOS 8 更新索引
yum makecache
1
2
3
4
5
6
7
8

# 1、安装特定版本的Docker Engine

安装docker的另一种方式,指定安装的版本。

以下命令列出并排序您存储库中可用的版本。此示例按版本号(从高到低)对结果进行排序。

[root@lswwdapldn /]# yum list docker-ce --showduplicates | sort -r

docker-ce.x86_64            3:23.0.1-1.el7                     docker-ce-stable 
docker-ce.x86_64            3:23.0.1-1.el7                     @docker-ce-stable
docker-ce.x86_64            3:23.0.0-1.el7                     docker-ce-stable 
docker-ce.x86_64            3:20.10.9-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:20.10.8-3.el7                    docker-ce-stable 
1
2
3
4
5
6
7

返回的列表取决于启用了哪些存储库,并且特定于您的 CentOS 版本(e17 就是 CentOS 7.x 版本,如果是 CentOS 8.2,则是 e18

通过其完整的软件包名称安装特定版本,该软件包名称是软件包名称(docker-ce)加上版本字符串(第二列),从第一个冒号(:)一直到第一个连字符,并用连字符(-)分隔。例如:docker-ce-18.09.1

sudo yum install docker-ce-<version> docker-ce-cli-<version> containerd.io

# 例子
sudo yum install docker-ce-18.09.1 docker-ce-cli-18.09.1 containerd.io
1
2
3
4

此命令会安装 Docker,但不会启动 Docker。它还会创建一个 docker组 ,但是,默认情况下它不会向该组添加任何用户。

# 5、启动Docker

安装完之后,输入以下命令启动。

sudo systemctl start docker
1

测试是否安装成功

# 执行该命令
[root@lswwdapldn /]# sudo systemctl status docker
# 执行结果,出现 active (running) 代表启动成功
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
   Active: active (running) since Thu 2023-02-23 22:38:19 CST; 2 weeks 6 days ago
     Docs: https://docs.docker.com
 Main PID: 9089 (dockerd)
    Tasks: 49
   Memory: 282.7M
   CGroup: /system.slice/docker.service
1
2
3
4
5
6
7
8
9
10
11

查看docker信息

[root@lswwdapldn /]# docker info
Client:
 Context:    default
 Debug Mode: false
 Plugins:
  buildx: Docker Buildx (Docker Inc.)
    Version:  v0.10.2
    Path:     /usr/libexec/docker/cli-plugins/docker-buildx
  compose: Docker Compose (Docker Inc.)
    Version:  v2.16.0
    Path:     /usr/libexec/docker/cli-plugins/docker-compose
  scan: Docker Scan (Docker Inc.)
    Version:  v0.23.0
    Path:     /usr/libexec/docker/cli-plugins/docker-scan

Server:
 Containers: 5
  Running: 4
  Paused: 0
  Stopped: 1
 Images: 7
 Server Version: 23.0.1
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Using metacopy: false
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Cgroup Version: 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

查看docker版本

[root@lswwdapldn /]# 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:51:00 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:48:42 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

# 6、其他命令

# 查看 docker 的状态
systemctl status docker

# 重启 docker
systemctl restart docker

# 设置 docker 服务开启自启动
systemctl enable docker
1
2
3
4
5
6
7
8

# 7、卸载

# 删除安装包:卸载 Docker Engine、CLI 和 Containerd 包
sudo yum remove docker-ce docker-ce-cli containerd.io

# 删除镜像、容器、配置文件等内容
sudo rm -rf /var/lib/docker
sudo rm -rf /var/lib/containerd
1
2
3
4
5
6

# 镜像加速器配置

镜像的拉取现在都是从**DockerHub (opens new window)** 上拉取,应该会比较慢,所以我们可以配置国内的镜像加速器。

Docker 官方和国内很多云服务商都提供了国内加速器服务,比如:

  • 阿里云的加速器:https://help.aliyun.com/document_detail/60750.html
  • 网易加速器:http://hub-mirror.c.163.com
  • Docker 官方中国加速器:https://registry.docker-cn.com
  • ustc 的镜像:https://docker.mirrors.ustc.edu.cn

# 1、配置网易加速器(不推荐)

对于使用 systemd 的系统(CentOS 7.x 以上),配置是在 /etc/docker/下的 daemon.json(如果没有docker文件请新建该文件)

# 创建文件夹
[root@lswwdapldn ~]# sudo mkdir -p /etc/docker
1
2

daemon.json等我们输入执行的命令后会自动生成,无需自己创建。

sudo tee /etc/docker/daemon.json <<-'EOF'
	{
		"registry-mirrors": ["https://registry.docker-cn.com"]
	}
EOF

例如:
# 复制过来回车就好了,然后看docker下有没有json文件生成
[root@lswwdapldn docker]# sudo tee /etc/docker/daemon.json <<-'EOF'
    {
       "registry-mirrors": ["https://23redj54.mirror.aliyuncs.com"]
    }
 EOF

1
2
3
4
5
6
7
8
9
10
11
12
13
14

然后重启docker服务

# 1、重新加载配置文件
sudo systemctl daemon-reload

#2、重新启动docker
sudo systemctl restart docker
1
2
3
4
5

# 2、配置阿里云镜像加速器(推荐)

访问阿里云登录自己的账号查看 docker 镜像加速服务:https://cr.console.aliyun.com/cn-shenzhen/instances/mirrors (opens new window)

image268d2b7813d901aa.png

接下来的步骤和配置网易的基本一致,上图中也给出了配置的方法。

# 创建文件夹
[root@lswwdapldn ~]# sudo mkdir -p /etc/docker

# 然后运行该命令
sudo tee /etc/docker/daemon.json <<-'EOF'
	{
		"registry-mirrors": ["你的加速器地址"]
	}
EOF
1
2
3
4
5
6
7
8
9

之后重新加载配置文件,重启服务即可。

然后再次查看docker配置的镜像加速器是否生效Registry Mirrors:

[root@lswwdapldn ~]# docker info
Client:
 Context:    default
 Debug Mode: false
......
 CPUs: 1
 Total Memory: 1.883GiB
 Name: lswwdapldn
 ID: d5429ea9-4dd4-4591-b2d2-ff531915c8d0
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 Registry: https://index.docker.io/v1/
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
  # 加速器
 Registry Mirrors:
  https://90uqvj74.mirror.aliyuncs.com/
 Live Restore Enabled: false

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

# 拉取镜像

先拉取一个案例进行初次尝试一下docker的神奇。那必然是hello-word镜像

# 输入以下命令
docker pull hello-world:latest
1
2
# 返回信息
[root@lswwdapldn ~]# docker pull hello-world:latest
latest: Pulling from library/hello-world
2db29710123e: Pull complete 
Digest: sha256:2498fce14358aa50ead0cc6c19990fc6ff866ce72aeb5546e1d59caac3d0d60f
Status: Downloaded newer image for hello-world:latest
docker.io/library/hello-world:latest
1
2
3
4
5
6
7

拉取完成之后,我们查看一下本地的仓库是否有这镜像

# 查看镜像的命令
docker images

# 返回信息
[root@lswwdapldn ~]# docker images
hello-world        latest    feb5d9fea6a5   17 months ago    13.3kB
1
2
3
4
5
6

运行这个镜像的实例,即容器

# 执行该命令
docker run hello-world

# 返回信息
[root@lswwdapldn ~]# docker run hello-world

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/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

注意, 如果你在没有镜像的时候,直接docker run hello-world也是可以的;它会先检查本地是否有这个镜像,没有的话会先从指定仓库中拉取。

进入到hello-word容器中

命令:

docker exec -it '容器名称' bash
#或
docker exec -it '容器ID' bash
1
2
3

先查看容器

# 查看当前运行的容器
[root@lswwdapldn ~]# docker ps
7b426daa3070   mysql:8.0.27       "docker-entrypoint.s…"   3 weeks ago         Up 3 weeks         0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   mysql

# 进入到容器中
[root@lswwdapldn ~]# docker exec -it 7b426daa3070 bash
root@7b426daa3070:/# ls
bin   dev			  entrypoint.sh  home  lib64  mnt  proc  run   srv  tmp  var
boot  docker-entrypoint-initdb.d  etc		 lib   media  opt  root  sbin  sys  usr
1
2
3
4
5
6
7
8
9

通过这个,我们可以看到容器里的东西和我们系统中的root文件基本差不多。

# 总结

这一章我们学习了安装docker,了解了如何在Linux上安装docker环境,并且学习了一些docker命令,还学习了如何配置镜像加速器,拉取镜像并且运行容器等操作。总结一点还是要多练习,结合实际去学习才能学习的更快有效。

# 参考文章

  • https://www.runoob.com/docker/docker-hello-world.html
  • https://www.cnblogs.com/0x00000/p/9158982.html#%E4%B8%80docker%E4%BB%8B%E7%BB%8D
  • https://notes.youngkbt.cn/docker/introduce/
  • https://www.pdai.tech/md/devops/docker/docker-02-basic.html
最近更新: 1/8/2024, 3:36:54 PM
Copyright © - 码上言   |