用Dockerfile构建MySQL镜像并实现容器启动过程中MySQL数据库系统的初始化

前一段时间就在研究用Dockerfile构建MySQL镜像并实现容器启动过程中MySQL数据库系统的初始化,但被一些无关紧要的事儿给耽误了,经过查阅《dockerfile最佳实践》及MySQL数据库的迁移与备份还原的资料,整理了操作过程,并进行了实验,确认此方法可行:
MySQL数据库系统的初始化是指MySQL安装到OS上后所经历的initinstall过程,只有经过初始化的数据库系统才能提供数据存储服务、才能被应用程序使用。
实现MySQL在解压安装过程或容器启动过程中的数据库系统的初始化,有三个思路:
1、在CentOS环境中安装配置好MySQL,使MySQL处于可对外服务状态,并对此环境进行打包制作成虚拟化的镜像包;
2、在启动容器的过程中指定MySQL的环境变量,通过环境变量和初始化shell脚本完成MySQL数据库系统的初始化;
3、通过Dockerfile指定镜像的构建过程,并提前准备好MySQL数据库系统初始化所必须的meta数据和shell脚本,这相当于对数据库系统进行迁移还原。
本次实验采用的即是思路三。因为在实践过程中,思路一相当于为特定应用做的个性化定制,思路二在操作过程中总会遇到数据库连接失败的故障,稳定性较差。

本次实验操作的环境:
CentOS 7 1804,VMware 15 Pro,4GB内存、4核心CPU,宿主OS为Windows7SP1 64Bit。

