运维辞典@欢迎访问小站,如有疑问,可通过右侧QQ或底部邮箱联系,非常感谢@

docker与shell的结合,快速创建容器部署wordpress程序

Docker Administrator 514℃

一个网站容器对应一个数据库容器

#!/bin/bash
NAME=$1
WEB_PORT=$2
MYSQLROOTPASS=west123
MYSQL_HOST_NAME=${NAME}_mysql
WEB_HOST_NAME=${NAME}_web
 
docker pull mysql:5.6
docker run --name=${MYSQL_HOST_NAME} -e MYSQL_ROOT_PASSWORD=${MYSQLROOTPASS} -d mysql:5.6
[ $? == 0 ] && echo "mysql容器创建成功.管理密码${MYSQLROOTPASS}" || echo "mysql err !"
docker pull wordpress
docker run -p ${WEB_PORT}:80 --name=${WEB_HOST_NAME} --link ${MYSQL_HOST_NAME}:mysql -d wordpress
[ $? == 0 ] && echo "web容器创建成功.访问端口${WEB_PORT}" || echo "web err !"

多个网站容器对应一个数据库容器,批量创建多个网站容器

#!/bin/bash
NAME=wd
MYSQLROOTPASS=west123
MYSQL_HOST_NAME=${NAME}_mysql
# docker pull mysql:5.6
# docker pull wordpress
docker run --name=${MYSQL_HOST_NAME} -e MYSQL_ROOT_PASSWORD=${MYSQLROOTPASS} -d mysql:5.6 &>/dev/null
MYSQL_IP=`docker inspect -f {{.NetworkSettings.IPAddress}} ${MYSQL_HOST_NAME}`
[ $? == 0 ] && echo "mysql容器${MYSQL_HOST_NAME}创建成功.连接ip地址${MYSQL_IP},管理密码${MYSQLROOTPASS}" || echo "mysql err !"
sleep 10
for (( i = 1; i < 10; i++ )); do
	WEB_PORT=$[80+$i]
	WEB_HOST_NAME=${NAME}_web_$i
	DB_NAME=wd$i
	docker exec $MYSQL_HOST_NAME mysql -uroot -p${MYSQLROOTPASS} -e "create database ${DB_NAME}"
	[ $? == 0 ] && echo "在数据库容器${MYSQL_HOST_NAME}上创建数据库${DB_NAME}成功." || echo "create database ${DB_NAME} err !"
	# echo ${WEB_HOST_NAME}
	# docker run --name=${MYSQL_HOST_NAME} -e MYSQL_ROOT_PASSWORD=${MYSQLROOTPASS} -d mysql:5.6
	# [ $? == 0 ] && echo "mysql容器创建成功.管理密码${MYSQLROOTPASS}" || echo "mysql err !"
	# docker run -p ${WEB_PORT}:80 --name=${WEB_HOST_NAME} --link ${MYSQL_HOST_NAME}:mysql -d wordpress &>/dev/null
	docker run -p ${WEB_PORT}:80 --name=${WEB_HOST_NAME} -d wordpress &>/dev/null
	[ $? == 0 ] && echo "web容器${WEB_HOST_NAME}创建成功.访问端口${WEB_PORT}" || echo "web err !"
done

码字很辛苦,转载请注明来自:《运维辞典 » docker与shell的结合,快速创建容器部署wordpress程序

喜欢 (2)