Host Jenkins + Host Docker

Marullo·2020년 12월 6일
0

docker 설치
jenkins 설치

jenkins가 docker 명령어를 사용할 수 있도록, docker와 그룹핑해준다. 그전에 그룹핑을 하려면 root 계정으로 이동해야 한다.

sudo -su
usermod groups docker jenkins
sudo reboot

jenkins가 docker 명령을 테스트 할 수 있는지 확인하자.
jenkins의 쉘 스크립트로
docker run hello-world를 작성하자.

build now를 누르고, console.output을 확인해보면 docker run hello-world의 결과물이 보인다.
또한 원격 터미널에서 docker images 명령어를 쳐보면, hello-world 이미지가 다운받아진 걸 볼 수 있다.

이제 우리는 Jenkins가 github webhook을 사용해서 해당 브랜치에 push가 발생하면, node image를 빌드하고, 컨테이너를 실행시키는 것 까지 진행할 것임

project 루트 폴더에 Dockerfile을 작성해야 하는데, github에 올릴것이므로 환경변수는 적으면 안된다.
이때, jenkins의 환경변수로 설정하고, ${NODE_ENV}와 같이 환경변수를 끌어다가 컨테이너를 빌드시킬 생각이다.

https://growd.tistory.com/81
https://www.popit.kr/%EA%B0%9C%EB%B0%9C%EC%9E%90%EA%B0%80-%EC%B2%98%EC%9D%8C-docker-%EC%A0%91%ED%95%A0%EB%95%8C-%EC%98%A4%EB%8A%94-%EB%A9%98%EB%B6%95-%EB%AA%87%EA%B0%80%EC%A7%80/
https://velog.io/@sdg9670/Jenkins-%EA%B5%AC%EC%84%B1-%EB%B0%8F-%EC%9E%90%EB%8F%99%EB%B0%B0%ED%8F%AC

"By default Docker containers can make connections to the outside world, but the outside world cannot connect to containers."

You will need to provide special options when invoking docker run in order for containers to accept incoming connections.

Use the -P or --publish-all=true|false for containers to accept incoming connections.

The below should allow you to access it from another network:

docker run -P -p 8080:8080 jenkins

docker logs 컨테이너 id

docker run 이미지이름 -e 키값=밸류값 ~~~ -P -d -p 4000:4000 ~~
나중에 환경변수 관리하기 어려우니까.
밸류값을 젠킨스의 환경변수로 관리하자.

get static props와 dockerfile

dockerfile 내부에서 next build가 있다.
따라서 이미지를 생성하면서 next를 빌드한다.
get static props는 next build 시점에 data를 fetch해오기 때문에,
jenkins가 docker run 시점에 넣어주는 환경변수를 인식하지 못한다.
fetch url이 환경변수로 되어있었고, 따라서 아무런 데이터도 fetch해오지 못함
그에 대한 해결책으로 dockerfile에서 env 태그로 환경변수를 삽입해주면서
docker build - next build 시점에도 환경변수를 인식할 수 있도록 했다.

github plugin에서 github intergration 설치
github webhook에서 jenkins-ip:port/github/webhook을 등록

jenkins node에 프로젝트를 깃헙 레포지토리로 설정
jenkins node에는 빌드 유발을 github으로 설정
빌드 시작하면서 사용할 쉘 스크립트 작성(여기서 docker cli사용)

웹훅을 보냈는데, delivery 실패
403 crumble 에러가 발생했다면, 아래를 참고
https://stackoverflow.com/questions/57731818/jenkins-2-192-http-error-403-no-valid-crumb-was-included-in-the-request

profile
한국외대 중국어&컴공 복수전공 - 세미 전공자의 기술 블로그

0개의 댓글