Springboot Gradle + AWS EC2 + Jenkins + Docker๋กœ ๋ฐฐํฌ๊นŒ์ง€(1) : EC2์— Docker ์ด๋ฏธ์ง€๋กœ Jenkins ์„ค์น˜ํ•˜๊ธฐ

2021. 4. 30. 15:44ใ†๐ŸŒฑ Develop/devOps

์•„์ง ec2์— docker, docker-compose ๊ฐ€ ์„ค์น˜๋˜์–ด์žˆ์ง€ ์•Š๋‹ค๋ฉด ๋‹ค์Œ ํฌ์ŠคํŒ…์„ ๋จผ์ € ์ฐธ๊ณ ํ•ด์ฃผ์„ธ์š”!

2021.04.30 - [๐ŸŒฑ Develop/devOps] - ๐Ÿณ EC2์— docker , docker-compose ์„ค์น˜ํ•˜๊ธฐ

 

๐Ÿณ 1) EC2์— docker ์ด๋ฏธ์ง€๋กœ Jenkins ์„ค์น˜ํ•˜๊ธฐ

  • ํ™˜๊ฒฝ: ec2 ํ”„๋ฆฌํ‹ฐ์–ด / linux

 

 ec2 ์ž์ฒด์— jenkins๋ฅผ ๋ฐ”๋กœ ์„ค์น˜ํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ, docker image๋กœ jenkins๋ฅผ ๋ฐ›์•„์™€์„œ ์‚ฌ์šฉํ•ด๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ผ๋‹จ mkdir jenkins-dockerfile ๋กœ ํด๋”๋ฅผ ๋งŒ๋“ค์–ด์„œ, ์ด ํด๋” ๋ฐ‘์— Dockerfile์„ ํ•˜๋‚˜ ์ƒ์„ฑํ•ด์ฃผ์„ธ์š”. jenkins ์ตœ์‹  ์ด๋ฏธ์ง€๋ฅผ ๋ฐ›์•„์˜ค๋Š” Dockerfile์ž…๋‹ˆ๋‹ค.

 

๐Ÿ“‚ home/ec2-user/compose/jenkins-dockerfile/Dockerfile

FROM jenkins/jenkins:lts 
USER root RUN apt-get update &&\   
              apt-get upgrade -y &&\
              apt-get install -y openssh-client

 

 

๊ทธ ๋‹ค์Œ์—” docker-compose.yaml ํŒŒ์ผ๋กœ jenkins ์ด๋ฏธ์ง€๋ฅผ ๋นŒ๋“œํ•ด์ค๋‹ˆ๋‹ค.

๐Ÿ“‚ home/ec2-user/compose/docker-compose.yml

1 version: "3"

  2 services:
  3   jenkins:
  4     container_name: jenkins-compose  // ์ปจํ…Œ์ด๋„ˆ ๋ช… ์ง€์ •
  5     image: jenkins/jenkins:lts // docker hub์—์„œ ๋ฐ›์•„์˜จ ์ด๋ฏธ์ง€
  6 #    build:
  7 #     context: jenkins-dockerfile // ์•„๊นŒ ์ƒ์„ฑํ•œ Dockerfile ๊ธฐ๋ฐ˜์œผ๋กœ ๋นŒ๋“œ
  8 #     dockerfile: Dockerfile
  9     user: root       
 10     ports:
 11       - "9090:8080"  // 8080์€ springboot์™€ ๊ฒน์น˜๋ฏ€๋กœ 9090์œผ๋กœ ์„ค์ •
 12     volumes:
 13       - /home/ec2-user/compose/jenkins:/var/jenkins_home
 14       - /home/ec2-user/compose/.ssh:/root/.ssh

 

ํ•ด๋‹น ํŒŒ์ผ์„ ์ €์žฅํ•˜๊ณ  docker-compose.yml ํŒŒ์ผ์ด ์žˆ๋Š” ์œ„์น˜์—์„œ docker-compose up ์„ ํ•˜๋ฉด jenkins๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์ด์ œ ec2์—์„œ docker logs ์ปจํ…Œ์ด๋„ˆ ๋ช… ์œผ๋กœ Jenkins์˜ ์ดˆ๊ธฐ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์–ป๊ณ ,

ec2 ip ์ฃผ์†Œ:8080์„ ์ž…๋ ฅํ•˜๋ฉด Jenkins ํ™”๋ฉด์ด ๋‚˜์˜ต๋‹ˆ๋‹ค. ์ด ๋•Œ ์ดˆ๊ธฐ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•˜๊ณ  ํ”Œ๋Ÿฌ๊ทธ์ธ ์„ค์น˜ ํ™”๋ฉด์„ ๋„˜๊ธฐ๋ฉด, user ์ •๋ณด ์ž…๋ ฅ์œผ๋กœ ๋„˜์–ด๊ฐ‘๋‹ˆ๋‹ค. user ์ •๋ณด๊นŒ์ง€ ์ž…๋ ฅํ•˜๊ณ  ๋‚˜๋ฉด Jenkins ์ž…์„ฑ ์™„๋ฃŒ!

 

 

 

โ—๏ธjenkins > ๊ด€๋ฆฌ์ž ํŽ˜์ด์ง€์—์„œ ์—ญ๋ฐฉํ–ฅ ํ”„๋ก์‹œ๊ฐ€ ์ž˜๋ชป๋˜์—ˆ๋‹ค๋Š” ๋ฌธ๊ตฌ๊ฐ€ ๋‚˜ํƒ€๋‚œ ๊ฒฝ์šฐ

jenkins URL์ด ์ž˜๋ชป ์„ค์ •๋œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์‹ค์ œ ๋ธŒ๋ผ์šฐ์ €์—์„œ ์ ‘์†ํ•˜๋Š” url๋กœ ์„ค์ •ํ•ด์•ผํ•˜๋ฏ€๋กœ, ec2๋กœ ์ ‘์†ํ–ˆ๋‹ค๋ฉด  http://ec2 ip ์ฃผ์†Œ:8080 ๊ฐ€ ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.