DevOps

[Docker] SpringBoot Application 배포 스크립트(docker 버전)

nineDeveloper 2020. 5. 1. 18:51
728x90

jar 파일 Docker 적용 스크립트

CI를 통해 생성된 jar 파일에 대해서 기존 Docker 컨테이너와 이미지를 제거하고
새로운 Docker 이미지 생성 및 컨테이너를 생성하여 서버를 구동하는 스크립트

스크립트 처리를 위해 CI를 통해 생성된 jar파일을 $APPLICATION_NAME 파일명으로 복사하여 $IMAGE_NAME:$VERSION로 이미지를 생성

기존에 구동되고 있는 컨테이너와 이미지가 있다면 모두 제거하고 새롭게 생성하여 구동한다

사용방법

CI를 통해 배포된 경로 /home/centos/app 경로에 각 스크립트 파일을 실행하면 된다

자동 적용 스크립트 사용법 (auto_apply.sh)

$ ./apply.sh 파일명.jar

수동 적용 스크립트 사용법 (apply.sh)

$ ./apply.sh 파일명.jar

예시

$ ./apply.sh imis-v2-0.0.1_190919023615.jar

Dockerfile

FROM azul/zulu-openjdk:14

ARG APPLICATION_NAME
ARG DEPLOY_NAME
ARG SPRING_PROFILE

# 언어 설정
RUN apt-get update && apt-get install -y language-pack-ko
RUN locale-gen ko_KR.UTF-8
RUN update-locale LC_ALL=ko_KR.UTF-8 LANG=ko_KR.UTF-8
#ENV LANG ko_KR.UTF-8
#ENV LANGUAGE ko_KR.UTF-8
#ENV LC_ALL ko_KR.UTF-8

ENV APPLICATION_NAME=${APPLICATION_NAME:-app.jar}
ENV DEPLOY_NAME=${DEPLOY_NAME:-deploy.jar}
ENV SPRING_PROFILE=${SPRING_PROFILE:-dev}

# TimeZone 설정
#ENV TZ=Asia/Seoul
#RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

RUN mkdir /iparking
RUN mkdir /iparking/dump

ADD ${DEPLOY_NAME} ${APPLICATION_NAME}
ENV JAVA_OPTS=""

RUN echo "APPLICATION_NAME = ${APPLICATION_NAME}"
RUN echo "DEPLOY_NAME = ${DEPLOY_NAME}"

CMD java -jar "${APPLICATION_NAME}" --spring.profiles.active="${SPRING_PROFILE}"

Dockerfile (운영용)

FROM azul/zulu-openjdk:14

ARG APPLICATION_NAME
ARG DEPLOY_NAME
ARG SPRING_PROFILE

# 언어 설정
RUN apt-get update && apt-get install -y language-pack-ko
RUN locale-gen ko_KR.UTF-8
RUN update-locale LC_ALL=ko_KR.UTF-8 LANG=ko_KR.UTF-8
#ENV LANG ko_KR.UTF-8
#ENV LANGUAGE ko_KR.UTF-8
#ENV LC_ALL ko_KR.UTF-8

ENV APPLICATION_NAME=${APPLICATION_NAME:-app.jar}
ENV DEPLOY_NAME=${DEPLOY_NAME:-deploy.jar}
ENV SPRING_PROFILE=${SPRING_PROFILE:-dev}

# TimeZone 설정
#ENV TZ=Asia/Seoul
#RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

RUN mkdir /iparking
RUN mkdir /iparking/dump

ADD ${DEPLOY_NAME} ${APPLICATION_NAME}
ENV JAVA_OPTS=""

RUN echo "APPLICATION_NAME = ${APPLICATION_NAME}"
RUN echo "DEPLOY_NAME = ${DEPLOY_NAME}"

CMD java -jar "${APPLICATION_NAME}" -Duser.timezone=Asia/Seoul --spring.profiles.active="${SPRING_PROFILE}" -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/iparking/dump

자동 적용 스크립트 (auto_apply.sh)

