G021-CON-CKA-DOC-01 急速入门,构建你的第一个 docker 学习环境
Posted CloudCS
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了G021-CON-CKA-DOC-01 急速入门,构建你的第一个 docker 学习环境相关的知识,希望对你有一定的参考价值。
1 您需要了解
- OS 版本 :CentOS 7.8 ,您可参考
G016
一文完成系统安装 - 因要在线安装软件包及下载镜像,故系统需要连通外部网络
-
您可以点击文章左上方
目录
按钮来显示文章层次结构,以便有更好的浏览体验 - 相关文章
- G016-OS-LIN-CENT-01 CentOS 7.8.2003 安装
2 环境配置
2.1 配置在线YUM源
[root@cloudcs ~]# cat /etc/redhat-release
CentOS Linux release 7.8.2003 (Core)
[root@cloudcs ~]# ls /etc/yum.repos.d/
CentOS-Base.repo epel.repo kubernetes.repo
2.1.1 CentOS 基础源
- 将以下内容拷贝并粘贴至
/etc/yum.repos.d/CentOS-Base.repo
文件
[base]
name=CentOS-$releasever - Base - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos-vault/7.8.2003/os/$basearch/
# http://mirrors.aliyuncs.com/centos/$releasever/os/$basearch/
# http://mirrors.cloud.aliyuncs.com/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos-vault/RPM-GPG-KEY-CentOS-7
#released updates
[updates]
name=CentOS-$releasever - Updates - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos-vault/7.8.2003/updates/$basearch/
# http://mirrors.aliyuncs.com/centos/$releasever/updates/$basearch/
# http://mirrors.cloud.aliyuncs.com/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos-vault/RPM-GPG-KEY-CentOS-7
#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos-vault/7.8.2003/extras/$basearch/
# http://mirrors.aliyuncs.com/centos/$releasever/extras/$basearch/
# http://mirrors.cloud.aliyuncs.com/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos-vault/RPM-GPG-KEY-CentOS-7
#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos-vault/7.8.2003/centosplus/$basearch/
# http://mirrors.aliyuncs.com/centos/$releasever/centosplus/$basearch/
# http://mirrors.cloud.aliyuncs.com/centos/$releasever/centosplus/$basearch/
gpgcheck=1
enabled=1
gpgkey=http://mirrors.aliyun.com/centos-vault/RPM-GPG-KEY-CentOS-7
#contrib - packages by Centos Users
[contrib]
name=CentOS-$releasever - Contrib - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos-vault/7.8.2003/contrib/$basearch/
# http://mirrors.aliyuncs.com/centos/$releasever/contrib/$basearch/
# http://mirrors.cloud.aliyuncs.com/centos/$releasever/contrib/$basearch/
gpgcheck=1
enabled=0
gpgkey=http://mirrors.aliyun.com/centos-vault/RPM-GPG-KEY-CentOS-7
2.1.2 Epel 拓展源
- 将以下内容拷贝并粘贴至
/etc/yum.repos.d/epel.repo
文件
[epel]
name=Extra Packages for Enterprise Linux 7 - $basearch
baseurl=http://mirrors.aliyun.com/epel/7/$basearch
failovermethod=priority
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
[epel-debuginfo]
name=Extra Packages for Enterprise Linux 7 - $basearch - Debug
baseurl=http://mirrors.aliyun.com/epel/7/$basearch/debug
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
gpgcheck=0
[epel-source]
name=Extra Packages for Enterprise Linux 7 - $basearch - Source
baseurl=http://mirrors.aliyun.com/epel/7/SRPMS
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
gpgcheck=0
2.1.3 Kubernetes源
- 将以下内容拷贝并粘贴至
/etc/yum.repos.d/kubernetes.repo
文件
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=1
repo_gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
2.1.4 显示可用仓库
[root@cloudcs ~]# yum repolist all
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
base | 3.6 kB 00:00:00
centosplus | 2.9 kB 00:00:00
epel | 4.7 kB 00:00:00
extras | 2.9 kB 00:00:00
kubernetes | 1.4 kB 00:00:00
updates | 2.9 kB 00:00:00
(1/9): epel/x86_64/group_gz | 96 kB 00:00:00
(2/9): epel/x86_64/updateinfo | 1.0 MB 00:00:00
(3/9): base/x86_64/group_gz | 153 kB 00:00:00
(4/9): extras/x86_64/primary_db | 206 kB 00:00:00
(5/9): kubernetes/primary | 108 kB 00:00:00
(6/9): epel/x86_64/primary_db | 7.0 MB 00:00:00
(7/9): centosplus/x86_64/primary_db | 2.3 MB 00:00:00
(8/9): updates/x86_64/primary_db | 4.5 MB 00:00:00
(9/9): base/x86_64/primary_db | 6.1 MB 00:00:02
kubernetes 797/797
repo id repo name status
base/x86_64 CentOS-7 - Base - mirrors.aliyun.com enabled: 10,070
centosplus/x86_64 CentOS-7 - Plus - mirrors.aliyun.com enabled: 69
contrib/x86_64 CentOS-7 - Contrib - mirrors.aliyun.com disabled
epel/x86_64 Extra Packages for Enterprise Linux 7 - x86_64 enabled: 13,756
epel-debuginfo/x86_64 Extra Packages for Enterprise Linux 7 - x86_64 - Debug disabled
epel-source Extra Packages for Enterprise Linux 7 - x86_64 - Source disabled
extras/x86_64 CentOS-7 - Extras - mirrors.aliyun.com enabled: 413
kubernetes Kubernetes enabled: 797
updates/x86_64 CentOS-7 - Updates - mirrors.aliyun.com enabled: 1,134
repolist: 26,239
2.2 安装启动 Docker
[root@cloudcs ~]# yum install -y docker
[root@cloudcs ~]# systemctl enable docker --now
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
2.3 (可选)命令自动补全
- 如果系统为最小化安装(minimal),可执行以下操作实现命令自动补全
[root@cloudcs ~]# yum install bash-completion
[root@cloudcs ~]# source /usr/share/bash-completion/bash_completion
2.4 (可选)配置加速功能
- 配置加速
[root@cloudcs ~]# vim /etc/docker/daemon.json
[root@cloudcs ~]# cat /etc/docker/daemon.json
"registry-mirrors": ["https://cc2d8woc.mirror.aliyuncs.com"]
- 重启服务
[root@cloudcs ~]# systemctl restart docker
3 基础操作
3.1 镜像操作
3.1.1 镜像下载
- 使用
pull
关键字进行镜像的拉取。
[root@cloudcs ~]# docker pull mysql
Using default tag: latest
Trying to pull repository docker.io/library/mysql ...
latest: Pulling from docker.io/library/mysql
72a69066d2fe: Pull complete
93619dbc5b36: Pull complete
99da31dd6142: Pull complete
626033c43d70: Pull complete
37d5d7efb64e: Pull complete
ac563158d721: Pull complete
d2ba16033dad: Pull complete
688ba7d5c01a: Pull complete
00e060b6d11d: Pull complete
1c04857f594f: Pull complete
4d7cfa90e6ea: Pull complete
e0431212d27d: Pull complete
Digest: sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709
Status: Downloaded newer image for docker.io/mysql:latest
3.1.2 镜像查询
[root@cloudcs ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/mysql latest 3218b38490ce 4 months ago 516 MB
3.1.3 镜像重命名
- 使用
tag
关键字进行重命名操作,查询镜像会显示两个条目,它们的Image ID
是一样的,都指向同一底层块数据。
[root@cloudcs ~]# docker tag docker.io/mysql mysql
[root@cloudcs ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/mysql latest 3218b38490ce 4 months ago 516 MB
mysql latest 3218b38490ce 4 months ago 516 MB
[root@cloudcs ~]# docker tag docker.io/mysql:latest mysql
3.1.4 镜像删除
- 使用
rmi
关键字删除一个或多个镜像,因同一个镜像有两个别名,这里只删除了其中一个,镜像依然存在。
[root@cloudcs ~]# docker rmi docker.io/mysql
Untagged: docker.io/mysql:latest
Untagged: docker.io/mysql@sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709
[root@cloudcs ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql latest 3218b38490ce 5 months ago 516 MB
- 再次执行删除,将彻底从底层删除镜像。
[root@cloudcs ~]# docker rmi mysql
Untagged: mysql:latest
Deleted: sha256:3218b38490cec8d31976a40b92e09d61377359eab878db49f025e5d464367f3b
Deleted: sha256:aa81ca46575069829fe1b3c654d9e8feb43b4373932159fe2cad1ac13524a2f5
Deleted: sha256:0558823b9fbe967ea6d7174999be3cc9250b3423036370dc1a6888168cbd224d
Deleted: sha256:a46013db1d31231a0e1bac7eeda5ad4786dea0b1773927b45f92ea352a6d7ff9
Deleted: sha256:af161a47bb22852e9e3caf39f1dcd590b64bb8fae54315f9c2e7dc35b025e4e3
Deleted: sha256:feff1495e6982a7e91edc59b96ea74fd80e03674d92c7ec8a502b417268822ff
Deleted: sha256:8805862fcb6ef9deb32d4218e9e6377f35fb351a8be7abafdf1da358b2b287ba
Deleted: sha256:872d2f24c4c64a6795e86958fde075a273c35c82815f0a5025cce41edfef50c7
Deleted: sha256:6fdb3143b79e1be7181d32748dd9d4a845056dfe16ee4c827410e0edef5ad3da
Deleted: sha256:b0527c827c82a8f8f37f706fcb86c420819bb7d707a8de7b664b9ca491c96838
Deleted: sha256:75147f61f29796d6528486d8b1f9fb5d122709ea35620f8ffcea0e0ad2ab0cd0
Deleted: sha256:2938c71ddf01643685879bf182b626f0a53b1356138ef73c40496182e84548aa
Deleted: sha256:ad6b69b549193f81b039a1d478bc896f6e460c77c1849a4374ab95f9a3d2cea2
[root@cloudcs ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
3.1.5 镜像保存(导出)
- 下载镜像
[root@cloudcs ~]# docker pull mysql
[root@cloudcs ~]# docker pull centos
- 查看镜像
[root@cloudcs ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/mysql latest 3218b38490ce 5 months ago 516 MB
docker.io/centos latest 5d0da3dc9764 8 months ago 231 MB
- 重命名镜像
[root@cloudcs ~]# docker tag docker.io/mysql mysql
[root@cloudcs ~]# docker tag docker.io/centos centos
[root@cloudcs ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql latest 3218b38490ce 5 months ago 516 MB
docker.io/mysql latest 3218b38490ce 5 months ago 516 MB
centos latest 5d0da3dc9764 8 months ago 231 MB
docker.io/centos latest 5d0da3dc9764 8 months ago 231 MB
- 如果想把镜像导出(保存),可使用
save
关键字对单个或多个镜像进行保存。
[root@cloudcs ~]# docker save mysql centos > /root/all_images.tar
3.1.6 镜像载入(导入)
- 导入镜像,使用 load 关键字,并配合
-i 或 --input
输入参数进行镜像导入
[root@cloudcs ~]# docker rmi mysql centos docker.io/mysql docker.io/centos
[root@cloudcs ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
[root@cloudcs ~]# docker load --input /root/all_images.tar
74ddd0ec08fa: Loading layer [==================================================>] 238.6 MB/238.6 MB
Loaded image: centos:latest
ad6b69b54919: Loading layer [==================================================>] 72.55 MB/72.55 MB
fba7b131c5c3: Loading layer [==================================================>] 338.4 kB/338.4 kB
0798f2528e83: Loading layer [==================================================>] 9.556 MB/9.556 MB
a0c2a050fee2: Loading layer [==================================================>] 4.202 MB/4.202 MB
d7a777f6c3a4: Loading layer [==================================================>] 2.048 kB/2.048 kB
0d17fee8db40: Loading layer [==================================================>] 53.77 MB/53.77 MB
aad27784b762: Loading layer [==================================================>] 5.632 kB/5.632 kB
1d1f48e448f9: Loading layer [==================================================>] 3.584 kB/3.584 kB
c654c2afcbba: Loading layer [==================================================>] 380.5 MB/380.5 MB
118fee5d988a: Loading layer [==================================================>] 5.632 kB/5.632 kB
fc8a043a3c75: Loading layer [==================================================>] 17.92 kB/17.92 kB
d67a9f3f6569: Loading layer [==================================================>] 1.536 kB/1.536 kB
Loaded image: mysql:latest
[root@cloudcs ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql latest 3218b38490ce 5 months ago 516 MB
centos latest 5d0da3dc9764 8 months ago 231 MB
3.2 容器操作
3.2.1 容器创建
- 根据现有镜像
centos
,使用run
关键字创建一个容器。
[root@cloudcs ~]# docker run centos
3.2.2 容器查询
- 使用
ps
关键字来查询容器,默认只查询正在运行的容器,带上-a
或--all
参数来查询所有容器。
[root@cloudcs ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5c293384cd60 centos "/bin/bash" 9 hours ago Exited (0) 9 hours ago nifty_bartik
3.2.3 容器删除
- 使用
rm
关键字删除容器,如果容器没有关闭,带上-f
或--force
强制删除。
[root@cloudcs ~]# docker rm -f nifty_bartik
- 在容器
container id
不重复的情况下,删除容器可直接指定container id
,例如
[root@cloudcs ~]# docker rm -f 5
5
[root@cloudcs ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
3.2.4 容器重命名
- 创建容器可指定
--name
来修改容器默认名称。
[root@cloudcs ~]# docker run --name os centos
[root@cloudcs ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b838cfdeef48 centos "/bin/bash" 26 seconds ago Exited (0) 25 seconds ago os
3.2.5 容器生命周期
3.2.5.1 容器挂载终端
- 细心的你可能发现了,我们创建的容器,
STATUS
状态显示为Exited
退出的,没有正常运行。我们尝试启动容器。
[root@cloudcs ~]# docker start os
os
[root@cloudcs ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b838cfdeef48 centos "/bin/bash" 26 seconds ago Exited (0) 25 seconds ago os
- 执行启动容器命令后,再次查看容器状态,依然是
Extied
。原因是因为该容器要运行一个COMMAND
为/bin/bash
的 shell 进程,在 Linux 里面 shell 进程必须由终端来承载,才能实现命令行的交互,而我们创建的容器,里面的 shell 进程没有终端来承载,所以容器在创建的一瞬间,里面的 shell 进程就结束了,一旦进程结束,该容器也会结束。状态才会显示退出。接下来尝试在创建容器的时候使用-t
或--tty
来为其挂载一个终端。
[root@cloudcs ~]# docker rm -f b
b
[root@cloudcs ~]# docker run --tty --name os centos
[root@c6e3ff261338 /]#
- 创建容器后,直接进入到了容器里面,但是无法执行操作,原因是因为终端有了,但没有交互功能。
3.2.5.2 容器交互
- 尝试在创建容器的时候使用
-i
或--interactive
来实现终端交互。
[root@cloudcs ~]# docker rm -f os
os
[root@cloudcs ~]# docker run --tty --interactive --name os centos
[root@586ef21a7f5c /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
- 创建容器后,直接进入到了容器里面,可以执行命令行操作。我们尝试退出容器。
[root@586ef21a7f5c ~]# exit
exit
[root@cloudcs ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
586ef21a7f5c centos "/bin/bash" 2 minutes ago Exited (0) 8 seconds ago os
- 当退出容器的时候,查询容器状态,再一次显示
Exited
过期的。只不过这一次是我们主动退出的,意味着主动结束了终端承载的shell
进程,因此容器也会随着shell
进程的结束而自动关闭。我们可以尝试手工启动容器并进入容器。
3.2.5.3 容器启动关闭与重启
- 当容器状态为
Up
的时候,意味着容器启动。
[root@cloudcs ~]# docker start os
os
[root@cloudcs ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
586ef21a7f5c centos "/bin/bash" 20 seconds ago Up 2 seconds
[root@cloudcs ~]# docker stop os
os
[root@cloudcs ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
586ef21a7f5c centos "/bin/bash" 57 seconds ago Exited (0) 3 seconds ago os
[root@cloudcs ~]# docker restart os
os
[root@cloudcs ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
586ef21a7f5c centos "/bin/bash" 2 minutes ago Up About a minute
3.2.5.4 容器附加(进入)
- 通过
attach
关键字进入容器。
[root@cloudcs ~]# docker attach os
[root@586ef21a7f5c /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
- 退出容器,再次查看状态。你会发现,该容器随着退出,又一次被关闭了。如果想再一次进入容器,只需按照
3.2.5.3
和3.2.5.4
小节执行启动及附加命令即可。但,这样操作不觉得很麻烦吗?
[root@586ef21a7f5c /]# exit
exit
[root@cloudcs ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
586ef21a7f5c centos "/bin/bash" 12 minutes ago Exited (0) 9 seconds ago os
3.2.5.5 容器持续运行
- 默认情况下,随着容器的退出,容器会自动关闭,如何实现容器持续运行呢?可以在创建的时候添加
--restart always
关键字。
[root@cloudcs ~]# docker rm -f os
os
[root@cloudcs ~]# docker run --tty --interactive --restart always --name os centos
[root@ff14c5a6c8e1 /]# exit
exit
[root@cloudcs ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ff14c5a6c8e1 centos "/bin/bash" 7 seconds ago Up 1 second os
- 这样,创建容器后,退出容器,查看状态,显示
Up
启动。实现随时进入随时退出。
[root@cloudcs ~]# docker attach os
[root@ff14c5a6c8e1 /]# exit
exit
[root@cloudcs ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ff14c5a6c8e1 centos "/bin/bash" 3 minutes ago Up 2 seconds os
3.2.5.6 容器后端分离
- 以上所有操作有个细节,就是当我们创建容器后,它会自动进入到容器里面,有时这种情况并不是我们想要的。理想状态是,当创建容器后,什么时候进入,什么时候退出,由自己来决定,这时候可以在创建容器的时候带上
-d
或--detach
关键字让容器在后端运行。
[root@cloudcs ~]# docker run --tty --interactive --detach --restart always --name os centos
bdea1b934861b340305f71f6047cc46a53015a381c024ce11ab8329d6d8356ed
[root@cloudcs ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bdea1b934861 centos "/bin/bash" 6 seconds ago Up 5 seconds os
[root@cloudcs ~]# docker run -tid --restart always --name os centos
b82e61388708b06816376c3ffc7faa4264949b450a103a2eeabf4e2591d55609
[root@cloudcs ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b82e61388708 centos "/bin/bash" 4 seconds ago Up 3 seconds
3.2.5.7 容器参数变量
- 对于某些镜像,在创建容器的时候,必须指定一些参数,这时候可以使用
-e
或--env
来设置其环境变量。
[root@cloudcs ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql latest 3218b38490ce 5 months ago 516 MB
centos latest 5d0da3dc9764 8 months ago 231 MB
[root@cloudcs ~]# docker run -tid --restart always --name os -e name=cloudcs -e age=23 centos
c11fe046e5f7bf2516f29874e01d3d1520c66f646c529f5e40ee503fdd74b331
[root@cloudcs ~]# docker attach os
[root@c11fe046e5f7 /]# echo $name
cloudcs
[root@c11fe046e5f7 /]# echo $age
23
4 综合练习(创建MySQL容器)
4.1 创建MySQL容器
[root@cloudcs ~]# docker run -tid --restart always --name db -e MYSQL_ROOT_PASSWORD=cloudcs -e MYSQL_USER=cloudcs -e MYSQL_PASSWORD=cloudcs -e MYSQL_DATABASE=cloudcs mysql
02a73d281a3d96711544e126b658122ea85ad6636743751c9b39eebaa20e7057
[root@cloudcs ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
02a73d281a3d mysql "docker-entrypoint..." 12 minutes ago Up 12 minutes 3306/tcp, 33060/tcp db
c11fe046e5f7 centos "/bin/bash" 22 minutes ago Up 18 minutes os
4.2 查看容器内容
- 获取
ip
[root@cloudcs ~]# docker inspect db
"SandboxKey": "/var/run/docker/netns/a59910af2579",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "ca58feb5034f0ac9c82c5d11dc48f466b6d85ba412429a11526737214dfb9964",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:03",
4.3 登录MySQL
[root@cloudcs ~]# mysql -u root -pcloudcs -h 172.17.0.3
ERROR 2059 (HY000): Authentication plugin caching_sha2_password cannot be loaded: /usr/lib64/mysql/plugin/caching_sha2_password.so: cannot open shared object file: No such file or directory
- 修改认证方式
[root@cloudcs ~]# docker exec -ti db /bin/bash
root@02a73d281a3d:/# mysql -u root -pcloudcs
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \\g.
Your MySQL connection id is 9
Server version: 8.0.27 MySQL Community Server - GPL
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type help; or \\h for help. Type \\c to clear the current input statement.
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select host,user,plugin from user where user=root;
+-----------+------+-----------------------+
| host | user | plugin |
+-----------+------+-----------------------+
| % | root | caching_sha2_password |
| localhost | root | caching_sha2_password |
+-----------+------+-----------------------+
2 rows in set (0.00 sec)
mysql> alter user root@% identified with mysql_native_password by cloudcs;
Query OK, 0 rows affected (0.00 sec)
mysql> select host,user,plugin from user where user=root;
+-----------+------+-----------------------+
| host | user | plugin |
+-----------+------+-----------------------+
| % | root | mysql_native_password |
| localhost | root | caching_sha2_password |
+-----------+------+-----------------------+
2 rows in set (0.00 sec)
mysql> exit
Bye
root@02a73d281a3d:/# exit
exit
- 再次登录
[root@cloudcs ~]# mysql -u root -pcloudcs -h 172.17.0.3
Welcome to the MariaDB monitor. Commands end with ; or \\g.
Your MySQL connection id is 17
Server version: 8.0.27 MySQL Community Server - GPL
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type help; or \\h for help. Type \\c to clear the current input statement.
MySQL [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| cloudcs |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
- END
以上是关于G021-CON-CKA-DOC-01 急速入门,构建你的第一个 docker 学习环境的主要内容,如果未能解决你的问题,请参考以下文章