로컬에서 커밋 시 도커 빌드 후 컨테이너 시작하기

로컬에서 커밋 시 훅을 이용해여 도커 빌드 후 컨테이너 시작하기

기능 목표

  • step1: 커밋 시 로컬 프로젝트를 빌드
  • step2: 도커가 빌드된 파일 기준으로 이미지 빌드
  • step3: 도커가 빌드된 파일 실행

sh 파일을 이용해서 자동화 스크립트 작성

path: /path/to/project/build-and-run.sh

#!/bin/bash

# 변수 설정
IMAGE_NAME=frontend:latest
PORT=8080

# 빌드 및 도커 실행
npm run build
docker build -t $IMAGE_NAME .
docker stop frontend-test || true
docker rm frontend-test || true
docker run -d --name frontend-test -p $PORT:80 $IMAGE_NAME

echo "로컬 테스트: http://localhost:$PORT"

위와 같이 sh 스크립트를 작성 후 실행권한 추가

chmod   +x   build-and-run.sh

실행 권한이 필요한 이유?

  • 기본적으로 새로 생성된 스크립트 파일은 실행 불가 상태
  • 권한 부여 없이 실행하려고 하면 다음과 같은 에러 발생:
-bash: ./build-and-run.sh: Permission denied

실행 권한 확인 방법

ls -l build-and-run.sh

스크립트 실행을 위해서 깃훅으로 연동하기

path: /path/to/project/.git/hooks

hooks 폴더안에는 여러가지 상황에 맞는 파일들이 설정되어 있습니다

지금은 커밋 후 자동으로 실행 되길 원하기 떄문에 post-commit 진입 후 스크립트 추가

nano /path/to/project/.git/hooks/post-commit

post-commit

/path/to/project/build-and-run.sh

후에 커밋을 날려봤지만 실행되질 않음

hint: The '.git/hooks/post-commit' hook was ignored because it's not set as executable.
hint: You can disable this warning with `git config advice.ignoredHook false`.

권한을 깜빡했구만

chmod +x post-commit

추가 후 다시 시도하면

한번에 실행되는 것을 확인할 수 있어요


최근 GCP의 VM에 배포하던 도중 배포 노가다 하는 모습의 나를 보면서 자동화를 시도했지만 남의 코드를 복붙만 하는 거 같아서 단계별로 하나 하나 퀘스트를 깨며 학습할 예정입니다...

> my-app@0.0.0 build
> vite build

^R
givite v6.2.1 building for production...
transforming (1) src/main.tsx^R
✓ 2288 modules transformed.
dist/index.html                   0.46 kB │ gzip:   0.30 kB
dist/assets/index-vuH6rREl.css   49.00 kB │ gzip:   5.82 kB
dist/assets/index-DWljNZS7.js   734.03 kB │ gzip: 216.05 kB

(!) Some chunks are larger than 500 kB after minification. Consider:
- Using dynamic import() to code-split the application
- Use build.rollupOptions.output.manualChunks to improve chunking: https://rollupjs.org/configuration-options/#output-manualchunks
- Adjust chunk size limit for this warning via build.chunkSizeWarningLimit.
✓ built in 3.12s
[+] Building 2.0s (9/9) FINISHED                                                                                            docker:desktop-linux
 => [internal] load build definition from Dockerfile                                                                                        0.0s
 => => transferring dockerfile: 356B                                                                                                        0.0s
 => [internal] load metadata for docker.io/library/nginx:1.25.4-alpine                                                                      1.8s
 => [auth] library/nginx:pull token for registry-1.docker.io                                                                                0.0s
 => [internal] load .dockerignore                                                                                                           0.0s
 => => transferring context: 2B                                                                                                             0.0s
 => [1/3] FROM docker.io/library/nginx:1.25.4-alpine@sha256:31bad00311cb5eeb8a6648beadcf67277a175da89989f14727420a80e2e76742                0.0s
 => => resolve docker.io/library/nginx:1.25.4-alpine@sha256:31bad00311cb5eeb8a6648beadcf67277a175da89989f14727420a80e2e76742                0.0s
 => [internal] load build context                                                                                                           0.0s
 => => transferring context: 785.80kB                                                                                                       0.0s
 => CACHED [2/3] COPY nginx/nginx.conf /etc/nginx/conf.d/default.conf                                                                       0.0s
 => CACHED [3/3] COPY dist /usr/share/nginx/html                                                                                            0.0s
 => exporting to image                                                                                                                      0.0s
 => => exporting layers                                                                                                                     0.0s
 => => exporting manifest sha256:53bc6114ca6e7d29024cf18159b4aa8c43551d7bcabc5e6982fb61a66a87fb42                                           0.0s
 => => exporting config sha256:7c18f3558404726990b30f8e86748dacbf38e0f76ea9caa61c88b21edce82bdd                                             0.0s
 => => exporting attestation manifest sha256:1be1fa6402632b870206f0da6be77e55034c509557fc585abfd7de85444d15cf                               0.0s
 => => exporting manifest list sha256:2caf0eeae8264233a72c67166ba8adeeb1932b2794ec422dee23925c95eaf60a                                      0.0s
 => => naming to docker.io/library/frontend:latest                                                                                          0.0s
 => => unpacking to docker.io/library/frontend:latest                                                                                       0.0s

View build details: docker-desktop://dashboard/build/desktop-linux/desktop-linux/vk3dy9rb8tkmpjaffkhks62it
frontend-test
frontend-test
8d423639a6f67b7c6400742eb62181836ecb80d90133e1dc716b4e88c26c62aa
로컬 테스트: http://localhost:8080
[main 61a4a77] test