Jenkins Send files or execute commands over SSH을 통해 자동적용을 사용할때 사용하는 자동적용 스크립트 파일

# 소스 도커 자동적용 및 자동 컨테이너 시작 스크립트
# 사용예시
#  Dockerfile에 명시된 파일명으로 변환하여 도커 컨테이너로 기동
# ./auto_apply.sh

DEPLOY_NAME="deploy.jar"

## HOME 디렉토리 명
HOME_DIRECTORY="backoffice"

## SERVICE DIRECTORY(현재 서비스 디렉토리명)
SERVICE_DIRECTORY="tis-api/stage"

## SERVICE 명(jar 파일 이름 앞자리와 동일해야함)
SERVICE_NAME="tis-api"

## Spring Profile
SPRING_PROFILE="stage"

## 외부에 제공되는 포트
HOST_PORT=9001
## Docker Container 포트
CONTAINER_PORT=9001

## Tag 버전명
VERSION="lts"

## IMAGE 명
IMAGE_NAME="${SERVICE_NAME}-${SPRING_PROFILE}"

APPLICATION_HOME="/home/${HOME_DIRECTORY}/${SERVICE_DIRECTORY}"
APPLICATION_NAME="${SERVICE_NAME}.jar"

cd ${APPLICATION_HOME}
FILE_NAME=`ls ${SERVICE_NAME}*.jar -ltr | tail -1 | awk '{ print $9 }'`
echo 'AUTO APPLY FILE_NAME: '$FILE_NAME

