-
[CD] 5분만에 오픈소스를 활용한 Docker container 이미지 자동으로 배포하기(feat. watchtower)코딩/CI.CD 2023. 5. 25. 00:51
간단한 GitActions를 이용한 간단한 CI를 구축하고 싶으시면 아래 포스트를 참고해 주세요.
2023.03.10 - [코딩/CI] - [CI] Github Action으로 Docker hub에 이미지 업로드 #1
[CI] Github Action으로 Docker hub에 이미지 업로드 #1
목표 master branch에 code가 push 되었을 때 Github Action을 통해서 Docker hub에 이미지를 빌드하여 푸쉬 github workflow 작성하기 Github Action은 트리거가 발생하면 repository의 root 디렉토리 밑에 .github/workflow의
yourknow.tistory.com
2023.03.14 - [코딩/CI] - [CI] Github Action으로 Docker hub에 이미지 업로드 #2
[CI] Github Action으로 Docker hub에 이미지 업로드 #2
1편 - gitaction workflow 예제를 작성해보는 포스트 [CI] Github Action으로 Docker hub에 이미지 업로드 #1 목표 master branch에 code가 push 되었을 때 Github Action을 통해서 Docker hub에 이미지를 빌드하여 푸쉬 github
yourknow.tistory.com
docker image가 server에서 동작하는 전제하에 진행을 권장하는 포스트 입니다.
이제 CI를 구축했다면 그 다음은 어떻게 CD를 자동화할까? 라는 고민을 많이 하실 겁니다.
많은 분이 AWS, GCP, Azure와 같은 CSP에서 제공되는 서비스와 Jenkins와 같은 틀을 사용하여 배포 자동화 파이프라인을 구축하곤 합니다.
그렇다면 이번 포스팅의 내용은 맞지 않고 저는 오픈소스인 watchtower를 이용해볼 생각입니다.https://github.com/containrrr/watchtower
GitHub - containrrr/watchtower: A process for automating Docker container base image updates.
A process for automating Docker container base image updates. - GitHub - containrrr/watchtower: A process for automating Docker container base image updates.
github.com
watchtower를 쓰는 이유
간단합니다. 너무 쉽고 빠르기 때문입니다.
물론 프로덕션 환경에서 쓸 정도는 아니라고 생각합니다. 하지만 사이드 프로젝트 진행할때 , 무중단배포 말고 자동으로 배포만 하면 되는 것을 원할때 등등.. 자동 배포의 목적을 빠르게 달성 할 수 있다고 생각합니다.
어떻게 동작하는 데?
얼마나 간단하길래 5분만에 가능하다는 걸까라는 궁금증이 생기실 겁니다.(아닐수도..)
https://containrrr.dev/watchtower/introduction/
Introduction - Watchtower
Introduction Watchtower is an application that will monitor your running Docker containers and watch for changes to the images that those containers were originally started from. If watchtower detects that an image has changed, it will automatically restar
containrrr.dev
watchtower의 문서를 잠시 살펴보겠습니다.
Watchtower is an application that will monitor your running Docker containers and watch for changes to the images that those containers were originally started from. If watchtower detects that an image has changed, it will automatically restart the container using the new image.
이미지에 잘 표현되어있는것 같습니다.
예를 들어 보겠습니다. 내 로컬 서버에서 Docker container가 실행되고 있습니다.(여러개여도 상관없습니다)
이떄 watchtower는 해당 container가 실행하고 있는 image repository를 주기적으로 체크합니다.
개발자가 해당 container의 이미지를 업데이트하면 watchtower가 그걸 감지해서 로컬 서버의 container를 중지시키고 최신 이미지를 다운 받아서 새로운 container를 작동시킵니다.
기본 설정은 docker로 실행중인 모든 container의 이미지 변경을 감지 해서 재배포 합니다.실행 방법
아래 명령어를 실행시키면 됩니다.
$ docker run -d \ --name watchtower \ -v /var/run/docker.sock:/var/run/docker.sock \ containrrr/watchtower
default는 86400시간마다 이미지 변경을 감지합니다.
따라서 -i 옵션을 통해서 60초에 한번씩 이미지 변경을 체크하도록 변경합니다.$ docker run -d \ --name watchtower \ -v /var/run/docker.sock:/var/run/docker.sock containrrr/watchtower \ -i 60
- 이미지 체크 주기
- 제외하고 싶은 이미지
- 프라이밋 repository 설정
등 추가 적인 기능은 아래 watcher 문서를 참고해주시기 바랍니다.
- https://containrrr.dev/watchtower/usage-overview/
- https://containrrr.dev/watchtower/arguments/
Arguments - Watchtower
Arguments By default, watchtower will monitor all containers running within the Docker daemon to which it is pointed (in most cases this will be the local Docker daemon, but you can override it with the --host option described in the next section). However
containrrr.dev
Usage overview - Watchtower
Usage overview Watchtower is itself packaged as a Docker container so installation is as simple as pulling the containrrr/watchtower image. If you are using ARM based architecture, pull the appropriate containrrr/watchtower:armhf- image from the containrrr
containrrr.dev
실행 결과
아래 명령어를 실행 후 container의 log를 보면 1분 간격으로 이미지 변경을 체크하는 것을 볼 수 있습니다.
$ docker logs -f {container id}
'코딩 > CI.CD' 카테고리의 다른 글
[CI] Github Action으로 Docker hub에 이미지 업로드 #2 (0) 2023.03.14 [CI] Github Action으로 Docker hub에 이미지 업로드 #1 (0) 2023.03.10