Skip to content

Building an Image and Using it in a Task

This guide will show you how to build and use an image within one job without pushing the image to an external image registry like Docker Hub.

Build The Image

To avoid repeating ourselves we're going to use the pipeline made in the other guide Building and Pushing an Image. We will start with the pipeline from the Defining the Build Context section.

We will add the UNPACK_ROOTFS parameter to the task. This parameter tells the oci-build-task to include the image in a special format that Concourse's container runtime uses.

Note

In the future this may not be necessary if Concourse starts using the OCI image format.

build-and-use-image.yml
---
resources:
  # The repo with our Dockerfile
  - name: concourse-examples
    type: git
    icon: github
    source:
      uri: https://github.com/concourse/examples.git
      branch: main

jobs:
  - name: build-and-use-image
    plan:
      - get: concourse-examples
      - task: build-task-image
        privileged: true
        config:
          platform: linux
          image_resource:
            type: registry-image
            source:
              # Check out the README for oci-build-task at
              # https://github.com/concourse/oci-build-task
              repository: concourse/oci-build-task
          inputs:
            - name: concourse-examples
          outputs:
            - name: image
          params:
            CONTEXT: concourse-examples/Dockerfiles/simple
            UNPACK_ROOTFS: "true" # only needed if using image in a future step
          run:
            path: build

The above pipeline will build a container image and also output it in Concourse's rootfs image format.

Use the Image

Next we want to add a second task to this job which will use the image generated from the first task as its container image. To use the image from the previous step add the top-level image key to the task step.

build-push.yml
resources: ... # omitting resource section from above

jobs:
  - name: build-and-use-image
    plan:
      - get: concourse-examples
      - task: build-task-image
        privileged: true
        config:
          platform: linux
          image_resource:
            type: registry-image
            source:
              # Check out the README for oci-build-task at
              # https://github.com/concourse/oci-build-task
              repository: concourse/oci-build-task
          inputs:
            - name: concourse-examples
          outputs:
            - name: image
          params:
            CONTEXT: concourse-examples/Dockerfiles/simple
            UNPACK_ROOTFS: "true" # only needed if using image in a future step
          run:
            path: build
      - task: use-built-image-in-task
        image: image
        config:
          platform: linux
          run:
            path: cat
            args: ["/stranger"]

You can set the pipeline with the following fly command.

fly -t <target> set-pipeline -p build-and-use-image \
  -c ./build-and-use-image.yml