一,创建目录
mkdir -p mysql/5.6
二,文件准备
注意执行脚本的权限,如果没有执行权限,构建的镜像里面也没有执行权限,导致启动失败。
my.cnf
[mysqld]
bind-address=0.0.0.0
# http://www.percona.com/blog/2008/05/31/dns-achilles-heel-mysql-installation/
skip_name_resolve
#server-id
#log-bin
mysqld_charset.cnf
[mysqld]
character_set_server=utf8
character_set_filesystem=utf8
collation-server=utf8_general_ci
init-connect='SET NAMES utf8'
init_connect='SET collation_connection = utf8_general_ci'
skip-character-set-client-handshake
import_sql.sh
#!/bin/bash
if [[ $# -ne 3 ]]; then
echo "Usage: $0 <username> <password> </path/to/sql_file.sql>"
exit 1
fi
echo "=> Starting MySQL Server"
/usr/bin/mysqld_safe > /dev/null 2>&1 &
PID=$!
RET=1
while [[ $RET -ne 0 ]]; do
echo "=> Waiting for confirmation of MySQL service startup"
sleep 5
mysql -u"$1" -p"$2" -e "status" > /dev/null 2>&1
RET=$?
done
echo " Started with PID ${PID}"
echo "=> Importing SQL file"
mysql -u"$1" -p"$2" < "$3"
echo "=> Stopping MySQL Server"
mysqladmin -u"$1" -p"$2" shutdown
echo "=> Done!"
run.sh
#!/bin/bash
set -m
set -e
VOLUME_HOME="/var/lib/mysql"
CONF_FILE="/etc/mysql/conf.d/my.cnf"
LOG="/var/log/mysql/error.log"
# Set permission of config file
chmod 644 ${CONF_FILE}
chmod 644 /etc/mysql/conf.d/mysqld_charset.cnf
StartMySQL ()
{
/usr/bin/mysqld_safe ${EXTRA_OPTS} > /dev/null 2>&1 &
# Time out in 1 minute
LOOP_LIMIT=60
for (( i=0 ; ; i++ )); do
if [ ${i} -eq ${LOOP_LIMIT} ]; then
echo "Time out. Error log is shown as below:"
tail -n 100 ${LOG}
exit 1
fi
echo "=> Waiting for confirmation of MySQL service startup, trying ${i}/${LOOP_LIMIT} …"
sleep 1
mysql -uroot -e "status" > /dev/null 2>&1 && break
done
}
CreateMySQLUser()
{
if [ "$MYSQL_PASS" = "**Random**" ]; then
unset MYSQL_PASS
fi
PASS=${MYSQL_PASS:-$(pwgen -s 12 1)}
_word=$( [ ${MYSQL_PASS} ] && echo "preset" || echo "random" )
echo "=> Creating MySQL user ${MYSQL_USER} with ${_word} password"
mysql -uroot -e "CREATE USER '${MYSQL_USER}'@'%' IDENTIFIED BY '$PASS'"
mysql -uroot -e "GRANT ALL PRIVILEGES ON *.* TO '${MYSQL_USER}'@'%' WITH GRANT OPTION"
echo "=> Done!"
echo "========================================================================"
echo "You can now connect to this MySQL Server using:"
echo ""
echo " mysql -u$MYSQL_USER -p$PASS -h<host> -P<port>"
echo ""
echo "Please remember to change the above password as soon as possible!"
echo "MySQL user 'root' has no password but only allows local connections"
echo "========================================================================"
}
OnCreateDB()
{
if [ "$ON_CREATE_DB" = "**False**" ]; then
unset ON_CREATE_DB
else
echo "Creating MySQL database ${ON_CREATE_DB}"
mysql -uroot -e "CREATE DATABASE IF NOT EXISTS ${ON_CREATE_DB};"
echo "Database created!"
fi
}
ImportSql()
{
for FILE in ${STARTUP_SQL}; do
echo "=> Importing SQL file ${FILE}"
if [ "$ON_CREATE_DB" ]; then
mysql -uroot "$ON_CREATE_DB" < "${FILE}"
else
mysql -uroot < "${FILE}"
fi
done
}
# Main
if [ ${REPLICATION_MASTER} == "**False**" ]; then
unset REPLICATION_MASTER
fi
if [ ${REPLICATION_SLAVE} == "**False**" ]; then
unset REPLICATION_SLAVE
fi
# Initialize empty data volume and create MySQL user
if [[ ! -d $VOLUME_HOME/mysql ]]; then
echo "=> An empty or uninitialized MySQL volume is detected in $VOLUME_HOME"
echo "=> Installing MySQL …"
if [ ! -f /usr/share/mysql/my-default.cnf ] ; then
cp /etc/mysql/my.cnf /usr/share/mysql/my-default.cnf
fi
mysql_install_db || exit 1
touch /var/lib/mysql/.EMPTY_DB
echo "=> Done!"
else
echo "=> Using an existing volume of MySQL"
fi
# Set MySQL REPLICATION - MASTER
if [ -n "${REPLICATION_MASTER}" ]; then
echo "=> Configuring MySQL replication as master (1/2) …"
if [ ! -f /replication_set.1 ]; then
RAND="$(date +%s | rev | cut -c 1-2)$(echo ${RANDOM})"
echo "=> Writting configuration file '${CONF_FILE}' with server-id=${RAND}"
sed -i "s/^#server-id.*/server-id = ${RAND}/" ${CONF_FILE}
sed -i "s/^#log-bin.*/log-bin = mysql-bin/" ${CONF_FILE}
touch /replication_set.1
else
echo "=> MySQL replication master already configured, skip"
fi
fi
# Set MySQL REPLICATION - SLAVE
if [ -n "${REPLICATION_SLAVE}" ]; then
echo "=> Configuring MySQL replication as slave (1/2) …"
if [ -n "${MYSQL_PORT_3306_TCP_ADDR}" ] && [ -n "${MYSQL_PORT_3306_TCP_PORT}" ]; then
if [ ! -f /replication_set.1 ]; then
RAND="$(date +%s | rev | cut -c 1-2)$(echo ${RANDOM})"
echo "=> Writting configuration file '${CONF_FILE}' with server-id=${RAND}"
sed -i "s/^#server-id.*/server-id = ${RAND}/" ${CONF_FILE}
sed -i "s/^#log-bin.*/log-bin = mysql-bin/" ${CONF_FILE}
touch /replication_set.1
else
echo "=> MySQL replication slave already configured, skip"
fi
else
echo "=> Cannot configure slave, please link it to another MySQL container with alias as 'mysql'"
exit 1
fi
fi
echo "=> Starting MySQL …"
StartMySQL
tail -F $LOG &
# Create admin user and pre create database
if [ -f /var/lib/mysql/.EMPTY_DB ]; then
echo "=> Creating admin user …"
CreateMySQLUser
OnCreateDB
rm /var/lib/mysql/.EMPTY_DB
fi
# Import Startup SQL
if [ -n "${STARTUP_SQL}" ]; then
if [ ! -f /sql_imported ]; then
echo "=> Initializing DB with ${STARTUP_SQL}"
ImportSql
touch /sql_imported
fi
fi
# Set MySQL REPLICATION - MASTER
if [ -n "${REPLICATION_MASTER}" ]; then
echo "=> Configuring MySQL replication as master (2/2) …"
if [ ! -f /replication_set.2 ]; then
echo "=> Creating a log user ${REPLICATION_USER}:${REPLICATION_PASS}"
mysql -uroot -e "CREATE USER '${REPLICATION_USER}'@'%' IDENTIFIED BY '${REPLICATION_PASS}'"
mysql -uroot -e "GRANT REPLICATION SLAVE ON *.* TO '${REPLICATION_USER}'@'%'"
mysql -uroot -e "reset master"
echo "=> Done!"
touch /replication_set.2
else
echo "=> MySQL replication master already configured, skip"
fi
fi
# Set MySQL REPLICATION - SLAVE
if [ -n "${REPLICATION_SLAVE}" ]; then
echo "=> Configuring MySQL replication as slave (2/2) …"
if [ -n "${MYSQL_PORT_3306_TCP_ADDR}" ] && [ -n "${MYSQL_PORT_3306_TCP_PORT}" ]; then
if [ ! -f /replication_set.2 ]; then
echo "=> Setting master connection info on slave"
mysql -uroot -e "CHANGE MASTER TO MASTER_HOST='${MYSQL_PORT_3306_TCP_ADDR}',MASTER_USER='${MYSQL_ENV_REPLICATION_USER}',MASTER_PASSWORD='${MYSQL_ENV_REPLICATION_PASS}',MASTER_PORT=${MYSQL_PORT_3306_TCP_PORT}, MASTER_CONNECT_RETRY=30"
mysql -uroot -e "start slave"
echo "=> Done!"
touch /replication_set.2
else
echo "=> MySQL replication slave already configured, skip"
fi
else
echo "=> Cannot configure slave, please link it to another MySQL container with alias as 'mysql'"
exit 1
fi
fi
fg
Dockerfile
FROM ubuntu:trusty
MAINTAINER Fernando Mayo <fernando@tutum.co>, Feng Honglin <hfeng@tutum.co>
# Add MySQL configuration
COPY my.cnf /etc/mysql/conf.d/my.cnf
COPY mysqld_charset.cnf /etc/mysql/conf.d/mysqld_charset.cnf
RUN apt-get update && \
apt-get -yq install mysql-server-5.6 pwgen && \
rm -rf /var/lib/apt/lists/* && \
rm /etc/mysql/conf.d/mysqld_safe_syslog.cnf && \
if [ ! -f /usr/share/mysql/my-default.cnf ] ; then cp /etc/mysql/my.cnf /usr/share/mysql/my-default.cnf; fi && \
mysql_install_db > /dev/null 2>&1 && \
touch /var/lib/mysql/.EMPTY_DB
# Add MySQL scripts
COPY import_sql.sh /import_sql.sh
COPY run.sh /run.sh
ENV MYSQL_USER=admin \
MYSQL_PASS=**Random** \
ON_CREATE_DB=**False** \
REPLICATION_MASTER=**False** \
REPLICATION_SLAVE=**False** \
REPLICATION_USER=replica \
REPLICATION_PASS=replica
# Add VOLUMEs to allow backup of config and databases
VOLUME ["/etc/mysql", "/var/lib/mysql"]
EXPOSE 3306
CMD ["/run.sh"]
docker-compose.yml
db:
image: tutum/mysql:5.6
environment:
MYSQL_PASS: "**ChangeMe**"
三,构建
docker build -t mysql mysql/5.6/
使用下面的标签也可
docker build -t hxy/mysql:5.6 mysql/5.6/ -t 仓库名/镜像名:tag
构建过程末尾出现,Successful(如出现问题,检查Dockerfile语法及目录文件书写是否正确)
Successfully built 6ae62f4234ed
Successfully tagged mysql111:5.6666
四,使用
[root@bogon ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hxy/mysql 5.6 a13c49e4455a 3 minutes ago 506MB
运行docker,并且绑定端口3306
docker run -d -p 3306:3306 hxy/mysql:5.6
第一次运行时一个admin 用户被创建,并且赋予所有的权限,和一个随机密码,通过下面的命令查看密码
docker logs <CONTAINER_ID>
类似
[root@bogon ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
67d302dc6eb5 hxy/mysql:5.6 "/run.sh" 6 seconds ago Up 4 seconds 0.0.0.0:3306->3306/tcp ecstatic_poitras
[root@bogon ~]#
[root@bogon ~]#
[root@bogon ~]# docker logs 67d302dc6eb5
=> Using an existing volume of MySQL
=> Starting MySQL …
=> Waiting for confirmation of MySQL service startup, trying 0/60 …
=> Waiting for confirmation of MySQL service startup, trying 1/60 …
=> Waiting for confirmation of MySQL service startup, trying 2/60 …
=> Creating admin user …
=> Creating MySQL user admin with random password
2018-01-05 03:13:00 917 [Note] InnoDB: 5.6.33 started; log sequence number 1626017
2018-01-05 03:13:00 917 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 55b1290a-f1c6-11e7-80c4-0242ac110002.
2018-01-05 03:13:00 917 [Note] Server hostname (bind-address): '0.0.0.0'; port: 3306
2018-01-05 03:13:00 917 [Note] - '0.0.0.0' resolves to '0.0.0.0';
2018-01-05 03:13:00 917 [Note] Server socket created on IP: '0.0.0.0'.
2018-01-05 03:13:00 917 [Warning] 'user' entry 'root@0b93885822f4' ignored in --skip-name-resolve mode.
2018-01-05 03:13:00 917 [Warning] 'proxies_priv' entry '@ root@0b93885822f4' ignored in --skip-name-resolve mode.
2018-01-05 03:13:00 917 [Note] Event Scheduler: Loaded 0 events
2018-01-05 03:13:00 917 [Note] /usr/sbin/mysqld: ready for connections.
Version: '5.6.33-0ubuntu0.14.04.1' socket: '/var/run/mysqld/mysqld.sock' port: 3306 (Ubuntu)
tail: unrecognized file system type 0x794c7630 for '/var/log/mysql/error.log'. please report this to bug-coreutils@gnu.org. reverting to polling
=> Done!
========================================================================
You can now connect to this MySQL Server using:
mysql -uadmin -p2G0YfZRlA1wo -h<host> -P<port>
Please remember to change the above password as soon as possible!
MySQL user 'root' has no password but only allows local connections
========================================================================
tail -F $LOG
指定一个密码:
docker run -d -p 3307:3306 -e MYSQL_PASS="mypass" haoxy/mysql:5.6
mysql -uadmin -p"mypass"
五,在宿主机上连接docker 中的mysql
在宿主机上安装mysql客户端(以CentOS 7 为例)
yum install mysql-server mysql-devel mysql
mysql -uadmin -pmypass -h192.168.0.169 -P3307
说明:192.168.0.169 为宿主机的IP地址,3307 是宿主机映射给容器的地址
六,推送镜像到Docker hub
1. 登陆
docker login
2.修改镜像名(仓库名斜线前面必须是Docker Hub id)
[root@bogon .docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hxy/mysql 5.6 f9fd65d0145d 2 days ago 506MB
[root@bogon .docker]# docker tag f9fd65d0145d haoxy/mysql:5.6
[root@bogon .docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
haoxy/mysql 5.6 f9fd65d0145d 2 days ago 506MB
hxy/mysql 5.6 f9fd65d0145d 2 days ago 506MB
3. 推送镜像
[root@bogon .docker]# docker push haoxy/mysql
登陆到Dcoker Hub上查看