echo "======= 경로확인 ======="
echo "pwd"
pwd
echo "======= START ======="
if [ $FILE_NAME ];then
  if [ -f $FILE_NAME ];then

    echo "1. remove previous $DEPLOY_NAME"
    if [ -f $DEPLOY_NAME ];then

      echo "EXIST $DEPLOY_NAME -----------------"
      echo "2. rename $FILE_NAME -> $DEPLOY_NAME"
      EXIST_COUNT=`ls *.jar | grep -v "$DEPLOY_NAME" | wc -l`
      echo "EXIST_COUNT=$EXIST_COUNT"
      if [ $EXIST_COUNT -gt 0 ];then
        echo "rm $DEPLOY_NAME"
        rm $DEPLOY_NAME
        echo "cp $FILE_NAME $DEPLOY_NAME"
        cp $FILE_NAME $DEPLOY_NAME
      fi
    else
      echo "NOT EXIST $DEPLOY_NAME--------------"
      echo "2. rename $FILE_NAME -> $DEPLOY_NAME"
      EXIST_COUNT=`ls *.jar | grep -v "$DEPLOY_NAME" | wc -l`
      echo "EXIST_COUNT=$EXIST_COUNT"
      if [ $EXIST_COUNT -gt 0 ];then
        echo "cp $FILE_NAME $DEPLOY_NAME"
        cp $FILE_NAME $DEPLOY_NAME
      fi
    fi

    CONTAINER_ID=`docker ps -af ancestor=${IMAGE_NAME}:${VERSION} --format "{{.ID}}"`
    echo "CONTAINER_ID=$CONTAINER_ID"
    echo "---------------------"
    echo "3. Docker container stop"
    if [ $CONTAINER_ID ];then
      echo "docker stop $CONTAINER_ID"
      docker stop $CONTAINER_ID
      echo "---------------------"
      echo "4. Docker container remove"
      #docker rm `sudo docker ps -a -q`
      echo "docker rm $CONTAINER_ID"
      docker rm $CONTAINER_ID
    else
      echo "CONTAINER is Empty pass..."
    fi
    echo "---------------------"
    echo "5. Docker image remove"
    IMAGE_ID=`docker images -f=reference=${IMAGE_NAME}':*' --format "{{.ID}}"`
    echo "IMAGE_ID=$IMAGE_ID"
    if [ $IMAGE_ID ];then
      echo "docker rmi -f $IMAGE_ID"
      docker rmi -f $IMAGE_ID
    else
      echo "IMAGE is Empty pass..."
    fi
    echo "---------------------"
    echo "6. zip log delete"

    echo "---------------------"
    echo "7. Docker build"
    echo "docker build --build-arg APPLICATION_NAME="${APPLICATION_NAME}" --build-arg DEPLOY_NAME="${DEPLOY_NAME}" --tag ${IMAGE_NAME}:${VERSION} ./"
    docker build --build-arg APPLICATION_NAME="${APPLICATION_NAME}" --build-arg DEPLOY_NAME="${DEPLOY_NAME}" --build-arg SPRING_PROFILE=${SPRING_PROFILE}  --tag ${IMAGE_NAME}:${VERSION} ./

    echo "---------------------"
    echo "8. Docker create image & container start"

    echo "docker run -itd -p $HOST_PORT:$CONTAINER_PORT --name ${IMAGE_NAME} -v $APPLICATION_HOME/log:/log -v $APPLICATION_HOME/iparking:/iparking:rw  -v /etc/localtime:/etc/localtime:ro -e TZ=Asia/Seoul ${IMAGE_NAME}:${VERSION}"
    docker run -itd -p $HOST_PORT:$CONTAINER_PORT --name ${IMAGE_NAME} -v $APPLICATION_HOME/log:/log -v $APPLICATION_HOME/iparking:/iparking:rw -v /etc/localtime:/etc/localtime:ro -e TZ=Asia/Seoul ${IMAGE_NAME}:${VERSION}

    echo "rm $DEPLOY_NAME"
    rm $DEPLOY_NAME

    ## 하루 지난 파일은 삭제
    #find $APPLICATION_HOME/*.jar -ctime +0 -exec sudo rm -f {} \;
    ## 두시간 지난 파일은 삭제
    find $APPLICATION_HOME/*.jar -cmin +120 -exec sudo rm -f {} \;

    echo "======= Complete ======="
  else
    echo "$FILE_NAME is not Exist"
    echo "======= FAIL ======="
  fi
else
  echo "FILE_NAME is Empty input FILE_NAME please .. "
  echo "======= FAIL ======="
fi

수동 적용 스크립트 (apply.sh)

일반적인 적용 스크립트 개발자가 수동으로 적용 파일명을 입력하여 사용

# 소스 도커 적용 및 컨테이너 시작 스크립트
# 사용예시
#  Dockerfile에 명시된 파일명으로 변환하여 도커 컨테이너로 기동
# ./apply.sh 적용파일명
# ./apply.sh  imis-v2-0.0.1_190919023615.jar

DEPLOY_NAME="deploy.jar"
FILE_NAME=$1

## HOME 디렉토리 명
HOME_DIRECTORY="centos"

## SERVICE DIRECTORY(현재 서비스 디렉토리명)
SERVICE_DIRECTORY="tis-api/dev"

## SERVICE 명(jar 파일 이름 앞자리와 동일해야함)
SERVICE_NAME="tis-api"

## Spring Profile
SPRING_PROFILE="dev"

## 외부에 제공되는 포트
HOST_PORT=9001
## Docker Container 포트
CONTAINER_PORT=9001

## Tag 버전명
VERSION="lts"

## IMAGE 명
IMAGE_NAME="${SERVICE_NAME}-${SPRING_PROFILE}"

APPLICATION_HOME="/home/${HOME_DIRECTORY}/${SERVICE_DIRECTORY}"
APPLICATION_NAME="${SERVICE_NAME}.jar"

echo "======= 경로확인 ======="
echo "pwd"
pwd
echo "======= START ======="
if [ $FILE_NAME ];then
  if [ -f $FILE_NAME ];then

    echo "1. remove previous $DEPLOY_NAME"
    if [ -f $DEPLOY_NAME ];then

      echo "EXIST $DEPLOY_NAME -----------------"
      echo "2. rename $FILE_NAME -> $DEPLOY_NAME"
      EXIST_COUNT=`ls *.jar | grep -v "$DEPLOY_NAME" | wc -l`
      echo "EXIST_COUNT=$EXIST_COUNT"
      if [ $EXIST_COUNT -gt 0 ];then
        echo "rm $DEPLOY_NAME"
        rm $DEPLOY_NAME
        echo "cp $FILE_NAME $DEPLOY_NAME"
        cp $FILE_NAME $DEPLOY_NAME
      fi
    else
      echo "NOT EXIST $DEPLOY_NAME--------------"
      echo "2. rename $FILE_NAME -> $DEPLOY_NAME"
      EXIST_COUNT=`ls *.jar | grep -v "$DEPLOY_NAME" | wc -l`
      echo "EXIST_COUNT=$EXIST_COUNT"
      if [ $EXIST_COUNT -gt 0 ];then
        echo "cp $FILE_NAME $DEPLOY_NAME"
        cp $FILE_NAME $DEPLOY_NAME
      fi
    fi

    CONTAINER_ID=`docker ps -af ancestor=${IMAGE_NAME}:${VERSION} --format "{{.ID}}"`
    echo "CONTAINER_ID=$CONTAINER_ID"
    echo "---------------------"
    echo "3. Docker container stop"
    if [ $CONTAINER_ID ];then
      echo "docker stop $CONTAINER_ID"
      docker stop $CONTAINER_ID
      echo "---------------------"
      echo "4. Docker container remove"
      #docker rm `sudo docker ps -a -q`
      echo "docker rm $CONTAINER_ID"
      docker rm $CONTAINER_ID
    else
      echo "CONTAINER is Empty pass..."
    fi
    echo "---------------------"
    echo "5. Docker image remove"
    IMAGE_ID=`docker images -f=reference=${IMAGE_NAME}':*' --format "{{.ID}}"`
    echo "IMAGE_ID=$IMAGE_ID"
    if [ $IMAGE_ID ];then
      echo "docker rmi -f $IMAGE_ID"
      docker rmi -f $IMAGE_ID
    else
      echo "IMAGE is Empty pass..."
    fi
    echo "---------------------"
    echo "6. zip log delete"

    echo "---------------------"
    echo "7. Docker build"
    echo "docker build --build-arg APPLICATION_NAME="${APPLICATION_NAME}" --build-arg DEPLOY_NAME="${DEPLOY_NAME}" --tag ${IMAGE_NAME}:${VERSION} ./"
    docker build --build-arg APPLICATION_NAME="${APPLICATION_NAME}" --build-arg DEPLOY_NAME="${DEPLOY_NAME}" --build-arg SPRING_PROFILE=${SPRING_PROFILE}  --tag ${IMAGE_NAME}:${VERSION} ./

    echo "---------------------"
    echo "8. Docker create image & container start"

    echo "docker run -itd -p $HOST_PORT:$CONTAINER_PORT --name ${IMAGE_NAME} -v $APPLICATION_HOME/log:/log -v $APPLICATION_HOME/iparking:/iparking:rw  -v /etc/localtime:/etc/localtime:ro -e TZ=Asia/Seoul ${IMAGE_NAME}:${VERSION}"
    docker run -itd -p $HOST_PORT:$CONTAINER_PORT --name ${IMAGE_NAME} -v $APPLICATION_HOME/log:/log -v $APPLICATION_HOME/iparking:/iparking:rw -v /etc/localtime:/etc/localtime:ro -e TZ=Asia/Seoul ${IMAGE_NAME}:${VERSION}

    echo "rm $DEPLOY_NAME"
    rm $DEPLOY_NAME

    ## 하루 지난 파일은 삭제
    #find $APPLICATION_HOME/*.jar -ctime +0 -exec sudo rm -f {} \;
    ## 두시간 지난 파일은 삭제
    find $APPLICATION_HOME/*.jar -cmin +120 -exec sudo rm -f {} \;

    echo "======= Complete ======="
  else
    echo "$FILE_NAME is not Exist"
    echo "======= FAIL ======="
  fi
else
  echo "FILE_NAME is Empty input FILE_NAME please .. "
  echo "======= FAIL ======="
fi
728x90