一. Dockerfile
FROM mysql:5.7
MAINTAINER 潘志勇 <sd4015700@126.com>
LABEL Description="基于mysql:5.7做成的org.pzy/mysql5.7:1.0" Version="1.0"
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
解决问题
- 在官方mysql5.7的基础上,将系统时区修改为东八区
二.打造自己的启动脚本
当前启动脚本会自动创建backend网络, 并将当前启动的容器加入到backend网络中
# 重复执行start命令并不会重复启动
echo "mysql5.7容器操作脚本,用于容器的启动,停止,删除以及状态查看!"
containerId=`docker ps -a | grep "mysql57" | grep "Up" | awk '{print $1}'`
if [ "$1" = "start" ]; then
containerId=`docker network ls | grep backend | awk '{print $1}'`
if [ -z "$containerId" ]; then
# 说明backend网络不存在,创建backend网络
echo "自动创建backend网络!"
docker network create backend
fi
containerId=`docker ps -a | grep "mysql57" | grep "Up" | awk '{print $1}'`
if [ -z "$containerId" ]; then
# 容器id为空,说明mysql未启动,或已停止
echo "容器mysql57状态: "`docker ps -a | grep "mysql57"`
containerId=`docker ps -a | grep "mysql57" | grep "Exited" | awk '{print $1}'`
if [ -n "$containerId" ]; then
# 容器id非空, 说明容器已停止
echo "重启容器: mysql57"
docker start mysql57
else
# 容器id不为空, 说明容器未启动
echo "启动容器: mysql57"
docker run -d --name mysql57 --net=backend -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root -v /Users/pan/docker/volume/mysql/data:/var/lib/mysql -v /Users/pan/docker/volume/mysql/conf:/etc/mysql/conf.d org.pzy/mysql5.7:1.0
fi
fi
containerId=`docker ps -a | grep "mysql57" | grep "Up" | awk '{print $1}'`
if [ -n "$containerId" ]; then
echo "容器mysql57已启动,并已加入backend网络!"
echo "使用[/Users/pan/docker/volume/mysql/mysql/data]作为数据目录!"
echo "使用[/Users/pan/docker/volume/mysql/mysql/conf]作为配置文件目录!"
echo "mysql端口为: 3306, 账号为:root, 密码为:root"
else
echo "容器mysql57启动异常! 请使用[docker logs]命令容器名称查看日志"
fi
echo "容器mysql57状态: "`docker ps -a | grep "mysql57"`
elif [ "$1" = "stop" ]; then
if [ -n "$containerId" ]; then
# 容器哦id非空, 表示容器已启动,执行容器停止命令
echo "停止mysql57!"
docker stop mysql57
containerId=`docker ps -a | grep "mysql57" | grep "Exited" | awk '{print $1}'`
if [ -z "$containerId" ]; then
# 容器id为空, 表示容器未成功停止
echo "容器mysql57停止失败! 请使用[docker logs]命令查看具体日志!"
else
echo "容器mysql57已停止!"
fi
echo "容器mysql57状态: "`docker ps -a | grep "mysql57"`
else
echo "容器mysql57已停止!"
echo "容器mysql57状态: "`docker ps -a | grep "mysql57"`
fi
elif [ "$1" = "status" ]; then
if [ -z "$containerId" ]; then
echo "容器mysql57未启动!"
else
echo "容器mysql57已启动!"
fi
echo "容器mysql57状态: "`docker ps -a | grep "mysql57"`
elif [ "$1" = "remove" ]; then
containerId=`docker ps -a | grep "mysql57" | awk '{print $1}'`
if [ -n "$containerId" ]; then
echo "删除容器mysql57!"
docker stop mysql57
docker rm mysql57
containerId=`docker ps -a | grep "mysql57" | awk '{print $1}'`
if [ -n "$containerId" ]; then
echo "容器mysql57删除失败!请使用[docker logs]命令查看详细日志!"
else
echo "容器mysql57已删除!"
fi
else
echo "容器mysql57已删除!"
fi
else
echo "正确使用脚本的方式:"
echo "./mysql start #启动mysql57,多次执行该命令并不会重复启动mysql57,请放心使用!"
echo "./mysql stop #停止mysql57"
echo "./mysql status #查看mysql57状态"
echo "./mysql remove #删除mysql57状态,无论mysql57处于什么状态!"
echo "./mysql help #查看脚本使用方式!"
fi
三. 附注说明
启动脚本请根据自己的实际需要修改之后再使用.
网友评论