docker配置springcloud项目到服务器

1.服务器安装部署docker镜像

2.创建镜像目录,如图所示

 以其中一个服务为例,每个文件夹对应一个服务单元,每个文件夹中包含  sh,Dockerfile文件

3.编辑项目中的application-test(根据环境来定),如图:

 

 

 

4.配置Nacos,单独配置命名空间,增加配置文件,注意,组一定要是test,配错了系统找不到配置。

 

server:
  port: 18206
spring:
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.10.21:8848
        namespace: museum
  datasource:
    type: com.zaxxer.hikari.HikariDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/museum_cms?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
    hikari:
      minimum-idle: 5
      maximum-pool-size: 15
      auto-commit: true
      idle-timeout: 30000
      max-lifetime: 1800000
      connection-timeout: 30000
      connection-test-query: SELECT 1
  redis:
    cluster:
      enabled: true
      timeout: 2000
      max-redirects: 8
      nodes: 192.168.10.21:8001,192.168.10.21:8002,192.168.10.21:8003,192.168.10.21:8004,192.168.10.21:8005,192.168.10.21:8006
    password: BJf+vg/PdRlCx5u/yTQwF3CHGUCu1pcXI40wVgu8rZJQoEMJHhQO+w+KOAZX9ALd7rOK4kn99G6SmidjUdreWgha4D+H9dXKtL27tJV6Qt4lypVwFQoZC3uz77+2EwHMcAfs2F8pvGh86vmkxEGPTvD23Dc=
mybatis-plus:
  mapper-locations: classpath:mapper/**/*Mapper.xml
  #实体扫描,多个package用逗号或者分号分隔
  typeAliasesPackage: com.cloudoer.**.cms.entity
  global-config:
    # 关闭MP3.0自带的banner
    banner: false
    db-config:
      logic-delete-field: flag  # 全局逻辑删除的实体字段名(since 3.3.0,配置后可以忽略不配置步骤2)
      logic-delete-value: 0 # 逻辑已删除值(默认为 1)
      logic-not-delete-value: 1 #
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl   #控制台打印SQL语句
    cache-enabled: false
  main:
    allow-bean-definition-overriding: true
logging:
  path: /home/apps/museum/cms/logs
  env: dev
  level:
    com.cloudoer.dct.core.dao: debug

download:
  directory: /home/apps/museum/cms

  

5.打包服务,idea直接 mvn clean install -U 即可,并将jar文件拷贝到服务器中指定目录下,如图所示,以后更新时,也是替换jar。

 6.编辑Dockerfile文件,和sh文件,注意路径需要更改

FROM xuxiaoweicomcn/dragonwell17:17-x86_64

MAINTAINER wangqw "xxx@xx.com"

ENV APP_NAME cloudoer-dct-cms-1.0.0-SNAPSHOT.jar
ENV WORK_PATH /home/apps/museum/cms/
ENV JAVA_OPTS "-Xms512m -Xmx2048m"
ENV TZ=Asia/Shanghai

#VOLUME
VOLUME ["/home/apps/museum/cms/logs", "/tmp/home"]

ADD $APP_NAME $WORK_PATH
#COPY . $WORK_PATH/
WORKDIR $WORK_PATH
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime
# CMD
CMD ["java","-jar","-Dspring.profiles.active=test", "cloudoer-dct-cms-1.0.0-SNAPSHOT.jar"]

EXPOSE 18408

  

#!/bin/sh
cd /home/apps/museum/cms
cp cloudoer-dct-cms-1.0.0-SNAPSHOT.jar cloudoer-dct-cms-1.0.0-SNAPSHOT.jar1
cp /home/apps/jar/cloudoer-dct-cms-1.0.0-SNAPSHOT.jar cloudoer-dct-cms-1.0.0-SNAPSHOT.jar

echo '================开始构建镜像=============='
#镜像名称
IMAGE_NAME='cloudoer-dct-cms'
#打包后在jenkins的地址
SOURCE_PATH='/home/apps/museum/cms'
echo IMAGE_NAME=$IMAGE_NAME

echo '================获取容器id=================='
CID=$(docker ps -a | grep "$IMAGE_NAME" | awk '{print $1}')
echo 容器id=$CID
 
echo '================获取镜像id=================='
IID=$(docker images | grep "$IMAGE_NAME" | awk '{print $3}')
echo 镜像id=$IID

if [ -n "$CID" ]
then
    echo "exist $IMAGE_NAME container,CID=$CID"
    #停止
    docker stop $IMAGE_NAME
    #删除容器
    docker rm $IMAGE_NAME
else
    echo "no exist $IMAGE_NAME container"
fi

if [ -n "$IID" ]
then
    echo "exist $IMAGE_NAME image,IID=$IID"
    #删除镜像
    docker rmi -f $IMAGE_NAME
    echo "delete $IMAGE_NAME image"
    #构建
	docker build -t $IMAGE_NAME -f $SOURCE_PATH/Dockerfile .
    echo "build $IMAGE_NAME image"
else
    echo "no exist $IMAGE_NAME image,build docker"
    #构建
    docker build -t $IMAGE_NAME -f $SOURCE_PATH/Dockerfile .
    echo "build $IMAGE_NAME image"
fi

docker run -p 18408:18408 -d --name $IMAGE_NAME --net=host -v /home/apps/workspace/cms:/home/cms/temp/ -v /home/apps/logs/cms:$SOURCE_PATH/logs/ $IMAGE_NAME

  7.依次执行sh即可

 

posted @ 2024-09-13 13:27  蔡徐坤1987  阅读(38)  评论(0编辑  收藏  举报