操作系统环境准备
[googlebigtable@localhost ~]$ su - root
Password:
Last login: Tue May 19 02:28:07 EDT 2020 on pts/0
[root@localhost ~]# mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.original
[root@localhost ~]# ls -F /etc/yum.repos.d/
CentOS-Base.repo.original CentOS-CR.repo CentOS-Debuginfo.repo CentOS-fasttrack.repo CentOS-Media.repo CentOS-Sources.repo CentOS-Vault.repo
[root@localhost ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo
--2020-05-19 03:00:48-- http://mirrors.163.com/.help/CentOS7-Base-163.repo
Resolving mirrors.163.com (mirrors.163.com)... 59.111.0.251
Connecting to mirrors.163.com (mirrors.163.com)|59.111.0.251|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1572 (1.5K) [application/octet-stream]
Saving to: ‘/etc/yum.repos.d/CentOS-Base.repo’

100%[==========================================================================================================>] 1,572 --.-K/s in 0s

2020-05-19 03:00:48 (460 MB/s) - ‘/etc/yum.repos.d/CentOS-Base.repo’ saved [1572/1572]

[root@localhost ~]# ls -F /etc/yum.repos.d/
CentOS-Base.repo CentOS-CR.repo CentOS-fasttrack.repo CentOS-Sources.repo
CentOS-Base.repo.original CentOS-Debuginfo.repo CentOS-Media.repo CentOS-Vault.repo
[root@localhost ~]# cat -n /etc/yum.repos.d/CentOS-Base.repo
1 # CentOS-Base.repo
2 #
3 # The mirror system uses the connecting IP address of the client and the
4 # update status of each mirror to pick mirrors that are updated to and
5 # geographically close to the client. You should use this for CentOS updates
6 # unless you are manually picking other mirrors.
7 #
8 # If the mirrorlist= does not work for you, as a fall back you can try the
9 # remarked out baseurl= line instead.
10 #
11 #
12 [base]
13 name=CentOS-$releasever - Base - 163.com
14 #mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os
15 baseurl=http://mirrors.163.com/centos/$releasever/os/$basearch/
16 gpgcheck=1
17 gpgkey=http://mirrors.163.com/centos/RPM-GPG-KEY-CentOS-7
18
19 #released updates
20 [updates]
21 name=CentOS-$releasever - Updates - 163.com
22 #mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates
23 baseurl=http://mirrors.163.com/centos/$releasever/updates/$basearch/
24 gpgcheck=1
25 gpgkey=http://mirrors.163.com/centos/RPM-GPG-KEY-CentOS-7
26
27 #additional packages that may be useful
28 [extras]
29 name=CentOS-$releasever - Extras - 163.com
30 #mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras
31 baseurl=http://mirrors.163.com/centos/$releasever/extras/$basearch/
32 gpgcheck=1
33 gpgkey=http://mirrors.163.com/centos/RPM-GPG-KEY-CentOS-7
34
35 #additional packages that extend functionality of existing packages
36 [centosplus]
37 name=CentOS-$releasever - Plus - 163.com
38 baseurl=http://mirrors.163.com/centos/$releasever/centosplus/$basearch/
39 gpgcheck=1
40 enabled=0
41 gpgkey=http://mirrors.163.com/centos/RPM-GPG-KEY-CentOS-7
[root@localhost ~]# yum clean all
Loaded plugins: fastestmirror, langpacks
Cleaning repos: base extras updates
Cleaning up everything
[root@localhost ~]# yum makecache
Loaded plugins: fastestmirror, langpacks
Determining fastest mirrors
base | 3.6 kB 00:00:00
extras | 2.9 kB 00:00:00
updates | 2.9 kB 00:00:00
(1/10): base/7/x86_64/group_gz | 153 kB 00:00:00
(2/10): base/7/x86_64/primary_db | 6.1 MB 00:00:04
.................................................................................................................................................
urw-fonts.noarch 0:2.4-16.el7

Complete!
[root@localhost ~]#
[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.8.2003 (Core)
[root@localhost ~]# hostnamectl status
Static hostname: localhost.localdomain
Icon name: computer-vm
Chassis: vm
Machine ID: b1625b44a4c448b193b2318086920ba8
Boot ID: c5894807414c44f8b7878f2412d39436
Virtualization: vmware
Operating System: CentOS Linux 7 (Core)
CPE OS Name: cpe:/o:centos:centos:7
Kernel: Linux 3.10.0-862.el7.x86_64
Architecture: x86-64
[root@localhost ~]# rpm -q centos-release
centos-release-7-8.2003.0.el7.centos.x86_64
[root@localhost ~]# cat /proc/version
Linux version 3.10.0-862.el7.x86_64 (builder@kbuilder.dev.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC) ) #1 SMP Fri Apr 20 16:44:24 UTC 2018
[root@localhost ~]# uname -a
Linux localhost.localdomain 3.10.0-862.el7.x86_64 #1 SMP Fri Apr 20 16:44:24 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
[root@localhost ~]#
[root@localhost ~]# yum -y update
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
No packages marked for update
[root@localhost ~]#
[root@localhost ~]# ls -F
anaconda-ks.cfg initial-setup-ks.cfg
[root@localhost ~]# cd /home/googlebigtable/
[root@localhost googlebigtable]#ls -F
Desktop/ Documents/ Downloads/ Music/ Pictures/ Public/ Templates/ Videos/
[root@localhost googlebigtable]# mkdir -p temp/dockerworking
[root@localhost googlebigtable]# yum install -y tree
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
..................................................................................................................................................
Installed:
tree.x86_64 0:1.6.0-10.el7

Complete!
[root@localhost googlebigtable]# tree temp/
temp/
└── dockerworking

1 directory, 0 files
[root@localhost googlebigtable]#

用shell脚本安装Docker
[root@localhost dockerworking]# curl -sSL https://get.docker.com/ | sh

Executing docker install script, commit: 26ff363bcf3b3f5a00498ac43694bf1c7d9ce16c

Remember that you will have to log out and back in for this to take effect!

WARNING: Adding a user to the "docker" group will grant the ability to run
containers which can be used to obtain root privileges on the
docker host.
Refer to https://docs.docker.com/engine/security/security/#docker-daemon-attack-surface
for more information.
[root@localhost dockerworking]#
启动docker服务
[root@localhost dockerworking]# yum list installed | grep docker
containerd.io.x86_64 1.2.13-3.2.el7 @docker-ce-stable
docker-ce.x86_64 3:19.03.9-3.el7 @docker-ce-stable
docker-ce-cli.x86_64 1:19.03.9-3.el7 @docker-ce-stable
[root@localhost dockerworking]# systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
[root@localhost dockerworking]# systemctl start docker
[root@localhost dockerworking]#
[root@localhost dockerworking]# ls -F /etc/ | grep docker
docker/
[root@localhost dockerworking]# ls -F /etc/docker/
key.json
[root@localhost dockerworking]# ttee /etc/docker/daemon.json <<-‘EOF‘

{
"registry-mirrors": ["https://alzgoonw.mirror.aliyuncs.com"]
}
EOF
{
"registry-mirrors": ["https://alzgoonw.mirror.aliyuncs.com"]
}
[root@localhost dockerworking]# cat -n /etc/docker/daemon.json
1 {
2 registry-mirrors": ["https://alzgoonw.mirror.aliyuncs.com"]
3 }
[root@localhost dockerworking]#
[root@localhost dockerworking]# systemctl daemon-reload
[root@localhost dockerworking]#systemctl restart docker
[root@localhost dockerworking]# service docker restart
Redirecting to /bin/systemctl restart docker.service
[root@localhost dockerworking]#
[root@localhost dockerworking]# ls -F
dockerubuntu/ gbtwithsshv0.tar
[root@localhost dockerworking]# cd dockerubuntu/
[root@localhost dockerubuntu]# vim Dockerfile
[root@localhost dockerubuntu]# vim setup.sh
[root@localhost dockerubuntu]# vim schema.sql
[root@localhost dockerubuntu]# vim privileges.sql
[root@localhost dockerubuntu]# ls -F
Dockerfile privileges.sql schema.sql setup.sh
[root@localhost dockerubuntu]# cat -n Dockerfile
1 FROM mysql:5.7.25
2 ENV MYSQL_ALLOW_EMPTY_PASSWORD yes
3 COPY setup.sh /mysql/setup.sh
4 COPY schema.sql /mysql/schema.sql
5 COPY privileges.sql /mysql/privileges.sql
6 CMD ["sh", "/mysql/setup.sh"]
[root@localhost dockerubuntu]# cat -n setup.sh
1 #!/bin/bash
2
3 set -e
4
5
6 echo ‘1. starting mysql...‘
7
8 service mysql start
9
10
11 echo ‘2. creating database...‘
12
13 mysql < /mysql/schema.sql
14
15 echo ‘3. setting password....‘
16
17 mysql < /mysql/privileges.sql
18
19 echo service mysql status
20
21 echo ‘4. mysql for testformysqlinit is ready...‘
22
23
24 tail -f /dev/null
[root@localhost dockerubuntu]# cat -n schema.sql
1 create database if not exists testformysqlinit;
[root@localhost dockerubuntu]# cat -n privileges.sql
1 use mysql;
2 set password for root@localhost = password(‘qwertyuiop2020‘);
3 grant all on . to root@‘%‘ identified by ‘qwertyuiop2020‘ with grant option;
4 flush privileges;
[root@localhost dockerubuntu]#
[root@localhost dockerubuntu]# docker build -t mysqlinit:mysql5725 .
Sending build context to Docker daemon 5.12kB
Step 1/6 : FROM mysql:5.7.25
5.7.25: Pulling from library/mysql
27833a3ba0a5: Pull complete
864c283b3c4b: Pull complete
cea281b2278b: Pull complete
8f856c14f5af: Pull complete
9c4f38c23b6f: Pull complete
1b810e1751b3: Pull complete
5479aaef3d30: Pull complete
1d924ec3d520: Pull complete
1ab7ae63ac60: Pull complete
08aa5f3680e9: Pull complete
a832d0a0972a: Pull complete
Digest: sha256:dba5fed182e64064b688ccd22b2f9cad4ee88608c82f8cff21e17bab8da72b81
Status: Downloaded newer image for mysql:5.7.25
---> 98455b9624a9
Step 2/6 : ENV MYSQL_ALLOW_EMPTY_PASSWORD yes
---> Running in 20c67e7a2821
Removing intermediate container 20c67e7a2821
---> 7b8f3e8b35e8
Step 3/6 : COPY setup.sh /mysql/setup.sh
---> 2eb86def7a40
Step 4/6 : COPY schema.sql /mysql/schema.sql
---> 212e9fbec90d
Step 5/6 : COPY privileges.sql /mysql/privileges.sql
---> 83160ca01bd7
Step 6/6 : CMD ["sh", "/mysql/setup.sh"]
---> Running in 67b4de01a0cf
Removing intermediate container 67b4de01a0cf
---> b43048b18ff1
Successfully built b43048b18ff1
Successfully tagged mysqlinit:mysql5725
[root@localhost dockerubuntu]#
[root@localhost dockerubuntu]# docker run --name mysqlinit -p 33060:3306 -d mysqlinit:mysql5725
5e0dfdfb6d034b8b62641b7fbf670e034cbb24486c456e63d8fbf952ec15e8e4
[root@localhost dockerubuntu]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysqlinit mysql5725 b43048b18ff1 21 minutes ago 372MB
<none> <none> fdbcd047e06b 3 hours ago 128MB
ubuntu1604 CMDtest fcdab87e5a82 4 hours ago 125MB
<none> <none> 8185bda8662c 4 hours ago 197MB
gbtwithssh v0 a371467d9077 24 hours ago 326MB
wordpress latest 675af3ca3193 4 days ago 540MB
ubuntu 16.04 005d2078bdfa 3 weeks ago 125MB
ubuntu 18.04 c3c304cb4f22 3 weeks ago 64.2MB
ubuntu 14.04 6e4f1fe62ff1 5 months ago 197MB
mysql 5.5 d404d78aa797 12 months ago 205MB
mysql 5.7.25 98455b9624a9 14 months ago 372MB
centos 7.5.1804 cf49811e3cdb 14 months ago 200MB
[root@localhost dockerubuntu]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5e0dfdfb6d03 mysqlinit:mysql5725 "docker-entrypoint.s…" 19 seconds ago Up 19 seconds 33060/tcp, 0.0.0.0:33060->3306/tcp mysqlinit
0d55d28e7556 6e4f1fe62ff1 "/bin/sh -c ‘apt-get…" 39 minutes ago Exited (100) 31 minutes ago hungry_hoover
fe69ea4fceb2 cf49811e3cdb "/bin/sh -c ‘yum -y …" 45 minutes ago Exited (1) 44 minutes ago nice_chaum
447f347b4807 fdbcd047e06b "/bin/sh -c ‘apt-get…" 2 hours ago Exited (100) About an hour ago inspiring_diffie
d5aca6ac8f01 fdbcd047e06b "/bin/sh -c ‘apt-get…" 3 hours ago Exited (100) 2 hours ago fervent_poincare
e103c446c82b fcdab87e5a82 "hostname" 4 hours ago Exited (0) 4 hours ago fervent_leakey
e2d84d1bc763 fcdab87e5a82 "hostnamectl status" 4 hours ago Created cool_bhabha
d93e878ffb47 fcdab87e5a82 "hostnamectl status" 4 hours ago Exited (1) 4 hours ago silly_moore
7d33e9e3a685 fcdab87e5a82 "/bin/date" 4 hours ago Exited (0) 4 hours ago clever_merkle
87db9e8e1ca0 fcdab87e5a82 "/bin/echo ‘I‘m excu…" 4 hours ago Exited (0) 4 hours ago stoic_benz
d64d6b812a74 8185bda8662c "/bin/echo this imag…" 4 hours ago Exited (0) 4 hours ago upbeat_sutherland
a3a6494e9354 8185bda8662c "/bin/echo hi,this i…" 4 hours ago Exited (0) 4 hours ago nifty_tereshkova
38cb9c00ee5f wordpress "docker-entrypoint.s…" 5 hours ago Up 5 hours 0.0.0.0:8080->80/tcp wordpress
c90e359d5af0 mysql:5.5 "docker-entrypoint.s…" 5 hours ago Up 5 hours 0.0.0.0:3306->3306/tcp mysql
8c6eb8f8d4ba cf49811e3cdb "/usr/sbin/init" 25 hours ago Exited (137) 16 hours ago ssh
b2dfd487a2f3 cf49811e3cdb "/usr/sbin/init" 25 hours ago Exited (137) 25 hours ago great_taussig
89e35ee3bc0e cf49811e3cdb "/bin/bash" 25 hours ago Exited (137) 25 hours ago centos7.5
[root@localhost dockerubuntu]#
[root@localhost dockerubuntu]# docker exec -it 5e0dfdfb6d03 /bin/bash
root@5e0dfdfb6d03:/# ls -F
bin/ dev/ entrypoint.sh@ home/ lib64/ mnt/ opt/ root/ sbin/ sys/ usr/
boot/ docker-entrypoint-initdb.d/ etc/ lib/ media/ mysql/ proc/ run/ srv/ tmp/ var/
root@5e0dfdfb6d03:/# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.25 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

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 user from user;
+---------------+
| user |
+---------------+
| root |
| mysql.session |
| mysql.sys |
| root |
+---------------+
4 rows in set (0.00 sec)

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| testformysqlinit |
+--------------------+
5 rows in set (0.00 sec)

mysql>select user,host from user;
+---------------+-----------+
| user | host |
+---------------+-----------+
| root | % |
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+---------------+-----------+
4 rows in set (0.00 sec)

mysql> quit;br/>Bye
root@5e0dfdfb6d03:/#
root@5e0dfdfb6d03:/# exit
exit
[root@localhost dockerubuntu]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5e0dfdfb6d03 mysqlinit:mysql5725 "docker-entrypoint.s…" 9 minutes ago Up 9 minutes 33060/tcp, 0.0.0.0:33060->3306/tcp mysqlinit
0d55d28e7556 6e4f1fe62ff1 "/bin/sh -c ‘apt-get…" 48 minutes ago Exited (100) 40 minutes ago hungry_hoover
fe69ea4fceb2 cf49811e3cdb "/bin/sh -c ‘yum -y …" 54 minutes ago Exited (1) 53 minutes ago nice_chaum
447f347b4807 fdbcd047e06b "/bin/sh -c ‘apt-get…" 2 hours ago Exited (100) About an hour ago inspiring_diffie
d5aca6ac8f01 fdbcd047e06b "/bin/sh -c ‘apt-get…" 3 hours ago Exited (100) 2 hours ago fervent_poincare
e103c446c82b fcdab87e5a82 "hostname" 4 hours ago Exited (0) 4 hours ago fervent_leakey
e2d84d1bc763 fcdab87e5a82 "hostnamectl status" 4 hours ago Created cool_bhabha
d93e878ffb47 fcdab87e5a82 "hostnamectl status" 4 hours ago Exited (1) 4 hours ago silly_moore
7d33e9e3a685 fcdab87e5a82 "/bin/date" 4 hours ago Exited (0) 4 hours ago clever_merkle
87db9e8e1ca0 fcdab87e5a82 "/bin/echo ‘I‘m excu…" 4 hours ago Exited (0) 4 hours ago stoic_benz
d64d6b812a74 8185bda8662c "/bin/echo this imag…" 4 hours ago Exited (0) 4 hours ago upbeat_sutherland
a3a6494e9354 8185bda8662c "/bin/echo hi,this i…" 4 hours ago Exited (0) 4 hours ago nifty_tereshkova
38cb9c00ee5f wordpress "docker-entrypoint.s…" 5 hours ago Up 5 hours 0.0.0.0:8080->80/tcp wordpress
c90e359d5af0 mysql:5.5 "docker-entrypoint.s…" 5 hours ago Up 5 hours 0.0.0.0:3306->3306/tcp mysql
8c6eb8f8d4ba cf49811e3cdb "/usr/sbin/init" 25 hours ago Exited (137) 16 hours ago ssh
b2dfd487a2f3 cf49811e3cdb "/usr/sbin/init" 25 hours ago Exited (137) 25 hours ago great_taussig
89e35ee3bc0e cf49811e3cdb "/bin/bash" 25 hours ago Exited (137) 25 hours ago centos7.5
[root@localhost dockerubuntu]#
[root@localhost dockerubuntu]# docker commit -a "googlebigtable" -m "mysqlwithinit" 5e0dfdfb6d03 mysql5725init:v0
sha256:3587682e3a3c76167740f21b514fdd205aa9386adf693105355b87abccabe06d
[root@localhost dockerubuntu]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql5725init v0 3587682e3a3c 11 seconds ago 372MB
mysqlinit mysql5725 b43048b18ff1 35 minutes ago 372MB
<none> <none> fdbcd047e06b 3 hours ago 128MB
ubuntu1604 CMDtest fcdab87e5a82 4 hours ago 125MB
<none> <none> 8185bda8662c 4 hours ago 197MB
gbtwithssh v0 a371467d9077 25 hours ago 326MB
wordpress latest 675af3ca3193 4 days ago 540MB
ubuntu 16.04 005d2078bdfa 3 weeks ago 125MB
ubuntu 18.04 c3c304cb4f22 3 weeks ago 64.2MB
ubuntu 14.04 6e4f1fe62ff1 5 months ago 197MB
mysql 5.5 d404d78aa797 12 months ago 205MB
mysql 5.7.25 98455b9624a9 14 months ago 372MB
centos 7.5.1804 cf49811e3cdb 14 months ago 200MB
[root@localhost dockerubuntu]# docker save -o mysql5725initv0.tar mysql5725init:v0
[root@localhost dockerubuntu]# ls -F
Dockerfile mysql5725initv0.tar privileges.sql schema.sql setup.sh
[root@localhost dockerubuntu]#

至此,本次实验结束。

孟伯,20200520

交流联系:微信 1807479153 ,QQ 1807479153

用Dockerfile构建MySQL镜像并实现容器启动过程中MySQL数据库系统的初始化

上一篇:java 操作数据库clob类型大字段


下一篇:mysql主从之主从延时监控及原因分析