Docker 一步一步 搭建开源 CMDB平台 第二步: DockerFile 构建mariadb
Posted Linux Dream
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Docker 一步一步 搭建开源 CMDB平台 第二步: DockerFile 构建mariadb相关的知识,希望对你有一定的参考价值。
整理了一下文档 今天来构建mariadb
主机还是 centos 172.16.0.200 构建第二个images
直接shell.sh 完成
#!/bin/bash
echo "----------------demo------------------"
#--------------------new--mysql--dockerfile----------
if [[ ! -e ./Dockerfile ]]; then
cat > ./Dockerfile <<\\EOF
FROM docker.io/centos
MAINTAINER Mrchen <237356573@qq.com>
ENV DATA_DIR /var/lib/mysql
# Install Mariadb
RUN yum install -y mariadb mariadb-server
RUN yum clean all
ADD mysqld_charset.cnf /etc/my.cnf.d/
COPY scripts /scripts
RUN chmod +x /scripts/start
EXPOSE 3306
VOLUME ["/var/lib/mysql"]
CMD ["/bin/bash"]
ENTRYPOINT ["/scripts/start"]
EOF
else
echo "Docker 已经存在"
fi
#---------------new-mysql.config--------------------
if [[ ! -e ./mysqld_charset.cnf ]]; then
cat > ./mysqld_charset.cnf <<\\EOF
[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\'
EOF
else
echo "mysqld.config 已存在!"
fi
#--------------new-start脚本---------------------
if [[ ! -d ./scripts ]]; then
mkdir ./scripts && touch ./scripts/firstrun
cat > ./scripts/start <<\\EOF
#!/bin/bash
ROOT_USER=${ROOT_USER:-admin}
ROOT_PASS=${ROOT_PASS:-237356573}
#DB user password
SQL_DB=${SQL_DB:-opsmanage}
DB_USER=${DB_USER:-chen}
DB_PASS=${DB_PASS:-237356573}
MARIADB_NEW=true
run_maria() {
# First install mariadb
if [[ ! -d ${DATA_DIR}/mysql ]]; then
echo "===> MariaDB not install..."
echo "===> Initializing maria database... "
mysql_install_db --user=mysql --ldata=${DATA_DIR}
echo "===> System databases initialized..."
# Start mariadb
/usr/bin/mysqld_safe --user mysql > /dev/null 2>&1 &
echo "===> Waiting for MariaDB to start..."
STA=1
while [[ STA -ne 0 ]]; do
printf "."
sleep 3
mysql -uroot -e "status" > /dev/null 2>&1
STA=$?
done
echo "===> Start OK..."
# 1. Create a localhost-only admin account
mysql -u root -e "CREATE USER \'$ROOT_USER\'@\'%\' IDENTIFIED BY \'$ROOT_PASS\'"
mysql -u root -e "CREATE USER \'$ROOT_USER\'@\'localhost\' IDENTIFIED BY \'$ROOT_PASS\'"
mysql -u root -e "CREATE USER \'$ROOT_USER\'@\'127.0.0.1\' IDENTIFIED BY \'$ROOT_PASS\'"
mysql -u root -e "GRANT ALL PRIVILEGES ON *.* TO \'$DB_USER\'@\'%\' WITH GRANT OPTION"
mysql -u root -e "CREATE DATABASE $SQL_DB"
mysql -u root -e "grant all privileges on $SQL_DB.* TO \'$DB_USER\'@\'%\' identified by \'$DB_PASS\'"
echo "===> Create localhost completed..."
# shutdown mariadb to wait for supervisor
mysqladmin -u root shutdown
else
if [[ -e ${DATA_DIR}/mysql.sock ]]; then
rm -f ${DATA_DIR}/mysql.sock
fi
MARIADB_NEW=false
echo "===> Using an existing volume of MariaDB"
fi
}
#
# When Startup Container script
#
if [[ -e /scripts/firstrun ]]; then
# config mariadb
run_maria
rm /scripts/firstrun
else
# Cleanup previous mariadb sockets
if [[ -e ${DATA_DIR}/mysql.sock ]]; then
rm -f ${DATA_DIR}/mysql.sock
fi
fi
exec /usr/bin/mysqld_safe
EOF
else
echo "Dir scripts 已存在!"
fi
echo "----------mkdir----mysql-----DB----------------"
mkdir -p /data/mysql
echo "------------------run_build--Dockerfile-----------------"
docker build -t python-mariadb:sql-v2 .
echo "------------------run_docker-Images-Mriadb---------------------------------"
docker run --name python-mariadb-v2 --privileged=true -v /data/mysql:/var/lib/mysql -p 3306:3306 -d python-mariadb:sql-v2
说明: 该脚本会在当前目录下检测是否有dockerfile 文件 没完创建 后又生成mariadb 配置文件 并开始 build 得mariadb images run 启
重点在start.sh 该脚本内 会copy(scripts 目录)进容器内为 容器启动时运行脚本 初始化 mariadb 并创建 SQL 库 ’opsmanage‘ ‘DBname’ ‘userName’ ’passwordName‘ 可以在run容器时 用 -e 参数 传进 环境变量 进行修改
并可在宿主机 ll /data/mysql 下查看到 容器是否 有创建opsmanage库 和有没有运行成功! 运行时所生成得 mysql.sock
查看一下运行状态:
连续测试一下:
以上是关于Docker 一步一步 搭建开源 CMDB平台 第二步: DockerFile 构建mariadb的主要内容,如果未能解决你的问题,请参考以下文章
一步一步搭建ZooKeeper + Mesos + Marathon平台管理Docker集群