1、Dockerfile文件
FROM pwup/centos:7
#容器是精简版,没有wget软件包,需要yum安装wget
RUN yum install -y wget
#下载并安装MySQL官方的 Yum Repository
RUN wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
RUN yum -y install mysql57-community-release-el7-10.noarch.rpm
RUN yum -y install mysql-community-server
RUN yum -y install kde-l10n-Chinese
RUN yum -y install glibc-common
#修改中文乱码
RUN localedef -c -f UTF-8 -i zh_CN zh_CN.utf8 && echo LC_ALL=zh_CN.utf8 >> /etc/profile && source /etc/profile
ADD start.sh /start.sh
ENTRYPOINT ["sh", "/start.sh"]
2、start.sh
#!/bin/bash
#初始化mysql
dir_path="/var/lib/mysql"
#环境变量没有配置
if [ ! $PASSWORD ]; then
PASSWORD="123456"
fi
if [ ! "$(ls -A $dir_path)" ]; then
mysqld --initialize --explicit_defaults_for_timestamp --user=mysql
#启动mysql
mysqld --daemonize --user=mysql --pid-file=/var/run/mysqld/mysqld.pid
#初始root密码
random_password=`grep "A temporary password is generated" /var/log/mysqld.log|awk '{print $NF}'|tail -n 1`
#连接到mysql后,修改密码
mysqladmin -u root -p$random_password password $PASSWORD
mysqladmin -uroot -p$PASSWORD shutdown
mysqld --user=mysql --pid-file=/var/run/mysqld/mysqld.pid
else
mysqld --user=mysql --pid-file=/var/run/mysqld/mysqld.pid
fi
3、创建镜像
docker build . -t mysql5.7:v1
4、运行容器
docker run -d --name mysql5.7 -e PASSWORD=6543210 mysql5.7:v1
5、登入验证查看
docker exec -it mysql5.7 /bin/bash
mysql -uroot -p6543210