[Docker] 도커에서 Git clone 하기

류재성's avatar
Jul 19, 2024
[Docker] 도커에서 Git clone 하기
 
💡
지난 블로그에선 .jar 파일을 폴더에 넣은 상태에서 실행했다. .jar 파일로 실행하는 방법은 과정이 단순하지만 코드의 변경 사항을 반영할 수 없기 때문에 다시 .jar 파일로 만든 후 실행해야 한다. 반면 git clone 방식을 사용하면 실행 시 마다 최산 파일을 빌드할 수 있다.
 

1. git 설치

 

1.1 리눅스에서 git 설치

 
notion image
docker run -dit openjdk:11-jdk-slim bash
 
notion image
 
notion image
git --version // 깃 버전 확인 , 없다면 확인안됨
 
컨테이너를 실행 후 확인 했을 때 git이 설치되어 있지 않다.
 
apt-get update
notion image
 
메뉴판을 업데이트한다.
 
apt-get install -y git >/dev/null //
notion image
 
git이 설치 완료되었다.
 

1.2 entrypoint 사용하기

 
notion image
파일을 이렇게 생성한다.
 
entrypoint.sh
apt-get update # 메뉴판 업데이트 apt-get install -y git 1>/dev/null # git 설치 git clone https://github.com/busanuv/blog-last # git clone 코드 sleep 1s # 혹시 문제 생기면 비동기적으로 처리하려고함 cd blog-last # blog-last 디렉토리로 이동 chmod +x gradlew # gradlew 파일에 실행 권한을 부여 ./gradlew build # 프로젝트를 빌드, .jar 파일 생성 java -jar -Dspring.profile.active=dev build/libs/*.jar # 빌드된 JAR 파일을 실행
notion image
 
ex03/Dockerfile
FROM openjdk:11-jdk-slim # 터미널이 실행되는 위치 설정, 이 폴더 내부에서 실행 WORKDIR /var/currunt/app # ./가 되는건 workdir 설정을 해서 COPY ./entrypoint.sh ./entrypoint.sh # 상수. 변경이 안됨. ENTRYPOINT ["/bin/bash","entrypoint.sh"]
notion image
 
💡
entrypoint 에 코드를 적어두면 빌드 시에 실행된다.
 

2. 빌드하기

 
notion image
 
새로운 이미지 파일을 생성한다.
 
notion image
 
컨테이너를 실행한다.
 
notion image
 
notion image
 
💡
엔트리포인트에 넣어놓은 코드가 실행되면서 git 설치, git clone, .jar 파일까지 생성, 실행 완료되었다.
 
Share article
RSSPowered by inblog