本文主要描述使用docker构建percona容器,并根据官方的percona镜像构建自己需要Dockerfile文件
注意:使用docker,系统至少是CentOS6.5以上。
一、安装docker软件
1.添加epel源
# rpm -ivhhttp://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
2.安装docker程序,主程序是docker-io
# yum install –y docker-io
二、percona官方镜像导入并运行容器。
1、要将官方制作好的percona镜像pull进来(这个过程比较慢)
搜索官方的percona镜像仓库
docker search percona
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
percona Percona Server is a fork of the MySQL rela... 19 [OK]
paulczar/percona-galera 6 [OK]
klevo/percona Percona MySQL server supporting replicatio... 1 [OK]
nicescale/percona-mysql Percona MySQL for NiceScale, support conne... 1 [OK]
wangyuntao/percona 0 [OK]
freshbooks/percona 0 [OK]
playlist/percona 0 [OK]
tyjak/percona-sphinxse percona 5.5 with sphinxse 0 [OK]
assembla/percona 0 [OK]
internavenue/centos-percona 0 [OK]
lagotto/percona 0 [OK]
mkaag/percona 0 [OK]
inviqa/percona 0 [OK]
vixns/percona-cluster 0 [OK]
servicerocket/docker-percona-with-backup 0 [OK]
strapdocker/percona 0 [OK]
psoutham/percona-tk 0 [OK]
rocketgraph/docker-percona-node 0 [OK]
vincentserpoul/percona Percona 5.6 with external storage on anoth... 0
syslogic/docker-percona-galera 0 [OK]
jmdejager/docker-percona-galera 0 [OK]
bryanlatten/percona 0 [OK]
kverma/percona Fork of official percona p_w_picpath but with lo... 0 [OK]
graemej/percona Percona server for use in CI environments. 0 [OK]
erickbrower/percona 0 [OK]
找到官方仓库并导入
docker pull percona:5.6
注:我这里导入是的我需要的5.6版本,一般直接导入percona即可。
注:官方percona是基于debian:wheezy镜像制作的。
2、查看自己最新pull的镜像可以使用
docker p_w_picpaths
3、现在就可以生成新的容器了
docker -d -eMYSQL_ROOT_PASSWORD=“密码”-P percona:5.6
注:-d 以daemon的方式在后台运行
-e 可以设置mysql的管理密码
-P 将容器内部的3306端口映射到宿主机的随机端口
-p ip:hostPort:containerPort |ip::containerPort | hostPort:containerPort | containerPort 指定宿主机的端口
-v /host:/container 可以指定容器使用的目录
二、构建自己需要的Dockerfile文件
由于官方的percona里面的配置文件不符合自身需求,所以要用自己的my.cnf文件构建新的镜像。
1.到官网下载percona的Dockerfile以及相关配置文件,根据官网的文件进行修改
Docker官方仓库:https://registry.hub.docker.com/
搜索percona下载官方的percona文件
注:Dockerfile是构建percona的文件
docker-entrypoint.sh是容器启动时运行的脚本
2.创建docker目录,方便管理自己的Dockerfile
# mkdir /root/docker
将这两个文件放到docker目录下,另外还有自己配置好的my.cnf也放入此目录下
3.修改Dockerfile和docker-entrypoint.sh文件
注:***字体是修改部分
Dockerfile文件:
# vim:set ft=dockerfile:
FROM debian:wheezy
# add our user and group firstto make sure their IDs get assigned consistently, regardless of whateverdependencies get added
RUN groupadd -r mysql&& useradd -r -g mysql mysql
RUN apt-key adv --keyserverha.pool.sks-keyservers.net --recv-keys 430BDF5C56E7C94E848EE60C1C4CBDCDCD2EFD2A
RUN echo 'debhttp://repo.percona.com/apt wheezy main' > /etc/apt/sources.list.d/percona.list
ENV PERCONA_MAJOR 5.6
ENV PERCONA_VERSION5.6.24-72.2-1.wheezy
RUN apt-get update \
&& apt-get install -y \
percona-server-server-$PERCONA_MAJOR=$PERCONA_VERSION \
&& rm -rf /var/lib/apt/lists/* \
&& rm -rf /var/lib/mysql \
&& mkdir /var/lib/mysql \
&& sed -ri 's/^(bind-address|skip-networking)/;\1/'/etc/mysql/my.cnf
VOLUME /var/lib/mysql
COPY docker-entrypoint.sh /
COPY my.cnf /
ENTRYPOINT["/docker-entrypoint.sh"]
EXPOSE 3306
CMD ["mysqld"]
docker-entrypoint.sh文件:
将自己的配置文件替换原本的配置文件,并创建配置文件里面相应的目录
#!/bin/bash
set -e
mv /etc/mysql/my.cnf /root/
mv /my.cnf /etc/mysql/
mkdir -p /var/log/mysql/{slowlog,errorlog,generallog,binlog,relaylog}
chown -R mysql.mysql/var/log/mysql/{slowlog,errorlog,generallog,binlog,relaylog}
if [ "${1:0:1}" ='-' ]; then
set -- mysqld "$@"
fi
if [ "$1" = 'mysqld']; then
# read DATADIR from the MySQL config
DATADIR="$("$@" --verbose --help 2>/dev/null |awk '$1 == "datadir" { print $2; exit }')"
if [ ! -d "$DATADIR/mysql" ]; then
if [ -z "$MYSQL_ROOT_PASSWORD" -a -z"$MYSQL_ALLOW_EMPTY_PASSWORD" ]; then
echo >&2 'error: database is uninitialized andMYSQL_ROOT_PASSWORD not set'
echo >&2 ' Did you forget to add -e MYSQL_ROOT_PASSWORD=... ?'
exit 1
fi
echo 'Running mysql_install_db ...'
mysql_install_db --datadir="$DATADIR"
echo 'Finished mysql_install_db'
# These statements _must_ be on individual lines, and _must_end with
# semicolons (no line breaks or comments are permitted).
# TODO proper SQL escaping on ALL the things D:
tempSqlFile='/tmp/mysql-first-time.sql'
cat > "$tempSqlFile" <<-EOSQL
DELETE FROM mysql.user ;
CREATE USER 'root'@'%' IDENTIFIED BY'${MYSQL_ROOT_PASSWORD}' ;
GRANT ALL ON *.* TO 'root'@'%' WITH GRANT OPTION ;
DROP DATABASE IF EXISTS test ;
EOSQL
if [ "$MYSQL_DATABASE" ]; then
echo "CREATE DATABASE IF NOT EXISTS\`$MYSQL_DATABASE\` ;" >> "$tempSqlFile"
fi
if [ "$MYSQL_USER" -a "$MYSQL_PASSWORD" ];then
echo "CREATE USER '$MYSQL_USER'@'%' IDENTIFIED BY'$MYSQL_PASSWORD' ;" >> "$tempSqlFile"
if [ "$MYSQL_DATABASE" ]; then
echo "GRANT ALL ON \`$MYSQL_DATABASE\`.* TO'$MYSQL_USER'@'%' ;" >> "$tempSqlFile"
fi
fi
echo 'FLUSH PRIVILEGES ;' >> "$tempSqlFile"
set -- "$@" --init-file="$tempSqlFile"
fi
chown -R mysql:mysql "$DATADIR"
fi
exec "$@"
4.构建镜像
# docker build –t my-percona5.6 /root/docker
-t:指定镜像名称
5.运行容器
# docker run -d –e MYSQL_ROOT_PASSWORD=“密码” –P my-percona5.6
转载于:https://blog.51cto.com/67263992/1657941