CI/CD

Jenkins Back-End Dockerizing

uuuhhh 2022. 12. 13. 10:29

👀 Epilogue


  • 역시나 험난한 트러블 슈팅기

 

  • 도커가 없다고 하는 거 같아요..

 

⚡ Leggo


  • 플러그인 설치
    • Docker
    • Docker Pipeline
  • 실패..

 

  • uname -a 를 통해 OS 정보 확인
+ uname -a

Linux default-hp930 5.4.0-99-generic #112-Ubuntu SMP Thu Feb 3 13:50:55 UTC 2022 x86_64 GNU/Linux

 

  • 도커 설치 스크립트 추가
sudo apt-get update
sudo apt-get install docker.io
sudo ln -sf /usr/bin/docker.io /usr/local/bin/docker

 

  • agent pod에서 sudo: not found 문제로 실패

 

  • 해결책
apt-get update && apt-get -y install sudo
  • 이지만 매번 agent pod가 띄워질 때마다 패키지를 업데이트하고 설치하는 것이 빌드 파이프라인에 시간이 많이 소비될 것이 우려됨

 

  • 다른 해결책을 찾다가 저번 Gradle Build에서 Custom Agent를 실패했던 것이 떠올랐다.

 

  • 현재 Agent pod에 도커가 없으니
  • ‘Docker Container를 Custom해서 Agent에 넣어주면 Image Build가 되지 않을까’
  • 라고 생각하게 되었다.

 

  • Agent Docker Container
spec:
  containers:
  - name: docker
    image: docker:latest
    command:
    - cat
    tty: true
    volumeMounts:
    - mountPath: /var/run/docker.sock
      name: docker-sock
  volumes:
  - name: docker-sock
    hostPath:
      path: /var/run/docker.sock

 

  • Dockerfile
FROM openjdk:11

ARG JAR_FILE_PATH=./*.jar
COPY ${JAR_FILE_PATH} ./app.jar

EXPOSE 8080
ENTRYPOINT ["java","-jar","app.jar"]

 

  • Jenkinsfile
pipeline{
    agent {
        kubernetes{
            yamlFile 'KubernetesPod.yaml'
        }
    }

...

stage('Docker Image Build'){
            steps{
                container('docker'){
                    sh "cp demo/build/libs/demo-0.0.1-SNAPSHOT.jar ./"
                    sh "cp demo/Dockerfile ./"
                    sh "docker build . -t ${dockerHubRegistry}:${currentBuild.number}"
                    sh "docker build . -t ${dockerHubRegistry}:latest"
                }
            }

...

 

  • 빌드 시작 !
    • 성공 !!!!!!!!! 🌈
+ cp demo/build/libs/demo-0.0.1-SNAPSHOT.jar ./
[Pipeline] sh
+ cp demo/Dockerfile ./
[Pipeline] sh
+ docker build . -t uh2959/cucumber-back:51
Sending build context to Docker daemon  35.92MB

Step 1/5 : FROM openjdk:11
11: Pulling from library/openjdk
001c52e26ad5: Pulling fs layer
...
66223a710990: Pulling fs layer
db38d58ec8ab: Pulling fs layer
d85151f15b66: Waiting
...
9daef329d350: Waiting
d9d4b9b6e964: Verifying Checksum
d9d4b9b6e964: Download complete
2068746827ec: Verifying Checksum
...
db38d58ec8ab: Verifying Checksum
db38d58ec8ab: Download complete
001c52e26ad5: Pull complete
...
db38d58ec8ab: Pull complete
Digest: sha256:99bac5bf83633e3c7399aed725c8415e7b569b54e03e4599e580fc9cdb7c21ab
Status: Downloaded newer image for openjdk:11
 ---> 47a932d998b7
Step 2/5 : ARG JAR_FILE_PATH=./*.jar
 ---> Running in 208235ed04bf
...
Successfully built d9934a7196a4
Successfully tagged uh2959/cucumber-back:latest
[Pipeline] }
[Pipeline] // container
Post stage
[Pipeline] echo
Docker image build success!
[Pipeline] }
...
[Pipeline] End of Pipeline
Finished: SUCCESS

 

 

ref.


How to Use Kubernetes Pods As Jenkins Agents

Docker in Docker with Jenkins Pod on Kubernetes