개발 일지

Jenkins Docker Builder 연동 이슈 (connection refused)

북극곰은콜라 2023. 12. 20. 10:31
반응형


개요

Jenkins는 CI / CD를 구성할 수 있는 강력한 툴이다.
Docker는 환경을 포함해서 Application을 이미지화 할 수 있는 툴이며
Jenkins를 통해 docker로 배포하는 방법은 많이 활용되는 조합이다.

 


문제 발생

기존 배포 방식

일반적인 배포방식으로는
1. Jenkins에서 build 및 docker image 생성
2. docker image를 DockerHub로 업로드
3. 배포 받을 서버에 dockerHub에서 특정 이미지를 받아서 실행하도록 command
이 있다.

배포 방식 단순화

2023 개인서버에는 위 로직을 통해 배포를 진행했었다.
넥스트 계획의 핵심은, 중복되는 작업들을 최대한 자동화 시키고 요구사항에서 벗어나는 확장성을 줄이고자한다.
이에, 서버 한곳에서 빌드 및 배포가 진행될 것이기에 로직을 단순화 시켰다.
1. Jenkins에서 build 및 docker image 생성
2. 서버에 Docker Command를 통해 컨테이너 생성 및 run

Jenkins Docker Plugin 적용

단순화된 배포방식으로, 기존 복잡한 Jenkins File대신
Jenkins Plugin을 최대한 활용하기로 결정

Connection Refuse 발생

Something went wrong, cannot connect to unix:///var/run/docker.sock, cause: org.apache.hc.client5.http.HttpHostConnectException: Connect to unix://localhost:2375 [localhost/127.0.0.1] failed: Connection refused
포럼에서 Jenkins는 Docker Builder 설정을 수동으로 해야한다고 가이드하여
해당 설정을 하면, docker에서 connection refuse가 떨어진다.
netstat -tnlp | grep 2375

/etc/docker# docker --version
Docker version 23.0.4, build f480fb1
확인해보니 2375로 LISTEN중인 어플리케이션이 없었다.
docker는 apt-get으로 설치한후 mount point 변경 등 작업을 한 상황이다.
/etc/docker/damon.json

{
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "10m",
    "max-file": "3"
  },
  "hosts": ["unix:///var/run/docker.sock", "tcp://0.0.0.0:2375"]
}
docker에서 REST API가 열리지 않은 것으로 판단되여
docker reference를 통해 설정을 진행했다.
"hosts": ["unix:///var/run/docker.sock", "tcp://0.0.0.0:2375"]를 json 설정파일에 추가했다.

Service Run Fail 발생

systemctl restart docker
Job for docker.service failed because the control process exited with error code.
See "systemctl status docker.service" and "journalctl -xeu docker.service" for details.
docker service가 실패했으며
journal을 확인해 보니
journalctl -xeu docker.service
...

.... dockerd[2153802]: unable to configure the Docker daemon with file /etc/docker/daemon.json: the following directives are specified both as a flag and in the configuration file: hosts: (from fla>
.... systemd[1]: docker.service: Main process exited, code=exited, status=1/FAILURE

...
specified both as a flag and in the configuration file: hosts
해당 문구로 볼때, docker.service 파일에서 hosts관련 옵션이 있는것으로 추정
/lib/systemd/system/docker.service

...
[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutStartSec=0
RestartSec=2
Restart=always
...
역시 dockerd execute할 때 -H 옵션이 있었다.
systemctl daemon-reload
systemctl start docker

// docker service status 확인
root@m-H110M4-C2D:/etc/docker# systemctl status docker
● docker.service - Docker Application Container Engine
     Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2023-12-20 10:27:46 KST; 1min 3s ago
TriggeredBy: ● docker.socket
       Docs: https://docs.docker.com
   Main PID: 2153904 (dockerd)
      Tasks: 139
     Memory: 58.6M
        CPU: 4.071s
     CGroup: /system.slice/docker.service
             ├─2153904 /usr/bin/dockerd --containerd=/run/containerd/containerd.sock
             ├─2154352 /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 9021 -container-ip 172.29.0.3 -container-port 9021
             ├─2154360 /usr/bin/docker-proxy -proto tcp -host-ip :: -host-port 9021 -container-ip 172.29.0.3 -container-port 9021
             ...

// LISTEN port 확인
netstat -tnlp | grep 2375
tcp6       0      0 :::2375                 :::*                    LISTEN      2153904/dockerd

Jenkins 연동도 정상적으로 작동 확인

 


REFERENCE

https://docs.docker.com/engine/reference/commandline/dockerd/#daemon-configuration-file

반응형