docker快速部署脚本

#!/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

借鉴于某个博主,找不到原文章了。可根据实际情况修改。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 Docker部署数据库脚本,您可以按照以下步骤进行操作: 1. 准备数据库脚本文件。确保您已经准备好要部署的数据库脚本文件。这可以是 SQL 文件、shell 脚本或其他数据库脚本文件。 2. 创建 Docker 容器。使用适用于您的数据库的 Docker 镜像来创建一个容器。例如,如果您要使用 MySQL 数据库,可以运行以下命令创建一个 MySQL 容器: ```shell docker run -d --name mysql-container -e MYSQL_ROOT_PASSWORD=<password> mysql:tag ``` 这将创建一个名为 `mysql-container` 的 MySQL 容器,并设置 `MYSQL_ROOT_PASSWORD` 环境变量为您的数据库的根密码。请将 `<password>` 替换为实际的密码,并将 `tag` 替换为所需的 MySQL 版本标签(例如 `5.7`)。 3. 将脚本文件复制到容器中。使用以下命令将数据库脚本文件复制到运行中的容器中: ```shell docker cp <script-file> mysql-container:/<destination-path>/<script-file> ``` 这将把 `<script-file>` 复制到名为 `mysql-container` 的容器中的 `<destination-path>` 目录下。请将 `<script-file>` 替换为实际的脚本文件名,并将 `<destination-path>` 替换为目标路径。 4. 进入容器并执行脚本。使用以下命令进入容器并执行数据库脚本: ```shell docker exec -it mysql-container bash ``` 这将在容器中打开一个交互式终端。 5. 在容器中执行脚本。在容器终端中,使用适合您的数据库类型的命令运行脚本。例如,对于 MySQL,可以运行以下命令: ```shell mysql -u root -p <destination-path>/<script-file> ``` 这将使用根用户登录 MySQL 并执行脚本。根据您的脚本和数据库类型,可能需要提供其他参数或进行其他调整。 6. 等待脚本执行完成。根据脚本的大小和复杂性,可能需要一些时间来执行脚本。请耐心等待直到脚本执行完成。 现在,您已经成功在 Docker 容器中部署了数据库脚本。您可以通过连接到容器或使用相关的应用程序验证脚本是否成功执行。 请注意,上述步骤中提到的 `<password>` 和 `<script-file>` 都是需要您替换为实际的密码和文件名的占位符。确保根据您的环境进行相应的调整。 希望这个步骤对您有所帮助!如果您有任何其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值