书籍来源:《CKA/CKAD应试指南:从Docker到Kubernetes完全攻略》
一边学习一边整理老师的课程内容及实验笔记,并与大家分享,侵权即删,谢谢支持!
附上汇总贴:CKA备考实验 | 汇总_热爱编程的通信人的博客-CSDN博客
在使用mysql镜像的时候,至少需要指定一个变量MYSQL_ROOT_PASSWORD来指定root密码,其他变量,比如MYSQL_USER、MYSQL_PASSWORD、MYSQL_DATABASE这些都是可选的。
##########实操验证##########
[root@vms100 ~]# docker history hub.c.163.com/library/mysql
IMAGE CREATED CREATED BY SIZE COMMENT
9e64176cd8a2 6 years ago /bin/sh -c #(nop) CMD ["mysqld"] 0B
<missing> 6 years ago /bin/sh -c #(nop) EXPOSE 3306/tcp 0B
<missing> 6 years ago /bin/sh -c #(nop) ENTRYPOINT ["docker-entry… 0B
<missing> 6 years ago /bin/sh -c ln -s usr/local/bin/docker-entryp… 34B
<missing> 6 years ago /bin/sh -c #(nop) COPY file:5453fdbdb142b28e… 6.03kB
<missing> 6 years ago /bin/sh -c #(nop) VOLUME [/var/lib/mysql] 0B
<missing> 6 years ago /bin/sh -c sed -Ei 's/^(bind-address|log)/#&… 1.23kB
<missing> 6 years ago /bin/sh -c { echo mysql-community-server m… 240MB
<missing> 6 years ago /bin/sh -c echo "deb http://repo.mysql.com/a… 55B
<missing> 6 years ago /bin/sh -c #(nop) ENV MYSQL_VERSION=5.7.18-… 0B
<missing> 6 years ago /bin/sh -c #(nop) ENV MYSQL_MAJOR=5.7 0B
<missing> 6 years ago /bin/sh -c set -ex; key='A4A9406876FCBD3C45… 20.8kB
<missing> 6 years ago /bin/sh -c apt-get update && apt-get install… 38.7MB
<missing> 6 years ago /bin/sh -c mkdir /docker-entrypoint-initdb.d 0B
<missing> 6 years ago /bin/sh -c set -x && apt-get update && apt-… 4.58MB
<missing> 6 years ago /bin/sh -c #(nop) ENV GOSU_VERSION=1.7 0B
<missing> 6 years ago /bin/sh -c groupadd -r mysql && useradd -r -… 330kB
<missing> 6 years ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
<missing> 6 years ago /bin/sh -c #(nop) ADD file:712c48086043553b8… 123MB
[root@vms100 ~]#
可以看到使用mysql镜像创建出来的容器里运行的是mysqld。
步骤1:创建容器。
##########实操验证##########
[root@vms100 ~]# docker run -d --name=db --restart=always -e MYSQL_ROOT_PASSWORD=redhat -e MYSQL_DATABASE=blog hub.c.163.com/library/mysql
b1fff2458132d47eb89958600445dce3942071ba638f7d5d0c6cb0cd0c7b6f08
[root@vms100 ~]#
这里使用MYSQL_ROOT_PASSWORD指定了MySQL root密码为redhat,在容器里创建一个数据库,名字为blog(由选项-e MYSQL_DATABASE=blog指定)。
步骤2:做连接测试。
在物理机上用yum安装mariadb客户端,令为“yum -y install mariadb”,然后连接容器。
##########实操验证##########
[root@vms100 ~]# mysql -uroot -predhat -h172.17.0.2
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.18 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 |
+--------------------+
| information_schema |
| blog |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
MySQL [(none)]> exit
Bye
[root@vms100 ~]#
容器的IP可以通过如下命令查看到。
##########实操验证##########
[root@vms100 ~]# docker exec db ip a | grep 'inet '
inet 127.0.0.1/8 scope host lo
inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
[root@vms100 ~]#