[CI/CD] Jenkins + Docker๋ฅผ ์ด์šฉํ•œ ๋ฌด์ค‘๋‹จ ๋ฐฐํฌ - โ‘ก Create Docker Image And BackUp

2023. 7. 5. 18:31ใ†System ์ž‘์—…์‹ค/DevOps

728x90
๋ฐ˜์‘ํ˜•

 

 

 

 




๐Ÿ—‚ ๋ชฉ์ฐจ

โš ๏ธ ์•„๋ž˜ ๋ชฉ์ฐจ ์ค‘ ๋ช‡๋ช‡๊ฐœ์˜ ๋งํฌ๊ฐ€ ๊ฑธ๋ฆฌ์ง€ ์•Š๋Š” ๋ฌธ์ œ๋กœ ๊ธ€ ๋งจ ํ•˜๋‹จ์— ๋‹ค์Œ ๊ธ€๋กœ ์ด๋™ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด ๋‘์—ˆ์Šต๋‹ˆ๋‹ค.

โœ… [CI/CD] Jenkins์™€ Gitea ์—ฐ๋™
โœ… [CI/CD] Jenkins Trigger ์ •๋ณด Discord๋กœ ๋ณด๋‚ด๊ธฐ
โœ… [CI/CD] ์ •์  ์ฝ”๋“œ ๋ถ„์„ ํˆด SonarQube์™€ Jenkins ์—ฐ๋™
โœ… [CI/CD] SonarQube๋ฅผ ํ†ตํ•ด Code Convention ์ ์šฉ
โœ… [DevOps] JAVA Gradle JaCoCo (Code coverage) ์„ค์ •ํ•˜๊ธฐ 
โœ… [DevOps] JAVA Gradle JaCoCo (Code coverage) ์„ค์ •ํ•˜๊ธฐ (์ถ”๊ฐ€)(https://junyharang.tistory.com/392)

โœ… [CI/CD] Jenkins + Docker๋ฅผ ์ด์šฉํ•œ ๋ฌด์ค‘๋‹จ ๋ฐฐํฌ - โ‘  Application Linuxt(Ubuntu)์— SSH๋ฅผ ์ด์šฉํ•œ ํŒŒ์ผ ์ „์†ก
โœ… [CI/CD] Jenkins + Docker๋ฅผ ์ด์šฉํ•œ ๋ฌด์ค‘๋‹จ ๋ฐฐํฌ - โ‘ก Create Docker Image And BackUp
โœ… [CI/CD] Jenkins + Docker๋ฅผ ์ด์šฉํ•œ ๋ฌด์ค‘๋‹จ ๋ฐฐํฌ - โ‘ข Application Server Docker Job (โ‘  Application ๋„์ปค ์กด์žฌ ์—ฌ๋ถ€ ํ™•์ธ)
โœ… [CI/CD] Jenkins + Docker๋ฅผ ์ด์šฉํ•œ ๋ฌด์ค‘๋‹จ ๋ฐฐํฌ - โ‘ข Application Server Docker Job (โ‘ก Application Docker Run)(https://junyharang.tistory.com/406)
โœ… [CI/CD] Jenkins + Docker๋ฅผ ์ด์šฉํ•œ ๋ฌด์ค‘๋‹จ ๋ฐฐํฌ - โ‘ข Application Server Docker Job (โ‘ข Application Docker Health Check)
โœ… [CI/CD] Jenkins + Docker๋ฅผ ์ด์šฉํ•œ ๋ฌด์ค‘๋‹จ ๋ฐฐํฌ - โ‘ฃ NGINX Server Docker Job (โ‘  NGINX ๊ฐ ์ข… ์„ค์ •) 
โœ… [CI/CD] Jenkins + Docker๋ฅผ ์ด์šฉํ•œ ๋ฌด์ค‘๋‹จ ๋ฐฐํฌ - โ‘ฃ NGINX Server Docker Job (โ‘ก NGINX Docker ์กด์žฌ ์—ฌ๋ถ€ ํ™•์ธ) 
โœ… [CI/CD] Jenkins + Docker๋ฅผ ์ด์šฉํ•œ ๋ฌด์ค‘๋‹จ ๋ฐฐํฌ - โ‘ฃ NGINX Server Docker Job (โ‘ข NGINX Docker Run & Health Check)
โœ… [CI/CD] Jenkins + Docker๋ฅผ ์ด์šฉํ•œ ๋ฌด์ค‘๋‹จ ๋ฐฐํฌ - โ‘ฃ NGINX Server Docker Job (โ‘ข NGINX ์žฌ ์„ค์ •)



๐Ÿค” ๋‚ด๊ฐ€ ๋งŒ๋‚œ ๋ฌธ์ œ

โ›”๏ธ [Jenkins] java.lang.OutOfMemoryError: Java heap space

 

 

 

 

๋ฐฐํฌ ์ž๋™ํ™”์™€ ์ง€์†์  ์ธ๋„:๋„์ปค์™€ ์  ํ‚จ์Šค ์ฟ ๋ฒ„๋„คํ‹ฐ์Šค๋กœ ๋งŒ๋“œ๋Š”

COUPANG

www.coupang.com

"์ด ํฌ์ŠคํŒ…์€ ์ฟ ํŒก ํŒŒํŠธ๋„ˆ์Šค ํ™œ๋™์˜ ์ผํ™˜์œผ๋กœ, ์ด์— ๋”ฐ๋ฅธ ์ผ์ •์•ก์˜ ์ˆ˜์ˆ˜๋ฃŒ๋ฅผ ์ œ๊ณต๋ฐ›์Šต๋‹ˆ๋‹ค."

 

 

 

 

 

๐Ÿš€ ์  ํ‚จ์Šค๋ฅผ ์ด์šฉํ•œ ๋„์ปค ์ด๋ฏธ์ง€ ๋งŒ๋“ค๊ธฐ ๋ฐ ๋ฐฑ์—…

    ๐Ÿ”ฝ ๊ฐœ์š”

        ๐Ÿ“ฆ ๊ตฌ์„ฑ๋„

CI/CD ๋ฌด์ค‘๋‹จ ๋ฐฐํฌ ๊ตฌ์„ฑ๋„



deploy Directory Path






        ๐Ÿ“ฆ ์†Œ๊ฐœ

์ด๋ฒˆ ๊ธ€์—์„œ๋Š” Spring Boot Application ๋„์ปค ์ด๋ฏธ์ง€๋ฅผ ๋งŒ๋“ค๊ณ , ์ด๋ฅผ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๊ตฌ๋™ ๋Œ€์ƒ ์„œ๋ฒ„ ํ˜ธ์ŠคํŠธ ํŠน์ • ๋””๋ ‰ํ† ๋ฆฌ์— Archive๋กœ Backup ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์ •๋ฆฌํ•ด ๋ณด๋ ค๊ณ  ํ•ด์š”.

์‚ฌ์‹ค ๋ฐฑ์—… ๋ถ€๋ถ„์€ Docker Hub์— ์ด๋ฏธ์ง€๋ฅผ ๋งŒ๋“ค๊ณ , Push ํ•˜๊ณ , ์‚ฌ์šฉํ•  ๋•Œ ๋‚ด๋ ค๋ฐ›๊ฒŒ ํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ, ์ผ๋‹จ ๋„์ปค ํ—ˆ๋ธŒ๋Š” ํ•˜๋‚˜ ์ด ์™ธ์— ์—ฌ๋Ÿฌ Private Repository๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๋ˆ์„ ๋‚ด์•ผ ํ•˜๊ณ ,

๋˜ํ•œ, ๊ตณ์ด ์™ธ๋ถ€ ํ™˜๊ฒฝ์— ์ฃผ๋‹ˆ์˜ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์˜ฌ๋ผ๊ฐ€๋Š” ๊ฒƒ์„ ์›์น˜ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์ด ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•ด ๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. 

 

 

 

    ๐Ÿ”ฝ ์  ํ‚จ์Šค

        ๐Ÿ“ฆ Jenkinsfile

jenkinsfile 407 ~ 447๋ฒˆ์งธ ์ค„


์  ํ‚จ์Šค ํŒŒ์ผ์— SSH ๊ด€๋ จ ์„ค์ • ์„ธ๋ถ€ ๋‚ด์—ญ์€ ์ด  ์ „๊ธ€์„ ํ†ตํ•ด ์•„์‹ค ์ˆ˜ ์žˆ์–ด์š”.

 

[Jenkins] Publish Over SSH Plugin์„ ์ด์šฉํ•œ ๋Œ€์ƒ ์„œ๋ฒ„ SSH ์ด์šฉ

๋ฐฐํฌ ์ž๋™ํ™”์™€ ์ง€์†์  ์ธ๋„:๋„์ปค์™€ ์  ํ‚จ์Šค ์ฟ ๋ฒ„๋„คํ‹ฐ์Šค๋กœ ๋งŒ๋“œ๋Š” COUPANG www.coupang.com "์ด ํฌ์ŠคํŒ…์€ ์ฟ ํŒก ํŒŒํŠธ๋„ˆ์Šค ํ™œ๋™์˜ ์ผํ™˜์œผ๋กœ, ์ด์— ๋”ฐ๋ฅธ ์ผ์ •์•ก์˜ ์ˆ˜์ˆ˜๋ฃŒ๋ฅผ ์ œ๊ณต๋ฐ›์Šต๋‹ˆ๋‹ค." ๐Ÿš€ ์  ํ‚จ์Šค Publish

junyharang.tistory.com



์ด ๋ถ€๋ถ„์—์„œ ํ•ต์‹ฌ ๋‚ด์šฉ์€ ๋ฐ”๋กœ 422๋ฒˆ์งธ ์ค„์ธ๋ฐ, 

jenkinsfile 60๋ฒˆ์งธ ์ค„


์ „์—ญ ๋ณ€์ˆ˜ ์„ค์ •ํ•˜๋Š” stage์—์„œ ์œ„์™€ ๊ฐ™์ด DOCKER_SERVER_BASE_DIRECTORY๋ฅผ ๋ฌธ์ž์—ด๋กœ Path ์žก์•„ ์ฃผ์—ˆ๊ณ , 

์ด๋ฅผ ๋ฐ›์•„์„œ ํ•˜์œ„์— deploy/prod/was/shell-script/createDockerImageAndBackup.sh๋ฅผ ์‹คํ–‰ ์‹œํ‚ค๋„๋ก ํ•ด ์ฃผ์—ˆ์–ด์š”.


 

 



    ๐Ÿ”ฝ Shell Script - createDockerImageAndBackup.sh

        ๐Ÿ“ฆ checkLogDirectory()

์ฃผ๋‹ˆ๊ฐ€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋„์ปค ์ด๋ฏธ์ง€ ์ƒ์„ฑ ๋ฐ ๋ฐฑ์—…์„ ์œ„ํ•ด ์ž‘์„ฑํ•œ ์‰˜ ์Šคํฌ๋ฆฝํŠธ๋Š” ์•„๋ž˜์™€ ๊ฐ™์•„์š”.

#!/bin/bash

set -e

NOW=$(date +"%y-%m-%d_%H:%M:%S")
SAVE_LOG_DATE=$(date +"%y-%m-%d")

APPLICATION_DOCKER_IMAGE_NAME="giggal-people/total-back-office-api"

#Application Docker File ๊ฒฝ๋กœ
APPLICATION_DOCKER_FILE_PATH="/data/deploy/giggal-total-back-office/deploy/prod/was/docker"

APPLICATION_DOCKER_BACKUP_DIR="/data/deploy/giggal-total-back-office/deploy/prod/backup/application"

echo "[$NOW] [INFO] ๊ธฐ๊น”๋‚˜๋Š” ์‚ฌ๋žŒ๋“ค ํ†ตํ•ฉ ๊ด€๋ฆฌ ์„œ๋ฒ„ API ๋„์ปค ์ด๋ฏธ์ง€ ์ƒ์„ฑ ๋ฐ ๋„์ปค ๋ฐฑ์—… ์ž‘์—…์ด ์‹œ์ž‘ ๋˜์—ˆ์–ด์š”."
echo "======================================[$NOW] ํ†ตํ•ฉ ๋ฐฑ ์˜คํ”ผ์Šค ๋„์ปค ์ด๋ฏธ์ง€ ์ƒ์„ฑ ๋ฐ ๋„์ปค ๋ฐฑ์—…======================================"
echo "[$NOW] [INFO] Author(๋งŒ๋“ ์ด): ์ฃผ๋‹ˆ(junyharang8592@gmail.com)"

checkLogDirectory() {
  sleep 5

  LOG_DIR="/var/log/deploy/giggal-total-back-office"

  if [ -d "$LOG_DIR" ];
  then
    echo "[$NOW] [INFO] ๊ธฐ๊น”๋‚˜๋Š” ์‚ฌ๋žŒ๋“ค ํ†ตํ•ฉ ๊ด€๋ฆฌ ์„œ๋ฒ„ API ๋„์ปค ์ด๋ฏธ์ง€ ์ƒ์„ฑ ๋ฐ ๋„์ปค ๋ฐฑ์—… ์ž‘์—…์ด ์‹œ์ž‘ ๋˜์—ˆ์–ด์š”." >> $LOG_DIR/"$SAVE_LOG_DATE"-createImageAndBackup.log 2>&1
    echo "======================================[$NOW] ํ†ตํ•ฉ ๋ฐฑ ์˜คํ”ผ์Šค ๋„์ปค ์ด๋ฏธ์ง€ ์ƒ์„ฑ ๋ฐ ๋„์ปค ๋ฐฑ์—…======================================" >> $LOG_DIR/"$SAVE_LOG_DATE"-createImageAndBackup.log 2>&1
    echo "[$NOW] [INFO] Author(๋งŒ๋“ ์ด): ์ฃผ๋‹ˆ(junyharang8592@gmail.com)" >> $LOG_DIR/"$SAVE_LOG_DATE"-createImageAndBackup.log 2>&1
    echo "[$NOW] [INFO] LOG Directory ์กด์žฌ ํ•ฉ๋‹ˆ๋‹ค."
    echo "[$NOW] [INFO] LOG Directory ์กด์žฌ ํ•ฉ๋‹ˆ๋‹ค." >> $LOG_DIR/"$SAVE_LOG_DATE"-createImageAndBackup.log 2>&1

  else
      echo "[$NOW] [INFO] cicd-admin์€ mkdir ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์–ด์š”. ๊ด€๋ฆฌ์ž ํ˜น์€ DMSO ํฌ๋ฃจ์—๊ฒŒ ${LOG_DIR} ์ƒ์„ฑ์„ ์š”์ฒญํ•ด ์ฃผ์„ธ์š”. ์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์ข…๋ฃŒ๋ฉ๋‹ˆ๋‹ค."
      exit 1
  fi

  unknownNameImageDelete
}

unknownNameImageDelete() {
  sleep 5
  echo "[$NOW] [INFO] ์ด๋ฆ„ ์—†๋Š” (๊ณ ์•„) Docker Image ์‚ญ์ œ ์ž‘์—… ์‹œ์ž‘ํ• ๊ฒŒ์š”."
  echo "[$NOW] [INFO] ์ด๋ฆ„ ์—†๋Š” (๊ณ ์•„) Docker Image ์‚ญ์ œ ์ž‘์—… ์‹œ์ž‘ํ• ๊ฒŒ์š”." >> $LOG_DIR/"$SAVE_LOG_DATE"-createImageAndBackup.log 2>&1

  checkDockerImage

  if ! docker images | grep "^<none>";
  then
    echo "[$NOW] [INFO] ์ด๋ฆ„ ์—†๋Š” (๊ณ ์•„) Docker Image๊ฐ€ ์กด์žฌ ํ•˜์ง€ ์•Š์•„์š”."
    echo "[$NOW] [INFO] ์ด๋ฆ„ ์—†๋Š” (๊ณ ์•„) Docker Image๊ฐ€ ์กด์žฌ ํ•˜์ง€ ์•Š์•„์š”." >> $LOG_DIR/"$SAVE_LOG_DATE"-createImageAndBackup.log 2>&1

    checkDockerImage

  else
    if ! docker rmi $(docker images -f "dangling=true" -q);
    then
      echo "[$NOW] [WARN] ์ด๋ฆ„ ์—†๋Š” Docker Image ์‚ญ์ œ ์ž‘์—… ์‹คํŒจํ•˜์˜€์–ด์š”. Server์— ์ ‘์†ํ•˜์—ฌ ์ง์ ‘ ์‚ญ์ œ ์ž‘์—…์ด ํ•„์š”ํ•ด์š”. ์Šคํฌ๋ฆฝํŠธ๋Š” ์ข…๋ฃŒ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค."
      echo "[$NOW] [WARN] ์ด๋ฆ„ ์—†๋Š” Docker Image ์‚ญ์ œ ์ž‘์—… ์‹คํŒจํ•˜์˜€์–ด์š”. Server์— ์ ‘์†ํ•˜์—ฌ ์ง์ ‘ ์‚ญ์ œ ์ž‘์—…์ด ํ•„์š”ํ•ด์š”. ์Šคํฌ๋ฆฝํŠธ๋Š” ์ข…๋ฃŒ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค." >> $LOG_DIR/"$SAVE_LOG_DATE"-createImageAndBackup.log 2>&1

    else
      echo "[$NOW] [INFO] ์ด๋ฆ„ ์—†๋Š” Docker Image ์‚ญ์ œ ์ž‘์—… ์„ฑ๊ณตํ•˜์˜€์–ด์š”."
      echo "[$NOW] [INFO] ์ด๋ฆ„ ์—†๋Š” Docker Image ์‚ญ์ œ ์ž‘์—… ์„ฑ๊ณตํ•˜์˜€์–ด์š”." >> $LOG_DIR/"$SAVE_LOG_DATE"-createImageAndBackup.log 2>&1

      checkDockerImage
    fi
  fi

  createdApplicationDockerImage
}

checkDockerImage() {
  DOCKER_IMAGES_LIST=$(docker images)

  echo "[$NOW] [INFO] ํ˜„์žฌ ์กด์žฌํ•˜๋Š” Docker Image ๋‚ด์—ญ : "
  echo "[$NOW] [INFO] ํ˜„์žฌ ์กด์žฌํ•˜๋Š” Docker Image ๋‚ด์—ญ : " >> $LOG_DIR/"$SAVE_LOG_DATE"-createImageAndBackup.log 2>&1
  echo "[$NOW] [INFO] ${DOCKER_IMAGES_LIST} "
  echo "[$NOW] [INFO] ${DOCKER_IMAGES_LIST} " >> $LOG_DIR/"$SAVE_LOG_DATE"-createImageAndBackup.log 2>&1
}

createdApplicationDockerImage() {
  sleep 5
  echo "[$NOW] [INFO] Docker Image ์ƒ์„ฑ ์ž‘์—…์„ ์œ„ํ•ด docker Image ์ด๋ฆ„์— ๋Œ€ํ•œ ๋ณ€์ˆ˜ ์„ค์ • ์ž‘์—…์„ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค."
  echo "[$NOW] [INFO] Docker Image ์ƒ์„ฑ ์ž‘์—…์„ ์œ„ํ•ด docker Image ์ด๋ฆ„์— ๋Œ€ํ•œ ๋ณ€์ˆ˜ ์„ค์ • ์ž‘์—…์„ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค." >> $LOG_DIR/"$SAVE_LOG_DATE"-createImageAndBackup.log 2>&1

  echo "[$NOW] [INFO] ${APPLICATION_DOCKER_IMAGE_NAME} Docker Image ์ƒ์„ฑ ์ž‘์—… ์‹œ์ž‘ํ• ๊ฒŒ์š”."
  echo "[$NOW] [INFO] ${APPLICATION_DOCKER_IMAGE_NAME} Docker Image ์ƒ์„ฑ ์ž‘์—… ์‹œ์ž‘ํ• ๊ฒŒ์š”." >> $LOG_DIR/"$SAVE_LOG_DATE"-createImageAndBackup.log 2>&1

  if ! docker build -t $APPLICATION_DOCKER_IMAGE_NAME $APPLICATION_DOCKER_FILE_PATH;

    then
      echo "[$NOW] [ERROR] ${APPLICATION_DOCKER_IMAGE_NAME} Docker Image ์ƒ์„ฑ ์ž‘์—… ์‹คํŒจํ•˜์˜€์–ด์š”. ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ข…๋ฃŒ ํ•ฉ๋‹ˆ๋‹ค."
      echo "[$NOW] [ERROR] ${APPLICATION_DOCKER_IMAGE_NAME} Docker Image ์ƒ์„ฑ ์ž‘์—… ์‹คํŒจํ•˜์˜€์–ด์š”. ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ข…๋ฃŒ ํ•ฉ๋‹ˆ๋‹ค." >> $LOG_DIR/"$SAVE_LOG_DATE"-createImageAndBackup.log 2>&1
      exit 1

    else

      if ! docker images | grep "$APPLICATION_DOCKER_IMAGE_NAME";
      then
        echo "[$NOW] [ERROR] ${APPLICATION_DOCKER_IMAGE_NAME} Docker Image ์ƒ์„ฑ ์ž‘์—… ์‹คํŒจํ•˜์˜€์–ด์š”. ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ข…๋ฃŒ ํ•ฉ๋‹ˆ๋‹ค."
        echo "[$NOW] [ERROR] ${APPLICATION_DOCKER_IMAGE_NAME} Docker Image ์ƒ์„ฑ ์ž‘์—… ์‹คํŒจํ•˜์˜€์–ด์š”. ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ข…๋ฃŒ ํ•ฉ๋‹ˆ๋‹ค." >> $LOG_DIR/"$SAVE_LOG_DATE"-createImageAndBackup.log 2>&1
        exit 1

      else
        echo "[$NOW] [INFO] ${APPLICATION_DOCKER_IMAGE_NAME} Docker Image ์ƒ์„ฑ ์ž‘์—… ์„ฑ๊ณตํ•˜์˜€์–ด์š”."
        echo "[$NOW] [INFO] ${APPLICATION_DOCKER_IMAGE_NAME} Docker Image ์ƒ์„ฑ ์ž‘์—… ์„ฑ๊ณตํ•˜์˜€์–ด์š”." >> $LOG_DIR/"$SAVE_LOG_DATE"-createImageAndBackup.log 2>&1

        applicationDockerImageBackUp
      fi
    fi
}

applicationDockerImageBackUp() {
  sleep 5

  echo "[$NOW] [INFO] ${APPLICATION_DOCKER_IMAGE_NAME} Docker Image Back Up ์ž‘์—… ์‹œ์ž‘ํ• ๊ฒŒ์š”."
  echo "[$NOW] [INFO] ${APPLICATION_DOCKER_IMAGE_NAME} Docker Image Back Up ์ž‘์—… ์‹œ์ž‘ํ• ๊ฒŒ์š”." >> $LOG_DIR/"$SAVE_LOG_DATE"-createImageAndBackup.log 2>&1

  checkBackupDirectory "$APPLICATION_DOCKER_BACKUP_DIR"
  cd $APPLICATION_DOCKER_BACKUP_DIR

  SAVE_DATE=$(date +"%y-%m-%d")

  if ! docker save -o "$SAVE_DATE"-giggal-total-back-office-api.tar "$APPLICATION_DOCKER_IMAGE_NAME";
  then
    echo "[$NOW] [ERROR] ${APPLICATION_DOCKER_IMAGE_NAME} Docker Image ๋ฐฑ์—… ์ž‘์—… ์‹คํŒจํ•˜์˜€์–ด์š”."
    echo "[$NOW] [ERROR] ${APPLICATION_DOCKER_IMAGE_NAME} Docker Image ๋ฐฑ์—… ์ž‘์—… ์‹คํŒจํ•˜์˜€์–ด์š”." >> $LOG_DIR/"$SAVE_LOG_DATE"-createImageAndBackup.log 2>&1

    DIRECTORY_OWNER_CHECK=$(ls -al ./)

    echo "[$NOW] [ERROR] ${DIRECTORY_OWNER_CHECK} "
    echo "[$NOW] [ERROR] ${DIRECTORY_OWNER_CHECK} " >> $LOG_DIR/"$SAVE_LOG_DATE"-createImageAndBackup.log 2>&1
    echo "[$NOW] [ERROR] ๊ฒฝ๋กœ์˜ ์†Œ์œ ์ž์™€ ๊ทธ๋ฃน์ด cicd-admin์ด ์•„๋‹Œ ๊ฒฝ์šฐ ๊ถŒํ•œ ๋ฌธ์ œ๋กœ ์‹คํŒจ ํ•ฉ๋‹ˆ๋‹ค. ๊ด€๋ฆฌ์ž ํ˜น์€ DMSO์—๊ฒŒ ${APPLICATION_DOCKER_BACKUP_DIR} ๊ฒฝ๋กœ์˜ ์†Œ์œ ์ž์™€ ๊ทธ๋ฃน์„ cicd-admin์œผ๋กœ ๋ณ€๊ฒฝ ์š”์ฒญํ•ด ์ฃผ์„ธ์š”. ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ข…๋ฃŒ ํ•ฉ๋‹ˆ๋‹ค."
    echo "[$NOW] [ERROR] ๊ฒฝ๋กœ์˜ ์†Œ์œ ์ž์™€ ๊ทธ๋ฃน์ด cicd-admin์ด ์•„๋‹Œ ๊ฒฝ์šฐ ๊ถŒํ•œ ๋ฌธ์ œ๋กœ ์‹คํŒจ ํ•ฉ๋‹ˆ๋‹ค. ๊ด€๋ฆฌ์ž ํ˜น์€ DMSO์—๊ฒŒ ${APPLICATION_DOCKER_BACKUP_DIR} ๊ฒฝ๋กœ์˜ ์†Œ์œ ์ž์™€ ๊ทธ๋ฃน์„ cicd-admin์œผ๋กœ ๋ณ€๊ฒฝ ์š”์ฒญํ•ด ์ฃผ์„ธ์š”. ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ข…๋ฃŒ ํ•ฉ๋‹ˆ๋‹ค." >> $LOG_DIR/"$SAVE_LOG_DATE"-createImageAndBackup.log 2>&1
    exit 1

  else
    echo "[$NOW] [INFO] ${APPLICATION_DOCKER_IMAGE_NAME} Docker Image ๋ฐฑ์—… ์ž‘์—… ์„ฑ๊ณตํ•˜์˜€์–ด์š”."
    echo "[$NOW] [INFO] ${APPLICATION_DOCKER_IMAGE_NAME} Docker Image ๋ฐฑ์—… ์ž‘์—… ์„ฑ๊ณตํ•˜์˜€์–ด์š”." >> $LOG_DIR/"$SAVE_LOG_DATE"-createImageAndBackup.log 2>&1
  fi
}

checkBackupDirectory() {
  sleep 5
  local backupDirectoryPath=$1

  echo "[$NOW] [INFO] Docker Backup Directory ๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธํ•ด ๋ณผ๊ฒŒ์š”."
  echo "[$NOW] [INFO] Docker Backup Directory ๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธํ•ด ๋ณผ๊ฒŒ์š”." >> $LOG_DIR/"$SAVE_LOG_DATE"-createImageAndBackup.log 2>&1

  if [ -d "$backupDirectoryPath" ];
  then
    echo "[$NOW] [INFO] Docker Backup Directory ${backupDirectoryPath} ๊ฐ€ ์กด์žฌ ํ•ฉ๋‹ˆ๋‹ค."
    echo "[$NOW] [INFO] Docker Backup Directory ${backupDirectoryPath} ๊ฐ€ ์กด์žฌ ํ•ฉ๋‹ˆ๋‹ค." >> $LOG_DIR/"$SAVE_LOG_DATE"-createImageAndBackup.log 2>&1

  else
      echo "[$NOW] [INFO] cicd-admin์€ mkdir ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์–ด์š”. ๊ด€๋ฆฌ์ž ํ˜น์€ DMSO ํฌ๋ฃจ์—๊ฒŒ ${backupDirectoryPath} ์ƒ์„ฑ์„ ์š”์ฒญํ•ด ์ฃผ์„ธ์š”. ์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์ข…๋ฃŒ๋ฉ๋‹ˆ๋‹ค."
      echo "[$NOW] [INFO] cicd-admin์€ mkdir ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์–ด์š”. ๊ด€๋ฆฌ์ž ํ˜น์€ DMSO ํฌ๋ฃจ์—๊ฒŒ ${backupDirectoryPath} ์ƒ์„ฑ์„ ์š”์ฒญํ•ด ์ฃผ์„ธ์š”. ์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์ข…๋ฃŒ๋ฉ๋‹ˆ๋‹ค." >> $LOG_DIR/"$SAVE_LOG_DATE"-createImageAndBackup.log 2>&1
      exit 1
  fi
}

checkLogDirectory

echo "[$NOW] [INFO] ๊ธฐ๊น”๋‚˜๋Š” ์‚ฌ๋žŒ๋“ค ํ†ตํ•ฉ ๊ด€๋ฆฌ ์„œ๋ฒ„ API ๋„์ปค ์ด๋ฏธ์ง€ ์ƒ์„ฑ ๋ฐ ๋„์ปค ๋ฐฑ์—… LOG ์œ„์น˜ : /var/log/docker/giggal-total-back-office/api"
echo "[$NOW] [INFO] ๊ธฐ๊น”๋‚˜๋Š” ์‚ฌ๋žŒ๋“ค ํ†ตํ•ฉ ๊ด€๋ฆฌ ์„œ๋ฒ„ API ๋„์ปค ์ด๋ฏธ์ง€ ์ƒ์„ฑ ๋ฐ ๋„์ปค ๋ฐฑ์—… ์ž‘์—…์ด ๋๋‚ฌ์–ด์š”."
echo "[$NOW] [INFO] ๊ธฐ๊น”๋‚˜๋Š” ์‚ฌ๋žŒ๋“ค ํ†ตํ•ฉ ๊ด€๋ฆฌ ์„œ๋ฒ„ API ๋„์ปค ์ด๋ฏธ์ง€ ์ƒ์„ฑ ๋ฐ ๋„์ปค ๋ฐฑ์—… ์ž‘์—…์ด ๋๋‚ฌ์–ด์š”." >> $LOG_DIR/"$SAVE_LOG_DATE"-createImageAndBackup.log 2>&1
echo -e "=====================================================================================================\n"
echo -e "=====================================================================================================\n" >> $LOG_DIR/"$NOW"-deploy.log 2>&1
๋ฐ˜์‘ํ˜•

์‰˜ ์Šคํฌ๋ฆฝํŠธ์— ๋Œ€ํ•œ ์ „๋ฐ˜์ ์ธ ๋‚ด์šฉ์€ ์ด ๊ณณ์— ์ค€๋น„ํ•ด ๋‘์—ˆ์–ด์š”.


createDockerImageAndBackup.sh 19 ~ 38๋ฒˆ์งธ ์ค„


์ตœ์ดˆ Log ์ €์žฅ์„ ์œ„ํ•ด Log ์ €์žฅ Directory๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•ด ์ฃผ์—ˆ์–ด์š”.

์ •์ƒ์ ์œผ๋กœ ํ™•์ธ์ด ๋˜๋ฉด 37๋ฒˆ์งธ ์ค„์—์„œ unknownNameImageDelete()๋ฅผ ํ˜ธ์ถœ ํ•ฉ๋‹ˆ๋‹ค.

ํ•˜์ง€๋งŒ ๋งŒ์•ฝ ๋””๋ ‰ํ† ๋ฆฌ๊ฐ€ ์—†๋‹ค๋ฉด ํ•ด๋‹น ์Šคํฌ๋ฆฝํŠธ๋Š” ์ข…๋ฃŒ ํ•ด ๋ฒ„๋ฆด๊ฑฐ์—์š”.





        ๐Ÿ“ฆ unknownNameImageDelete()

createDockerImageAndBackup.sh 40 ~ 69๋ฒˆ์งธ ์ค„


์ด ํ•จ์ˆ˜์—์„œ๋Š” ๋„์ปค ์ด๋ฏธ์ง€๊ฐ€ <none>๊ณผ ๊ฐ™์ด ์ด๋ฆ„์ด ์—†๋Š” ์ด๋ฏธ์ง€๋ฅผ ์ฐพ์•„ ์ง€์›Œ์ฃผ๊ธฐ ์œ„ํ•œ ํ•จ์ˆ˜์—์š”.

45๋ฒˆ์งธ ์ค„์— checkDockerImage()๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ํ˜„์žฌ ์„œ๋ฒ„์— ์ €์žฅ๋œ ๋„์ปค ์ด๋ฏธ์ง€๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด ์ฃผ์—ˆ์–ด์š”.

createDockerImageAndBackup.sh 71 ~ 78๋ฒˆ์งธ ์ค„


์ด ๊ณณ์—์„œ 72๋ฒˆ์งธ ์ค„์— docker images ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•˜์—ฌ ๋„์ปค ์ด๋ฏธ์ง€ ๋ณด์œ  ํ˜„ํ™ฉ์„ ์ถœ๋ ฅํ•˜๊ฒŒ ํ•œ ๋’ค ์ด ๊ฐ’์„ DOCKER_IMAGES_LIST์— ๋‹ด์•„ ์ถœ๋ ฅ๋˜๋„๋ก ํ•ด ์ฃผ์—ˆ์–ด์š”.


๋‹ค์‹œ unknownNameImageDelete()๋กœ ๋Œ์•„์™€์„œ docker images | grep "^<none>" ๋ช…๋ น์–ด๊ฐ€ ์‹คํ–‰๋˜๊ฒŒ ๋˜๊ณ , ํ•ด๋‹น ๋ช…๋ น ์‹คํ–‰ ๊ฒฐ๊ณผ๊ฐ€ ์ฐธ์ด ์•„๋‹ ๋•Œ๋Š” then ์ ˆ ์ฒ˜๋ฆฌ๋ฅผ ์ฐธ์ผ ๋•Œ๋Š” else ์ ˆ ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๊ฒŒ ํ•ด ์ฃผ์—ˆ์–ด์š”.

์ฆ‰, ๋„์ปค ์ด๋ฏธ์ง€ ๋ชฉ๋ก์„ ์กฐํšŒํ•˜๋Š”๋ฐ, grep์œผ๋กœ ์ฃผ์–ด์ง„ ํŒจํ„ด <none>์œผ๋กœ ์‹œ์ž‘ํ•˜๋Š” ํ–‰์„ ํ•„ํ„ฐ๋งํ•˜๊ฒŒ ํ•ด ์ฃผ์—ˆ์–ด์š”.
์—ฌ๊ธฐ์„œ ^ ์˜๋ฏธ๋Š” ํŒจํ„ด์˜ ์‹œ์ž‘์„ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฒƒ์ด๊ณ , <none>์œผ๋กœ ์‹œ์ž‘ํ•˜๋Š” ํ–‰์„ ์ฐพ์œผ๋ผ๋Š” ์˜๋ฏธ์—์š”.


๋งŒ์•ฝ ์œ„์™€ ๊ฐ™์ด ์ด๋ฆ„ ์—†๋Š” ๋„์ปค ์ด๋ฏธ์ง€๊ฐ€ ์žˆ์œผ๋ฉด ๊ฒฐ๊ณผ๊ฐ€ ์ถœ๋ ฅ๋  ๊ฑฐ์—์š”.

๊ฒฐ๊ณผ๊ฐ€ ์—†์œผ๋ฉด ์ด๋ฆ„ ์—†๋Š” ๋„์ปค ์ด๋ฏธ์ง€๋Š” ์—†๋Š” ๊ฒƒ์œผ๋กœ ๋ฐ”๋กœ checkDockerImage()๋ฅผ ํ˜ธ์ถœํ•˜์ง€๋งŒ,

์ด๋ฆ„ ์—†๋Š” ๋„์ปค ์ด๋ฏธ์ง€๊ฐ€ ์กด์žฌํ•œ๋‹ค๋ฉด ์ด๋ฅผ ์ง€์›Œ์ฃผ๊ธฐ ์œ„ํ•ด docker rmi $(docker images -f "dangling=true" -q) ๋ช…๋ น์–ด๋ฅผ ์ฃผ์–ด "dangling=true" ํ•„ํ„ฐ๋ฅผ ์ ์šฉํ•œ ๋ฏธ์‚ฌ์šฉ (dangling) ๋„์ปค ์ด๋ฏธ์ง€๋ฅผ ์‚ญ์ œํ•˜๋ผ๊ณ  ํ•œ ๊ฒƒ์ด์—์š”.


docker images -f "dangling=true" -q ์ด ๋ช…๋ น์–ด๋Š” "dangling=true" ํ•„ํ„ฐ๋ฅผ ์ ์šฉํ•˜์—ฌ ๋ฏธ ์‚ฌ์šฉ ๋„์ปค ์ด๋ฏธ์ง€ ID ๋ชฉ๋ก์„ ๋ฐ˜ํ™˜ํ•˜๊ฒŒ ๋˜๊ณ , -f ์˜ต์…˜์€ ํ•„ํ„ฐ ์ ์šฉํ•˜๋Š” ๊ฒƒ์„ ๋‚˜ํƒ€๋‚ด๋ฉฐ,
"dangling=true"๋Š” ๋ฏธ ์‚ฌ์šฉ ์ด๋ฏธ์ง€ ํ•„ํ„ฐ๋ง์„ ์œ„ํ•œ ์กฐ๊ฑด์ด์—์š”.

-q ์˜ต์…˜์€ ์ด๋ฏธ์ง€ ID๋งŒ์„ ์ถœ๋ ฅํ•˜๋„๋ก ์ง€์ •ํ•œ ๊ฒƒ์ด๊ตฌ์š”.

๊ทธ๋ ‡๊ฒŒ ID๊ฐ€ ์ถœ๋ ฅ๋˜๋ฉด docker rmi ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ํ•ด๋‹น ์ด๋ฏธ์ง€๊ฐ€ ์ง€์›Œ์ง€๋„๋ก ํ•œ ๊ฒƒ์ด์—์š”.

ํ•ด๋‹น ์ž‘์—…์ด ์„ฑ๊ณตํ•˜๋ฉด checkDockerImage()๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ๋‹ค์‹œ ๋„์ปค ์ด๋ฏธ์ง€ ๋ชฉ๋ก์„ ์ถœ๋ ฅํ•˜๊ฒŒ ํ•ด ์ฃผ์—ˆ์–ด์š”.

๋„์ปค ์ด๋ฏธ์ง€๊ฐ€ ์ •๋ฆฌ๊ฐ€ ๋˜๊ฑด ์•ˆ ๋˜๊ฑด ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ข…๋ฃŒํ•˜์ง€ ์•Š๊ฒŒ ํ•œ ๋’ค
createdApplicationDockerImage()๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค.





        ๐Ÿ“ฆ createdApplicationDockerImage()

createDockerImageAndBackup.sh 80 ~ 110๋ฒˆ์งธ ์ค„


์ด ๊ณณ์—์„œ ์‹ค์ œ Spring Boot Application ๊ด€๋ จ ๋„์ปค ์ด๋ฏธ์ง€ ๋งŒ๋“œ๋Š” ์ž‘์—…์„ ์‹คํ–‰ํ•˜๊ฒŒ ๋˜์š”.

docker build -t $APPLICATION_DOCKER_IMAGE_NAME $APPLICATION_DOCKER_FILE_PATH
docker build -t <์ด๋ฏธ์ง€๋ช…:ํƒœ๊ทธ> <Dockerfile ๊ฒฝ๋กœ>


์œ„ ๋ช…๋ น์–ด๋Š” 

createDockerImageAndBackup.sh 8 ~ 12๋ฒˆ์งธ ์ค„


์œ„์™€ ๊ฐ™์ด ์ „์—ญ ์ƒ์ˆ˜ ๋ณ€์ˆ˜๋กœ ์„ค์ •ํ•ด ์ค€ ๋„์ปค ์ด๋ฏธ์ง€ ์ด๋ฆ„๊ณผ ๋„์ปค ํŒŒ์ผ ๊ฒฝ๋กœ๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋‹ด๊ณ  ์žˆ๋Š”๋ฐ,

APPLICATION_DOCKER_FILE_PATH์— ์œ„์น˜ํ•œ ๋„์ปค ํŒŒ์ผ์„ ์ด์šฉํ•ด์„œ ๋„์ปค ์ด๋ฏธ์ง€๋ฅผ ๋นŒ๋“œํ•˜๊ณ , ํ•ด๋‹น ๋„์ปค ์ด๋ฏธ์ง€ ์ด๋ฆ„์„ APPLICATION_DOCKER_IMAGE_NAME์œผ๋กœ ์ ์šฉํ•˜๋ผ๋Š” ๋ช…๋ น์–ด์—์š”.

๋งŒ์•ฝ ๋ช…๋ น์–ด๊ฐ€ ์‹คํŒจํ•˜๋ฉด ํ•ด๋‹น ์Šคํฌ๋ฆฝํŠธ๋Š” ์ข…๋ฃŒ๋˜๊ณ , ์„ฑ๊ณตํ•˜๊ฒŒ ๋˜๋ฉด docker images ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ํ•ด๋‹น ๋„์ปค ์ด๋ฏธ์ง€๊ฐ€ ์ถœ๋ ฅ๋˜๋Š”์ง€ ํ™•์ธ์„ ๋‹ค์‹œ ํ•œ๋ฒˆ ํ•ด ์ฃผ์—ˆ์–ด์š”.

๋งŒ์•ฝ ์ถœ๋ ฅ๋˜์ง€ ์•Š์•˜๋‹ค๋ฉด ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์Šคํฌ๋ฆฝํŠธ๋Š” ์ข…๋ฃŒ๋˜๊ณ , ์„ฑ๊ณตํ•˜๋ฉด
applicationDockerImageBackUp()์„ ํ˜ธ์ถœํ•˜๊ฒŒ ๋œ๋‹ต๋‹ˆ๋‹ค.


Spring Boot Dockerfile

 

FROM openjdk:11

#์ž‘์„ฑ์ž
MAINTAINER ์ฃผ๋‹ˆ <junyharang8592@gmail.com>

# ์‹ค์ œ Docker ๊ธฐ๋™ ์„œ๋ฒ„์˜ Build ๋œ ๋’ค ๋งŒ๋“ค์–ด์ง„ jar file ์œ„์น˜ ์ง€์ •
ARG JAR_FILE=./build/libs/*.jar

# ์ปจํ…Œ์ด๋„ˆ ์•ˆ์— /app ๋””๋ ‰ํ† ๋ฆฌ ์ƒ์„ฑ
# ์ปจํ…Œ์ด๋„ˆ ์•ˆ์—๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ root๊ถŒํ•œ์œผ๋กœ ๋งŒ๋“ค์–ด์ง
# ๋งŒ์ผ, ์šด์˜๊ณ„์ •์„ ๋ณ„๋„๋กœ ๋งŒ๋“ค์–ด ๊ด€๋ฆฌํ•œ๋‹ค๋ฉด ํด๋” ์†Œ์œ ์ž ํ˜น์€ ๊ถŒํ•œ์— ๋Œ€ํ•œ ์„ค์ • ํ•„์š”.
RUN mkdir -p /app

# ์ปจํ…Œ์ด๋„ˆ ์•ˆ์˜ /app ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ํ™œ์šฉ ๋””๋ ‰ํ„ฐ๋ฆฌ(Work Dircetory)๋กœ ์„ค์ •
WORKDIR /app

# Build๊ฐ€ ๋œ ๋’ค ๋งŒ๋“ค์–ด์ง„ jar File์„ ์ปจํ…Œ์ด๋„ˆ ๋‚ด๋ถ€ /app Directory ๋ฐ‘์— ๋ณต์‚ฌ
COPY ${JAR_FILE} /app/application.jar

# TIME Zone ์„ค์ •
ENV TZ=Asia/Seoul
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

ENTRYPOINT ["java", "-jar", "-Dspring.profiles.active=prod", "/app/application.jar", "-Djava.net.preferIPv4Stack=true", "-Duser.timezone=Asia/Seoul"]
728x90


๋„์ปค ํŒŒ์ผ์€ ์œ„์™€ ๊ฐ™์ด ์ค€๋น„ํ–ˆ์–ด์š”.


RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone


์ด ๋ถ€๋ถ„์€ ์ปจํ…Œ์ด๋„ˆ ๋‚ด๋ถ€์—์„œ ์‹คํ–‰๋  ๋ช…๋ น์–ด๋ฅผ ์ •์˜ํ•œ ๋ถ€๋ถ„์ธ๋ฐ,
$TZ ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ๊ฐ’์— ๋”ฐ๋ผ ์ปจํ…Œ์ด๋„ˆ Time Zone์„ ์„ค์ •ํ•˜๋Š” ๋ช…๋ น์–ด์—์š”.

/usr/sare/zoneinfo/$TZ ๊ฒฝ๋กœ์— ํ•ด๋‹นํ•˜๋Š” Time Zone File์„ /etc/localtime์— ์‹ฌ๋ณผ๋ฆญ ๋งํฌ๋ฅผ ๊ฑธ์–ด ์—ฐ๊ฒฐ ์‹œ์ผœ์ฃผ์—ˆ๊ณ , /etc/timezone ํŒŒ์ผ์— $TZ ํ™˜๊ฒฝ ๋ณ€์ˆ˜์— ๊ฐ’์„ ์ €์žฅํ•˜๊ฒŒ ํ•ด ์ฃผ์—ˆ์–ด์š”.


ENTRYPOINT ["java", "-jar", "-Dspring.profiles.active=prod", "/app/application.jar", "-Djava.net.preferIPv4Stack=true", "-Duser.timezone=Asia/Seoul"]


์ด ๋ถ€๋ถ„ ์—ญ์‹œ ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์‹คํ–‰๋  ๋•Œ, ๋ช…๋ น์–ด๊ฐ€ ๋™์ž‘ํ•˜๊ฒŒ ์ •์˜ํ•˜๋Š” ๋ถ€๋ถ„์ด์—์š”.

์ตœ์ดˆ java ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด Java ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ๊ตฌ๋™๋  ์ˆ˜ ์žˆ๋„๋ก ํ•ด ์ฃผ์—ˆ๊ณ , -jar ์˜ต์…˜ ๋‹ค์Œ์— jar ํŒŒ์ผ ๊ฒฝ๋กœ /app/application.jar๋ฅผ ์ง€์ •ํ•ด ์ฃผ์—ˆ์–ด์š”.

-Dspring.profiles.active=prod ๋ถ€๋ถ„์€
Spring Boot ํ™˜๊ฒฝ ํ”„๋กœํŒŒ์ผ ์„ค์ • ๋ถ€๋ถ„์œผ๋กœ ์šด์˜ ํ™˜๊ฒฝ์— ๋งž๊ฒŒ ๊ตฌ๋™ํ•˜๋ผ๊ณ  ๋ช…๋ น์–ด๋ฅผ ์ค€ ๊ฒƒ์ด์—์š”.

-Djava.net.preferIPv4Stack=true ์ด ๋ถ€๋ถ„์€ IPv4 ์šฐ์„  ๋„คํŠธ์›Œํฌ ์Šคํƒ์„ ์‚ฌ์šฉํ•˜๋„๋ก ์„ค์ •ํ•˜์—ฌ IP ์ •๋ณด๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ, IPv6 ๋ฐฉ์‹์ด ์•„๋‹Œ v4 ๋ฐฉ์‹์œผ๋กœ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด ์ค€ ๋ถ€๋ถ„์ด์—์š”.

๊ทธ๋ฆฌ๊ณ , -Duser.timezone=Asia/Seoul ์„ ํ†ตํ•ด JVM ์‹œ๊ฐ„๋Œ€๋ฅผ Asia/Seoul๋กœ ์„ค์ •ํ•ด ์ฃผ์—ˆ์–ด์š”.


 

 

 

 

        ๐Ÿ“ฆ applicationDockerImageBackUp()

createDockerImageAndBackup.sh 111 ~ 139๋ฒˆ์งธ ์ค„


์ด ๊ณณ์—์„œ ๋„์ปค ์ด๋ฏธ์ง€๋ฅผ ์•„์นด์ด๋ธŒ ํ˜•ํƒœ๋กœ ๋ฐฑ์—…ํ•ด ์ค„๊ฑฐ์—์š”.

๋ฐฑ์—…์— ๋Œ€ํ•œ ์ƒ์„ธ ๋‚ด์šฉ์€ ์ด ๊ณณ์— ์ž์„ธํžˆ ์ค€๋น„ํ•ด ๋‘์—ˆ์–ด์š”.

 

[Docker] Bash Shell Script๋ฅผ ์ด์šฉํ•œ Docker Backup

๋ฆฌ๋ˆ…์Šค ์ปค๋งจ๋“œ๋ผ์ธ ์‰˜ ์Šคํฌ๋ฆฝํŠธ ๋ฐ”์ด๋ธ” COUPANG www.coupang.com "์ด ํฌ์ŠคํŒ…์€ ์ฟ ํŒก ํŒŒํŠธ๋„ˆ์Šค ํ™œ๋™์˜ ์ผํ™˜์œผ๋กœ, ์ด์— ๋”ฐ๋ฅธ ์ผ์ •์•ก์˜ ์ˆ˜์ˆ˜๋ฃŒ๋ฅผ ์ œ๊ณต๋ฐ›์Šต๋‹ˆ๋‹ค." ๐Ÿš€ Docker Container Backup ๐Ÿ”ฝ ๊ฐœ์š” ๐Ÿ“ฆ ์†Œ๊ฐœ ์ด

junyharang.tistory.com

 

๋จผ์ € ๋ฐฑ์—… ์ €์žฅ ๋””๋ ‰ํ† ๋ฆฌ๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธ์„ ์œ„ํ•ด 117๋ฒˆ์งธ ์ค„์— checkBackupDirctory()๋ฅผ ํ˜ธ์ถœํ•˜๋Š”๋ฐ, APPLICATION_DOCKER_BACKUP_DIR ์ „์—ญ ์ƒ์ˆ˜ ๋ณ€์ˆ˜๋ฅผ ์ „๋‹ฌํ•ด ์ฃผ์—ˆ์–ด์š”.

๋””๋ ‰ํ„ฐ๋ฆฌ๊ฐ€ ์žˆ๋‹ค๋ฉด ํ•ด๋‹น ๋””๋ ‰ํ„ฐ๋ฆฌ๋กœ ์œ„์น˜๋ฅผ ์˜ฎ๊ธฐ๊ฒŒ ๋˜๊ณ , 122๋ฒˆ์งธ ์ค„์„ ํ†ตํ•ด yyyy-mm-dd-giggal-total-back-office-api.tar ์ด๋ฆ„์œผ๋กœ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋„์ปค ์ด๋ฏธ์ง€๋ฅผ ์•„์นด์ด๋ธŒ ํ˜•ํƒœ๋กœ ๋ฐฑ์—…ํ•˜๊ฒŒ ๋œ๋‹ต๋‹ˆ๋‹ค.

127๋ฒˆ์งธ ์ค„์— ํ•ด๋‹น ๋ฐฑ์—… ๋””๋ ‰ํ† ๋ฆฌ์— Owner ship ํ™•์ธ์„ ์œ„ํ•ด ls -al ./ ๋ช…๋ น์–ด๋ฅผ ๋˜์ง€๋Š”๋ฐ, ํ•ด๋‹น ๋ช…๋ น์–ด๊ฐ€ ์•ˆ๋˜๋ฉด cicd ์šฉ ๊ณ„์ •์—๊ฒŒ ๊ถŒํ•œ์ด ์—†๋Š” ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ด€๋ จ ๋‚ด์šฉ์„ ์ถœ๋ ฅํ•˜๊ณ , ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ข…๋ฃŒํ•˜๊ฒŒ ํ•ด ์ฃผ์—ˆ์–ด์š”.

์•„๋ฌด ๋ฌธ์ œ๊ฐ€ ์—†๋‹ค๋ฉด ํ•ด๋‹น ํ•จ์ˆ˜๋Š” ์ข…๋ฃŒ ๋ฉ๋‹ˆ๋‹ค.


 

        ๐Ÿ“ฆ checkBackupDirctory()

createDockerImageAndBackup.sh 141 ~ 158๋ฒˆ์งธ ์ค„


ํ•ด๋‹น ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋˜๋ฉด 142๋ฒˆ์งธ ๋ช…๋ น์–ด๋กœ ์ธํ•ด 5์ดˆ๊ฐ„ ์ž‘๋™์ด ๋ฉˆ์ถ”๊ฒŒ ๋˜์š”.

๊ทธ๋Ÿฐ ๋’ค ๋งค๊ฐœ ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌ๋œ ๊ฐ’์„ backupDirectoryPath ๋ณ€์ˆ˜์— ๋ฐ›๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

148๋ฒˆ์งธ ์ค„์— ls -al ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ํ•ด๋‹น ๋””๋ ‰ํ† ๋ฆฌ๊ฐ€ ์‹ค์ œ ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธํ•˜๊ณ , ์—†์œผ๋ฉด ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ข…๋ฃŒ ์‹œํ‚ค๊ณ , ์žˆ์œผ๋ฉด 

createDockerImageAndBackup.sh 161 ~ 165๋ฒˆ์งธ ์ค„



์ด ๋ถ€๋ถ„์ด ์ถœ๋ ฅ ๋ฐ Log ๋กœ ์ €์žฅ๋˜๊ณ , ํ•ด๋‹น ์Šคํฌ๋ฆฝํŠธ๋Š” ์ข…๋ฃŒ๋˜๊ฒŒ ํ•ด ์ฃผ์—ˆ์–ด์š”.




์ด๋ฒˆ ๊ธ€์—๋Š” ์—ฌ๊ธฐ๊นŒ์ง€ ์ •๋ฆฌํ•˜๊ณ , ๋‹ค์Œ ๊ธ€์—์„œ๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋„์ปค ๊ด€๋ จ ์ž‘์—… ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์ •๋ฆฌํ•ด ๋ณผ๊ฒŒ์š”.


 

 

 

๋„์ปค ๊ต๊ณผ์„œ:์„ค์น˜๋ถ€ํ„ฐ ์‹ค์ „ ์šด์˜ ํˆฌ์ž…๊นŒ์ง€ ํ•œ ๊ถŒ์— ๋‹ด์•˜๋‹ค!

COUPANG

www.coupang.com

"์ด ํฌ์ŠคํŒ…์€ ์ฟ ํŒก ํŒŒํŠธ๋„ˆ์Šค ํ™œ๋™์˜ ์ผํ™˜์œผ๋กœ, ์ด์— ๋”ฐ๋ฅธ ์ผ์ •์•ก์˜ ์ˆ˜์ˆ˜๋ฃŒ๋ฅผ ์ œ๊ณต๋ฐ›์Šต๋‹ˆ๋‹ค."

 

 

์ด์ „ ๊ธ€: [CI/CD] Jenkins + Docker๋ฅผ ์ด์šฉํ•œ ๋ฌด์ค‘๋‹จ ๋ฐฐํฌ - โ‘  Application Linux(Ubuntu)์— SSH๋ฅผ ์ด์šฉํ•œ ํŒŒ์ผ ์ „์†ก

๋‹ค์Œ ๊ธ€: [CI/CD] Jenkins + Docker๋ฅผ ์ด์šฉํ•œ ๋ฌด์ค‘๋‹จ ๋ฐฐํฌ - โ‘ข Application Server Docker Job (โ‘  Application ๋„์ปค ์กด์žฌ ์—ฌ๋ถ€ ํ™•์ธ)

 

 

 

 

728x90
๋ฐ˜์‘ํ˜•