apiVersion: batch/v1
kind: Job
metadata:
  name: cabotage-dind-builder
spec:
  template:
    spec:
      containers:
      - name: cabotage-dind
        image: docker:stable-dind
        command: ["/bin/sh", "-c"]
        args:
          - |
            dockerd --host=unix:///var/run/docker.sock &
            CHILD_PID=$!
            (while true; do if [[ -f "/var/run/build-terminated" ]]; then kill $CHILD_PID; fi; sleep 1; done) &
            wait $CHILD_PID
            if [[ -f "/var/run/build-terminated" ]]; then exit 0; fi
        securityContext:
          privileged: true
        volumeMounts: 
          - mountPath: /var/run 
            name: docker-sock 
      - name: cabotage-dind-builder
        image: docker:stable
        command: ["/bin/sh", "-c"]
        args:
          - |
            trap "touch /var/run/build-terminated" EXIT
            docker images
        volumeMounts: 
          - mountPath: /var/run 
            name: docker-sock 
      restartPolicy: Never
      volumes:
        - name: docker-sock
          emptyDir:
            medium: "Memory"
            sizeLimit: "1k"
  backoffLimit: 10
