docker-in-dockertektontekton-pipelines

Is it possible to have a sidecar persist across multiple tasks in a Tekton pipeline?


Tekton's build-in sidecar functionality allows you to define sidecars that will run alongside a single task and stop when the task completes. The specific use-case I'm concerned with is using a Docker-in-Docker sidecar to build an image in one task, and then pushing that image to a registry in a separate task.


Solution

  • In Kubernetes terms, Tekton Tasks are Pods. If you run two Tasks: at best, you can re-use the same sidecar code in both. But short answer: no, you can't have a single sidecar container shared, in between two Pods.

    Regarding your use case: build and push. I would first suggest you to implement both as "steps" from the same Task, rather than different Tasks.

    A Task is a list of steps, each of them is a separate container in your Pod. They would run sequentially (first step would start, exit, then second step would start, ...). If you have a sidecar in your Task, then that container would live through the whole Task execution: both your steps container would be able to query it, sharing the same runtime data.

    The TektonCD Catalog repository has some examples that could help you, such as the docker-build task: https://github.com/tektoncd/catalog/blob/main/task/docker-build/0.1/docker-build.yaml

    If for some reason, you have two implement build and push as separate Tasks, then one way to do this may be to use a PersistentVolumeClaim, attached to both tasks. Either share your DIND sidecar data volume, or export your image as a .tar archive, once you're done building your image, and load it into your next Task's runtime.