#!/bin/bash
# 先判断Docker服务是否启动
dockerStatusResult=$(service docker status)
statusName="running"
if [[ ! $dockerStatusResult =~ $statusName ]]; then
echo "请先启动Docker服务..."
exit 0
fi
funPrintK() {
echo ''
}
funPrintX() {
for ((i = 0; i < $1; i++)); do
printf '*'
done
funPrintK
}
funPrintJ() {
for ((i = 0; i < $1; i++)); do
printf '+'
done
if [ "$2" -eq 1 ]; then
funPrintK
fi
}
funPrintK
funPrintX 81
printf "*%-28sDocker 容器快捷安装程序%-28s*"
funPrintK
printf "*%-28s@Author: An%-39s*"
funPrintK
printf "*%-28sVersion: 1.0%-37s*"
funPrintK
funPrintX 81
funPrintK
funPrintJ 36 0
printf '开始安装'
funPrintJ 37 1
printf "+%-32s请输入你的选择:%-32s+"
funPrintK
printf "+%-32s[1]: Nacos%-37s+"
funPrintK
printf "+%-32s[2]: Redis%-37s+"
funPrintK
printf "+%-32s[3]: MySql%-37s+"
funPrintK
printf "+%-32s[4]: Nginx%-37s+"
funPrintK
printf "+%-32s[5]: Keycloak%-37s+"
funPrintK
printf "+%-32s[6]: go-fastdfs%-37s+"
funPrintK
printf "+%-32s[7]: postgres%-37s+"
funPrintK
printf "+%-32s[8]: rabbitmq%-37s+"
funPrintK
printf "+%-32s[9]: postgis%-37s+"
funPrintK
printf "+%-32s[*]: 任意字符退出%-30s+"
funPrintK
funPrintJ 81 1
funPrintK
BASE_PATH="/opt/docker"
JAVA_PATH="/opt/speed"
NACOS_NAME="nacos"
MYSQL_NAME="mysql"
REDIS="redis"
NGINX="nginx"
KEYCLOAK="keycloak"
FASTDFS="fastdfs"
POSTGRES="postgres9.5"
RABBITMQ="rabbitmq"
POSTGIS="postgis"
externalPort() {
funPrintK
read -p "请输入""$APPLICATION_NAME""外部端口:" EXTERNAL_PORT
if echo "$EXTERNAL_PORT" | grep -q '[^0-9]'; then
echo "请输入正整数..."
externalPort
fi
netstat -antp | grep "$EXTERNAL_PORT"
funPrintK
read -p "查看端口是否被占用,端口占用可能导致安装失败!继续下一步请输入(yes/y),重来请输入(no/n);退出直接回车:" port
case "$port" in
y | yes | Y | YES)
internalPort
;;
n | no | N | NO)
externalPort
;;
*)
exit 0
;;
esac
}
internalPort() {
read -p "请输入""$APPLICATION_NAME""内部端口:" INTERNAL_PORT
if echo "$INTERNAL_PORT" | grep -q '[^0-9]'; then
echo "请输入正整数..."
internalPort
fi
}
funclogerror(){
DATE=`date "+%Y-%m-%d %H:%M:%S"`
USER=$(whoami)
echo "\${DATE} \${USER} execute \$0 [INFO] \$@" >>/opt/docker/log_error.log
}
funPassword() {
read -s -p "请输入创建""$APPLICATION_NAME""的密码:" PASSWORD
funPrintK
}
funPassword2() {
read -s -p "请输入""$APPLICATION_NAME""数据库的密码:" Passkeycloak
funPrintK
}
funIp() {
read -s -p "请输入""$APPLICATION_NAME""数据库的ip地址:" IP
funPrintK
}
basePath() {
sleep 1
if [ ! -x "$BASE_PATH" ]; then
sudo mkdir "$BASE_PATH"
sudo chown -R "$USER" "$BASE_PATH"
fi
if [ ! -x "$BASE_PATH/$APPLICATION_NAME" ]; then
mkdir -p "$BASE_PATH/$APPLICATION_NAME"
if [ "$?" -ne 0 ]; then
echo "$BASE_PATH/$APPLICATION_NAME""路径创建失败"
exit 0
else
echo "$BASE_PATH/$APPLICATION_NAME""路径创建成功"
fi
fi
}
logPath() {
if [ ! -x "$BASE_PATH/$APPLICATION_NAME""/logs" ]; then
mkdir -p "$BASE_PATH/$APPLICATION_NAME""/logs"
if [ "$?" -ne 0 ]; then
echo "$BASE_PATH/$APPLICATION_NAME""/logs路径创建失败"
exit 0
else
echo "$BASE_PATH/$APPLICATION_NAME""/logs路径创建成功"
fi
fi
}
confPath() {
if [ ! -x "$BASE_PATH/$APPLICATION_NAME""/conf" ]; then
mkdir -p "$BASE_PATH/$APPLICATION_NAME""/conf"
if [ "$?" -ne 0 ]; then
echo "$BASE_PATH/$APPLICATION_NAME""/conf路径创建失败"
exit 0
else
echo "$BASE_PATH/$APPLICATION_NAME""/conf路径创建成功"
fi
fi
}
dataPath() {
if [ ! -x "$BASE_PATH/$APPLICATION_NAME""/data" ]; then
mkdir -p "$BASE_PATH/$APPLICATION_NAME""/data"
if [ "$?" -ne 0 ]; then
echo "$BASE_PATH/$APPLICATION_NAME""/data路径创建失败"
exit 0
else
echo "$BASE_PATH/$APPLICATION_NAME""/data路径创建成功"
fi
fi
}
themesPath() {
if [ ! -x "$BASE_PATH/$APPLICATION_NAME""/themes" ]; then
mkdir -p "$BASE_PATH/$APPLICATION_NAME""/themes"
if [ "$?" -ne 0 ]; then
echo "$BASE_PATH/$APPLICATION_NAME""/themes路径创建失败"
exit 0
else
echo "$BASE_PATH/$APPLICATION_NAME""/themes路径创建成功"
fi
fi
}
providersPath() {
if [ ! -x "$BASE_PATH/$APPLICATION_NAME""/providers" ]; then
mkdir -p "$BASE_PATH/$APPLICATION_NAME""/providers"
if [ "$?" -ne 0 ]; then
echo "$BASE_PATH/$APPLICATION_NAME""/providers路径创建失败"
exit 0
else
echo "$BASE_PATH/$APPLICATION_NAME""/providers路径创建成功"
fi
fi
}
nginxConf() {
if [ ! -x "$BASE_PATH/$APPLICATION_NAME""/conf/conf.d" ]; then
mkdir -p "$BASE_PATH/$APPLICATION_NAME""/conf/conf.d"
if [ "$?" -ne 0 ]; then
echo "$BASE_PATH/$APPLICATION_NAME""/conf/conf.d路径创建失败"
exit 0
else
echo "$BASE_PATH/$APPLICATION_NAME""/conf/conf.d路径创建成功"
fi
fi
}
nginxHtml() {
if [ ! -x "$BASE_PATH/$APPLICATION_NAME""/html" ]; then
mkdir -p "$BASE_PATH/$APPLICATION_NAME""/html"
if [ "$?" -ne 0 ]; then
echo "$BASE_PATH/$APPLICATION_NAME""/html路径创建失败"
exit 0
else
echo "$BASE_PATH/$APPLICATION_NAME""/html路径创建成功"
fi
fi
}
funCheckRedisConf() {
if [ ! -f "$BASE_PATH/$APPLICATION_NAME""/conf/redis.conf" ]; then
touch "$BASE_PATH/$APPLICATION_NAME""/conf/redis.conf"
if [ "$?" -ne 0 ]; then
echo "$BASE_PATH/$APPLICATION_NAME""/conf/redis.conf创建失败"
exit 0
else
echo "$BASE_PATH/$APPLICATION_NAME""/conf/redis.conf创建成功"
fi
echo " " >>"$BASE_PATH/$APPLICATION_NAME""/conf/redis.conf"
fi
}
funCheckNginxConf() {
if [ ! -f "$BASE_PATH/$APPLICATION_NAME""/conf/nginx.conf" ]; then
touch "$BASE_PATH/$APPLICATION_NAME""/conf/nginx.conf"
if [ "$?" -ne 0 ]; then
echo "$BASE_PATH/$APPLICATION_NAME""/conf/nginx.conf创建失败"
exit 0
else
echo "$BASE_PATH/$APPLICATION_NAME""/conf/nginx.conf创建成功"
fi
funPrintK
echo "user nginx;" >>"$BASE_PATH/$APPLICATION_NAME""/conf/nginx.conf"
echo "worker_processes auto;" >>"$BASE_PATH/$APPLICATION_NAME""/conf/nginx.conf"
funPrintK
echo "error_log /var/log/nginx/error.log notice;" >>"$BASE_PATH/$APPLICATION_NAME""/conf/nginx.conf"
echo "pid /var/run/nginx.pid;" >>"$BASE_PATH/$APPLICATION_NAME""/conf/nginx.conf"
funPrintK
funPrintK
echo "events {" >>"$BASE_PATH/$APPLICATION_NAME""/conf/nginx.conf"
echo " worker_connections 1024;" >>"$BASE_PATH/$APPLICATION_NAME""/conf/nginx.conf"
echo "}" >>"$BASE_PATH/$APPLICATION_NAME""/conf/nginx.conf"
funPrintK
funPrintK
echo "http {" >>"$BASE_PATH/$APPLICATION_NAME""/conf/nginx.conf"
echo " include /etc/nginx/mime.types;" >>"$BASE_PATH/$APPLICATION_NAME""/conf/nginx.conf"
echo " default_type application/octet-stream;" >>"$BASE_PATH/$APPLICATION_NAME""/conf/nginx.conf"
funPrintK
echo " log_format main" '$remote_addr - $remote_user [$time_local] "$request" ' >>"$BASE_PATH/$APPLICATION_NAME""/conf/nginx.conf"
echo " "'$status $body_bytes_sent "$http_referer" ' >>"$BASE_PATH/$APPLICATION_NAME""/conf/nginx.conf"
echo " "'"$http_user_agent" "$http_x_forwarded_for"'';' >>"$BASE_PATH/$APPLICATION_NAME""/conf/nginx.conf"
funPrintK
echo " access_log /var/log/nginx/access.log main;" >>"$BASE_PATH/$APPLICATION_NAME""/conf/nginx.conf"
echo " sendfile on;" >>"$BASE_PATH/$APPLICATION_NAME""/conf/nginx.conf"
echo " #tcp_nopush on;" >>"$BASE_PATH/$APPLICATION_NAME""/conf/nginx.conf"
echo " keepalive_timeout 65;" >>"$BASE_PATH/$APPLICATION_NAME""/conf/nginx.conf"
echo " #gzip on;" >>"$BASE_PATH/$APPLICATION_NAME""/conf/nginx.conf"
echo " include /etc/nginx/conf.d/*.conf;" >>"$BASE_PATH/$APPLICATION_NAME""/conf/nginx.conf"
echo "}" >>"$BASE_PATH/$APPLICATION_NAME""/conf/nginx.conf"
fi
}
funCheckNginxDef() {
if [ ! -f "$BASE_PATH/$APPLICATION_NAME""/conf/conf.d/default.conf" ]; then
touch "$BASE_PATH/$APPLICATION_NAME""/conf/conf.d/default.conf"
if [ "$?" -ne 0 ]; then
echo "$BASE_PATH/$APPLICATION_NAME""/conf/conf.d/default.conf创建失败"
exit 0
else
echo "$BASE_PATH/$APPLICATION_NAME""/conf/conf.d/default.conf创建成功"
fi
echo "server {" >>"$BASE_PATH/$APPLICATION_NAME""/conf/conf.d/default.conf"
echo "listen 80;" >>"$BASE_PATH/$APPLICATION_NAME""/conf/conf.d/default.conf"
echo "listen [::]:80;" >>"$BASE_PATH/$APPLICATION_NAME""/conf/conf.d/default.conf"
echo "server_name localhost;" >>"$BASE_PATH/$APPLICATION_NAME""/conf/conf.d/default.conf"
echo " #access_log /var/log/nginx/host.access.log main;" >>"$BASE_PATH/$APPLICATION_NAME""/conf/conf.d/default.conf"
echo "location / {" >>"$BASE_PATH/$APPLICATION_NAME""/conf/conf.d/default.conf"
echo "root /usr/share/nginx/html;" >>"$BASE_PATH/$APPLICATION_NAME""/conf/conf.d/default.conf"
echo "index index.html index.htm;" >>"$BASE_PATH/$APPLICATION_NAME""/conf/conf.d/default.conf"
echo "}" >>"$BASE_PATH/$APPLICATION_NAME""/conf/conf.d/default.conf"
echo "#error_page 404 /404.html;" >>"$BASE_PATH/$APPLICATION_NAME""/conf/conf.d/default.conf"
echo "# redirect server error pages to the static page /50x.html" >>"$BASE_PATH/$APPLICATION_NAME""/conf/conf.d/default.conf"
echo "#" >>"$BASE_PATH/$APPLICATION_NAME""/conf/conf.d/default.conf"
echo "error_page 500 502 503 504 /50x.html;" >>"$BASE_PATH/$APPLICATION_NAME""/conf/conf.d/default.conf"
echo "location = /50x.html {" >>"$BASE_PATH/$APPLICATION_NAME""/conf/conf.d/default.conf"
echo " root /usr/share/nginx/html;" >>"$BASE_PATH/$APPLICATION_NAME""/conf/conf.d/default.conf"
echo "}" >>"$BASE_PATH/$APPLICATION_NAME""/conf/conf.d/default.conf"
echo "}" >>"$BASE_PATH/$APPLICATION_NAME""/conf/conf.d/default.conf"
fi
}
funDocker() {
echo "开始停止""$APPLICATION_NAME"" 容器..."
docker stop "$APPLICATION_NAME"
funPrintK
echo "开始删除""$APPLICATION_NAME""容器..."
docker rm "$APPLICATION_NAME"
funPrintK
}
funStart() {
funPrintK
echo "开始安装""$APPLICATION_NAME""..."
funPrintK
}
funSuccess() {
if [ "$?" -ne 0 ]; then
echo "$APPLICATION_NAME""安装失败!"
else
echo "$APPLICATION_NAME""安装成功!"
funPrintK
docker ps | grep "$APPLICATION_NAME"
fi
exit 0
}
funNacos() {
APPLICATION_NAME="nacos"
externalPort
basePath
logPath
confPath
funStart
funDocker
docker run -itd --name "$APPLICATION_NAME" --restart always -p "$EXTERNAL_PORT":"$INTERNAL_PORT" \
-v "$BASE_PATH/$APPLICATION_NAME/data":/home/nacos/data \
-e MODE=standalone \
"$NACOS_NAME"
funSuccess
}
funRedis() {
APPLICATION_NAME="redis"
externalPort
funPassword
basePath
confPath
dataPath
funCheckRedisConf
funStart
funDocker
docker run -itd --name "$APPLICATION_NAME" --restart always -p "$EXTERNAL_PORT":"$INTERNAL_PORT" \
-v "$BASE_PATH/$APPLICATION_NAME""/conf/redis.conf":/etc/redis/redis.conf \
-v "$BASE_PATH/$APPLICATION_NAME""/data":/etc/redis/data \
"$REDIS" \
--appendonly yes --requirepass "$PASSWORD"
funSuccess
}
funMysql() {
APPLICATION_NAME="mysql"
externalPort
funPassword
basePath
dataPath
confPath
funStart
funDocker
docker run -itd --name "$APPLICATION_NAME" --net static --ip 172.18.0.2 --restart always -p "$EXTERNAL_PORT":"$INTERNAL_PORT" \
-e MYSQL_ROOT_PASSWORD="$PASSWORD" \
-v "$BASE_PATH/$APPLICATION_NAME""/data/":/var/lib/mysql \
-v "$BASE_PATH/$APPLICATION_NAME""/conf":/etc/mysql/conf.d \
"$MYSQL_NAME"
funSuccess
}
funNginx() {
APPLICATION_NAME="nginx"
externalPort
basePath
confPath
logPath
nginxConf
nginxHtml
funCheckNginxConf
funCheckNginxDef
funStart
funDocker
docker run -d --name "$APPLICATION_NAME" -p "$EXTERNAL_PORT":"$INTERNAL_PORT" \
-v "$BASE_PATH/$APPLICATION_NAME""/html/":/usr/share/nginx/html \
-v "$BASE_PATH/$APPLICATION_NAME""/conf/nginx.conf":/etc/nginx/nginx.conf \
-v "$BASE_PATH/$APPLICATION_NAME""/conf/conf.d/default.conf":/etc/nginx/conf.d/default.conf \
-v "$BASE_PATH/$APPLICATION_NAME""/logs/":/var/log/nginx \
"$NGINX"
funSuccess
}
funKeycloak() {
APPLICATION_NAME="keycloak"
externalPort
funPassword
funIp
funPassword2
basePath
themesPath
providersPath
funStart
funDocker
docker run -itd --name "$APPLICATION_NAME" --restart=always -p "$EXTERNAL_PORT":"$INTERNAL_PORT" \
-e KEYCLOAK_USER=admin \
-e KEYCLOAK_PASSWORD=$PASSWORD \
-e DB_VENDOR=postgres \
-e DB_USER=postgres \
-e DB_PASSWORD=$Passkeycloak \
-e DB_ADDR=$IP \
-e DB_PORT=5432 \
-e DB_DATABASE=keycloak \
-e JDBC_PARAMS='connectTimeout=30' \
-v "$BASE_PATH/$APPLICATION_NAME""/themes":/opt/jboss/keycloak/themes \
-v "$BASE_PATH/$APPLICATION_NAME""/providers":/opt/jboss/keycloak/providers \
"$KEYCLOAK"
funSuccess
}
funFastdfs() {
APPLICATION_NAME="fastdfs"
externalPort
basePath
dataPath
funStart
funDocker
docker run -itd --name "$APPLICATION_NAME" --restart always -p "$EXTERNAL_PORT":"$INTERNAL_PORT" \
-v "$BASE_PATH/$APPLICATION_NAME""/data":/data \
-e GO_FASTDFS_DIR=/data \
"$FASTDFS"
funSuccess
}
funPostgres() {
APPLICATION_NAME="postgres"
externalPort
funPassword
basePath
dataPath
funStart
funDocker
docker run -itd --name "$APPLICATION_NAME" --restart always -p "$EXTERNAL_PORT":"$INTERNAL_PORT" \
-e POSTGRES_PASSWORD="$PASSWORD" \
-v "$BASE_PATH/$APPLICATION_NAME""/data/":/var/lib/postgresql/data \
"$POSTGRES"
funSuccess
}
funRabbitmq() {
APPLICATION_NAME="rabbitmq"
externalPort
funPassword
logPath
basePath
dataPath
funStart
funDocker
docker run -itd --name "$APPLICATION_NAME" --restart always -p "$EXTERNAL_PORT":"$INTERNAL_PORT" -p 5672:5672 \
-e RABBITMQ_DEFAULT_USER=admin \
-e RABBITMQ_DEFAULT_PASS="$PASSWORD" \
-v "$BASE_PATH/$APPLICATION_NAME""/conf":/etc/rabbitmq/rabbitmq.conf \
-v "$BASE_PATH/$APPLICATION_NAME""/data/":/var/lib/rabbitmq \
-v "$BASE_PATH/$APPLICATION_NAME""/log/":/var/log/rabbitmq/log \
"$RABBITMQ"
funSuccess
}
funPostgis() {
APPLICATION_NAME="postgis"
externalPort
funPassword
basePath
dataPath
funStart
funDocker
docker run -itd --name "$APPLICATION_NAME" -p "$EXTERNAL_PORT":"$INTERNAL_PORT" \
-e RABBITMQ_DEFAULT_USER=postgres \
-e RABBITMQ_DEFAULT_PASS="$PASSWORD" \
-e POSTGRES_DBNAME=gis \
-e ALLOW_IP_RANGE=0.0.0.0/0 \
-v "$BASE_PATH/$APPLICATION_NAME""/conf":/var/lib/postgresql/data \
"$POSTGIS"
funSuccess
}
read -p "请输入对应的序号:" input
case $input in
1)
funNacos
;;
2)
funRedis
;;
3)
funMysql
;;
4)
funNginx
;;
5)
funKeycloak
;;
6)
funFastdfs
;;
7)
funPostgres
;;
8)
funRabbitmq
;;
9)
funPostgis
;;
*)
echo "退出安装程序"
exit 0
;;
esac
借鉴于某个博主,找不到原文章了。可根据实际情况修改。