2.6. Buildah를 사용한 사용자 정의 이미지 빌드

OpenShift Container Platform 4.9에서는 호스트 노드에 Docker 소켓이 존재하지 않습니다. 즉 사용자 정의 빌드의 Docker 소켓 마운트 옵션에서 사용자 정의 빌드 이미지 내에서 사용하도록 액세스 가능한 Docker 소켓을 제공하지 않을 수 있습니다.

이미지를 빌드하고 내보내기 위해 이 기능이 필요한 경우 사용자 정의 빌드 이미지에 Buildah 툴을 추가한 후 이 툴을 사용하여 사용자 정의 빌드 논리 내에서 이미지를 빌드하고 내보내십시오. 다음은 Buildah를 사용하여 사용자 정의 빌드를 실행하는 방법의 예입니다.

참고

사용자 정의 빌드 전략을 사용하려면 사용자가 클러스터에서 실행되는 권한 있는 컨테이너 내에서 임의의 코드를 실행할 수 있으므로 기본적으로 일반 사용자에게는 없는 권한이 필요합니다. 이 수준의 액세스 권한은 사용 시 클러스터를 손상시킬 수 있으므로 클러스터에 대한 관리 권한이 있는 신뢰할 수 있는 사용자에게만 부여해야 합니다.

2.6.1. 사전 요구 사항

2.6.2. 사용자 정의 빌드 아티팩트 생성

사용자 정의 빌드 이미지로 사용할 이미지를 생성해야 합니다.

프로세스

  1. 빈 디렉터리에서부터 다음 콘텐츠를 사용하여 Dockerfile이라는 파일을 생성합니다.

    FROM registry.redhat.io/rhel8/buildah
    # In this example, `/tmp/build` contains the inputs that build when this
    # custom builder image is run. Normally the custom builder image fetches
    # this content from some location at build time, by using git clone as an example.
    ADD dockerfile.sample /tmp/input/Dockerfile
    ADD build.sh /usr/bin
    RUN chmod a+x /usr/bin/build.sh
    # /usr/bin/build.sh contains the actual custom build logic that will be run when
    # this custom builder image is run.
    ENTRYPOINT ["/usr/bin/build.sh"]
  2. 동일한 디렉터리에서 dockerfile.sample이라는 파일을 생성합니다. 이 파일은 사용자 정의 빌드 이미지에 포함되어 있으며 사용자 정의 빌드에서 생성하는 이미지를 정의합니다.

    FROM registry.access.redhat.com/ubi8/ubi
    RUN touch /tmp/build
  3. 동일한 디렉터리에 build.sh라는 파일을 생성합니다. 이 파일에는 사용자 정의 빌드가 실행될 때 실행되는 논리가 포함되어 있습니다.

    #!/bin/sh
    # Note that in this case the build inputs are part of the custom builder image, but normally this
    # is retrieved from an external source.
    cd /tmp/input
    # OUTPUT_REGISTRY and OUTPUT_IMAGE are env variables provided by the custom
    # build framework
    TAG="${OUTPUT_REGISTRY}/${OUTPUT_IMAGE}"
    
    
    # performs the build of the new image defined by dockerfile.sample
    buildah --storage-driver vfs bud --isolation chroot -t ${TAG} .
    
    
    # buildah requires a slight modification to the push secret provided by the service
    # account to use it for pushing the image
    cp /var/run/secrets/openshift.io/push/.dockercfg /tmp
    (echo "{ \"auths\": " ; cat /var/run/secrets/openshift.io/push/.dockercfg ; echo "}") > /tmp/.dockercfg
    
    
    # push the new image to the target for the build
    buildah --storage-driver vfs push --tls-verify=false --authfile /tmp/.dockercfg ${TAG}

2.6.3. 사용자 정의 빌더 이미지 빌드

OpenShift Container Platform을 사용하여 사용자 정의 전략에서 사용할 사용자 정의 빌더 이미지를 빌드하고 내보낼 수 있습니다.

사전 요구 사항

  • 새 사용자 정의 빌더 이미지를 생성하는 데 사용할 모든 입력을 정의합니다.

프로세스

  1. 사용자 정의 빌더 이미지를 빌드할 BuildConfig 오브젝트를 정의합니다.

    $ oc new-build --binary --strategy=docker --name custom-builder-image
  2. 사용자 정의 빌드 이미지를 생성한 디렉터리에서 빌드를 실행합니다.

    $ oc start-build custom-builder-image --from-dir . -F

    빌드가 완료되면 custom-builder-image:latest라는 이미지 스트림 태그의 프로젝트에서 새 사용자 정의 빌더 이미지를 사용할 수 있습니다.

2.6.4. 사용자 정의 빌더 이미지 사용

사용자 정의 빌더 이미지와 함께 사용자 정의 전략을 사용하여 사용자 정의 빌드 논리를 실행하는 BuildConfig 오브젝트를 정의할 수 있습니다.

사전 요구 사항

  • 새 사용자 정의 빌더 이미지에 필요한 모든 입력을 정의합니다.
  • 사용자 정의 빌더 이미지를 빌드합니다.

프로세스

  1. buildconfig.yaml이라는 파일을 생성합니다. 이 파일은 프로젝트에서 생성되어 실행되는 BuildConfig 오브젝트를 정의합니다.

    kind: BuildConfig
    apiVersion: build.openshift.io/v1
    metadata:
      name: sample-custom-build
      labels:
        name: sample-custom-build
      annotations:
        template.alpha.openshift.io/wait-for-ready: 'true'
    spec:
      strategy:
        type: Custom
        customStrategy:
          forcePull: true
          from:
            kind: ImageStreamTag
            name: custom-builder-image:latest
            namespace: <yourproject> 1
      output:
        to:
          kind: ImageStreamTag
          name: sample-custom:latest
    1
    프로젝트 이름을 지정합니다.
  2. BuildConfig를 생성합니다.

    $ oc create -f buildconfig.yaml
  3. imagestream.yaml이라는 파일을 생성합니다. 이 파일은 빌드에서 이미지를 내보낼 이미지 스트림을 정의합니다.

    kind: ImageStream
    apiVersion: image.openshift.io/v1
    metadata:
      name: sample-custom
    spec: {}
  4. 이미지 스트림을 생성합니다.

    $ oc create -f imagestream.yaml
  5. 사용자 정의 빌드를 실행합니다.

    $ oc start-build sample-custom-build -F

    빌드를 실행하면 빌드에서 이전에 빌드한 사용자 정의 빌더 이미지를 실행하는 Pod를 시작합니다. Pod는 사용자 정의 빌더 이미지의 진입점으로 정의된 build.sh 논리를 실행합니다. build.sh 논리는 Buildah를 호출하여 사용자 정의 빌더 이미지에 포함된 dockerfile.sample을 빌드한 다음 Buildah를 사용하여 새 이미지를 sample-custom image stream으로 내보냅니다.