본문 바로가기

리눅스

[리눅스] 젠킨스 파이프라인을 이용한 도커 이미지 빌드(CI/CD)

728x90

젠킨스 파이프라인을 이용한 도커 이미지 빌드(CI/CD)

Jenkinsfile

pipeline {
    environment {
        registry = "anti1346/apm"
        registryCredential = 'dockerimagepush'
        dockerImage = ''
    }

    agent any

    stages {

        stage('Build image') {
            steps {
                sh 'docker build -t $registry:$BUILD_NUMBER .'
                sh 'docker image tag $registry:$BUILD_NUMBER $registry:latest'
                echo 'Build image...'
            }
        }

        stage('Test image') {
            steps {
                sh 'docker run -d -p 80:80 --name apm $registry:$BUILD_NUMBER'
                echo 'Test image...'
            }
        }

        stage('Push image') {
            steps {
                withDockerRegistry([ credentialsId: registryCredential, url: "" ]) {
                    sh 'docker push $registry:$BUILD_NUMBER'
                    sh 'docker push $registry:latest'
                }
                echo 'Push image...'
            }
        }

        stage('Clean image') {
            steps {
                sh 'docker rm -f `docker ps -aq --filter="name=apm"`'
                sh 'docker rmi $registry:$BUILD_NUMBER'
                sh 'docker rmi $registry:latest'
                echo 'Clean image...'
            }
        }

    }
}

Dockerfile

FROM centos:7

##### 시스템 운영에 필요한 패키지 설치
RUN yum install -y epel-release yum-utils
#RUN yum install -y tar unzip vi vim telnet net-tools curl openssl

##### Apache 설치
RUN yum install -y httpd httpd-tools

##### PHP 설치
RUN yum install -y http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
RUN yum-config-manager --enable remi-php74
RUN yum install -y php php-cli php-common php-devel php-json php-mbstring php-pdo php-gd php-xml php-mcrypt \
  && yum clean all \
  && rm -rf /var/cache/yum \
  && rm -rf /tmp/*

##### Apache 유저, 그룹, 기본설정(ServerName)
RUN sed -i "s/`grep '^User ' /etc/httpd/conf/httpd.conf`/User nobody/g" /etc/httpd/conf/httpd.conf \
  && sed -i "s/`grep '^Group ' /etc/httpd/conf/httpd.conf`/Group nobody/g" /etc/httpd/conf/httpd.conf \
  && sed -i 's/#ServerName www.example.com:80/ServerName localhost:80/g' /etc/httpd/conf/httpd.conf

##### Apache PHP 연동 설정
RUN sed -i 's/DirectoryIndex index.html/DirectoryIndex index.html index.php/g' /etc/httpd/conf/httpd.conf \
  && sed -i 's,\(IfModule mime_module.*\),\1\n\tAddType application/x-httpd-php .htm .html .php .inc,g;' /etc/httpd/conf/httpd.conf \
  && sed -i 's,\(IfModule mime_module.*\),\1\n\tAddType application/x-httpd-php-source .phps,g;' /etc/httpd/conf/httpd.conf

##### 운영체제 업데이트 및 yum cache 삭제
# RUN yum update -y  \
#   && yum clean all \
#   && rm -rf /var/cache/yum \
#   && rm -rf /tmp/*

WORKDIR /var/www/html

ADD phpinfo.php /var/www/html/

EXPOSE 80

ENTRYPOINT ["/usr/sbin/httpd", "-D", "FOREGROUND"]

phpinfo.php

<?php
phpinfo();
?>

Jenkins Pipeline


빌드한 도커 이미지 실행

docker-compose 파일 생성

docker-compose.yml

version: '3.8'
services:

  web01:
    image: anti1346/apm:latest
    restart: unless-stopped
    hostname: web01
    container_name: web01
    ports:
    - 80:80
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost/phpinfo.php"]
      interval: 5s
      timeout: 3s
      retries: 60

web01 컨테이너 실행

docker-compose up -d

$ docker-compose up -d

docker-compose ps

$ docker-compose ps
Name               Command                 State             Ports
-------------------------------------------------------------------------
web01   /usr/sbin/httpd -D FOREGROUND   Up (healthy)   0.0.0.0:80->80/tcp

curl 테스트

curl -I localhost

curl -I localhost/phpinfo.php

$ curl -I localhost
HTTP/1.1 403 Forbidden
Date: Tue, 08 Mar 2022 09:18:27 GMT
Server: Apache/2.4.6 (CentOS) PHP/7.4.28
X-Powered-By: PHP/7.4.28
Content-Length: 4897
Content-Type: text/html; charset=UTF-8


$ curl -I localhost/phpinfo.php
HTTP/1.1 200 OK
Date: Tue, 08 Mar 2022 09:18:34 GMT
Server: Apache/2.4.6 (CentOS) PHP/7.4.28
X-Powered-By: PHP/7.4.28
Content-Type: text/html; charset=UTF-8
728x90