docker安装tomcat、mysql、redis

#!/bin/bash
#Author: 臆想的一只猫
#Created: 2022-04-04 16:47:08
#Description: docker安装软件 

# Tomcat
SEARCH_TOMCAT=`docker search tomcat | wc -l`
HOST_TOMCAT='/data/volume/tomcat-8081'
DOCKER_TOMCAT='/usr/local/tomcat'
function install_tomcat() {
  
  if [[ ${SEARCH_TOMCAT} -gt 0 ]];
  then
    docker pull tomcat:9-jre8-alpine
    TOMCAT_IMAGE_ID=`docker images | grep tomcat | awk '{print $3}'`
    docker run -d -p 8081:8080 --name tomcat-8081 --privileged=true -v ${HOST_TOMCAT}/logs/:${DOCKER_TOMCAT}/logs/ ${TOMCAT_IMAGE_ID}
  else
    echo '没有找到相关镜像...'
  fi 
}

IS_TOMCAT=`docker images | grep tomcat`
if [[ ${IS_TOMCAT} ]];
then
  echo 'tomcat镜像已存在,此脚本不适用...'
else
  install_tomcat
fi

# MySQL
SEARCH_MYSQL=`docker search mysql | wc -l`
HOST_MYSQL='/data/volume/mysql-3306'
DOCKER_MYSQL='/var'
function install_mysql() {
 
  if [[ ${SEARCH_MYSQL} -gt 0 ]];
  then
    docker pull mysql:5.7
    MYSQL_IMAGE_ID=`docker images | grep mysql | awk '{print $3}'`
    docker run -d -p 3306:3306 --name mysql-3306 --privileged=true -v ${HOST_MYSQL}/log:${DOCKER_MYSQL}/log/mysql -v ${HOST_MYSQL}/data:${DOCKER_MYSQL}/lib/mysql -v ${HOST_MYSQL}/conf:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=123456 ${MYSQL_IMAGE_ID}
    cd ${HOST_MYSQL}/conf
    touch my.cnf
    echo '[client]' >> ${HOST_MYSQL}/conf/my.cnf
    echo 'default_character_set=utf8' >>  ${HOST_MYSQL}/conf/my.cnf
    echo '[mysqld]' >>  ${HOST_MYSQL}/conf/my.cnf
    echo 'collation_server=utf8_general_ci' >>  ${HOST_MYSQL}/conf/my.cnf
    echo 'character_set_server=utf8' >>  ${HOST_MYSQL}/conf/my.cnf
    docker restart mysql-3306
  else
     echo '没有找到相关镜像...'
  fi
}

IS_MYSQL=`docker images | grep mysql`
if [[ ${IS_MYSQL} ]];
then
  echo 'mysql镜像已存在,此脚本不适用...'
else
  install_mysql
fi

# Redis
SEARCH_REDIS=`docker search reids | wc -l`
HOST_REDIS='/data/volume/redis-6379'
DOCKER_REDIS='/etc/redis'

function install_redis() {

  if [[ ${SEARCH_REDIS} -gt 0 ]];
  then
    docker pull redis:6.0.8
    REDIS_IMAGE_ID=`docker images | grep redis | awk '{print $3}'`
    echo -e '\033[31m拷贝一个redis.conf到/data/volume/redis-6379下\033[0m'
    docker run -p 6379:6379 --name redis-6379 --privileged=true -v ${HOST_REDIS}/redis.conf:${DOCKER_REDIS}/redis.conf -v ${HOST_REDIS}/data:/data -d ${REDIS_IMAGE_ID} redis-server ${DOCKER_REDIS}/redis.conf
   else
     echo '没有找到相关镜像...'
   fi
}

IS_REDIS=`docker images | grep redis`
if [[ ${IS_REDIS} ]];
then
  echo 'redis镜像已存在,此脚本不适用...'
else
  install_redis
fi

你可能感兴趣的:(#,Shell,#,Docker,docker,脚本)