项目中简单实用docker部署微服务

首先声明该应用主要是为了自己备份,并不具有一定的通用性。
1、首先准备好需要部署的jar包、Dockerfile文件(文件名一定要是这样)

2、将jar包和Dockerfile等文件打包成zip包,当然也可以直接放到服务器的某个目录下(如下图):
jar包建议单个文件
3、启动脚本dockerRun.sh中的内容如下(注意脚本编写需要在linux下编写,否则会存在格式问题):

#!/usr/bin/env bash
curDir=`pwd`

profiles=$2
rootDir=${curDir}
imageName="xxxx-deal"
imageVersion="1.0"
#日志路径,默认的docker服务会将日志生成到docker容器中,需要在docker run时追加到宿主机
logDir=${rootDir}/logs

start() {
     
  if [ ! -d ${logDir} ]; then
    echo "${logDir} not exist, create it."
    mkdir -p ${logDir}
  fi
  echo "start ${imageName}:${imageVersion}..."
  # --network=host表示使用宿主机ip
  # -v表示进行容器和宿主机的卷映射,类似于共享目录
  docker run --network=host -v /sse/users/rcpetl/data/in/:/sse/users/rcpetl/data/in/ \
  -e profiles=${profiles} --name ${imageName} ${imageName}:${imageVersion} >> ${logDir}/app.log &
}

stop() {
     
  pid=`docker ps -a | grep ${
      imageName}:${
      imageVersion} | grep -v grep | awk '{print $1}'`
  echo "docker stop ${imageName}..."
  if [ -n "${pid}" ]; then
    docker stop $pid
  fi
}

restart() {
     
  stop
  sleep 1
  remove
  sleep 1
  build
  sleep 1
  start
}

remove() {
     
  stop
  sleep 1
  pid=`docker ps -a | grep ${
      imageName}:${
      imageVersion} | grep -v grep | awk '{print $1}'`
  echo "docker remove ${imageName}..."
  if [ -n "${pid}" ]; then
    docker rm $pid
  fi
  sleep 1
  gid=`docker images | grep ${
      imageName} | grep -v grep | awk '{print $1":"$2}'`
  if [ -n "${gid}" ]; then
    docker rmi ${imageName}:${imageVersion}
  fi
}

build() {
     
  docker build -t ${imageName}:${imageVersion} ${rootDir}/
}

echo $"Usage: remind:  
       sh dockerRun.sh stop|start|restart test|dev|pro or
       sh dockerRun.sh build|remove test
       please first stop, then remove and start last,
       or you can use restart instruction to start project."

case "$1" in
  start|stop|restart)
    case "$2" in
      pro|test|dev)
        $1
        ;;
      *)
        echo $"Usage: $0 (start|stop|restart) {pro|test|dev}"
        exit 2
    esac
    ;;
  build)
    case "$2" in 
      pro|test|dev)
        $1
        ;;
      *)
        echo $"Usage: $0 (build) {pro|test|dev}"
        exit 2
    esac
    ;;
  remove)
    case "$2" in 
      pro|test|dev)
        $1
        ;;
      *)
        echo $"Usage: $0 (remove) {pro|test|dev}"
        exit 2
    esac
    ;;
  *)
    echo $"Usage: $0 {start|stop|restart|build|remove}"
    exit 1
esac

4、使用docker启动服务的命令基本上就是

sh dockerRun.sh restart|stop|start test|pro|dev

5、Dockerfile文件的内容如下图所示:

FROM openjdk:8-jre-slim
MAINTAINER sse

ENV profiles=""

#进行docker容器时区与宿主机的同步
RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' > /etc/timezone

RUN mkdir /logs/

ADD offdata-deal-1.0-SNAPSHOT.jar /app.jar

# 执行命令的入口,可以将相关jvm参数通过dockerRun脚本参数传入
ENTRYPOINT ["sh", "-c", "java -XX:+UseConcMarkSweepGC -Xmx2048m -Xms1024m \
-XX:+PrintGCDateStamps -XX:+PrintGCDetails -Xloggc:/logs/java_gc.log \
-jar /app.jar  -XX:-HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/logs/ \
-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8392 \
--spring.profiles.active=$profiles"]

6、docker的相关命令:

#导出镜像
docker save -o openjdk:8-jre-slim openjdk:8-jre-slim
#导入镜像
docker load -i ./openjdk:8-jre-slim
docker load < ./openjdk:8-jre-slim
#创建自定义网桥,会虚拟出一个ip地址
docker network create --subnet=172.19.0.0/16 mybridge
#进入容器的命令(以root用户)
docker exec -it containerId /bin/bash

7、多服务器运行脚本(start-all.sh)

#!/usr/bin/env bash
curDir=`pwd`

profiles=$2
rootDir=${curDir}
imageName="offdata"
date=$3
# 该脚本与array中值属于同一个目录,每个array值都是一个目录名称
modelArray=("xxx-deal-1.0-SNAPSHOT" "xxx-xxljob-admin-1.0-SNAPSHOT" "xxx-dbmysql-1.0-SNAPSHOT")

echo $"Usage: remind:  
       sh start-all.sh stop|start|restart|build|remove test|dev|pro or
       please first stop, then remove and start last,
       or you can use restart instruction to start project."

case "$1" in
  start|stop|restart|build|remove)
    case "$2" in
      pro|test|dev)        
        for model in ${modelArray[@]}
        do
          cd ${rootDir}/$model
          sh dockerRun.sh $1 ${profiles} ${date}
          #echo "$1 ${profiles} ${date}"
          sleep 1
        done
        ;;
      *)
        echo $"Usage: $0 (start|stop|restart|build|remove) {pro|test|dev} yyyyMMdd"
        exit 2
    esac
    ;;
  *)
    echo $"Usage: $0 {start|stop|restart|build|remove}"
    exit 1
esac

启动|停止等命令

sh start-all.sh restart|stop|start test|pro|dev

你可能感兴趣的:(运维管理,docker,shell)