<p>In my application, I have a control plane component which spawns Jobs on my k8s cluster.  I'd like to be able to pass in a dynamically generated (but read-only) config file to each Job.  The config file will be different for each Job.</p>

<p>One way to do that would be to create, for each new Job, a ConfigMap containing the desired contents of the config file, and then set the ConfigMap as a VolumeMount in the Job spec when launching the Job.  But now I have two entities in the cluster which are semantically tied together but don't share a lifetime, i.e. if the Job ends, the ConfigMap won't automatically go away.</p>

<p>Is there a way to directly "mount a string" into the Job's Pod, without separately creating some backing entity like a ConfigMap to store it?  I could pass it in as an environment variable, I guess, but that seems fragile due to length restrictions.</p>

<p>The way that is traditionally done is via an <code>initContainer</code> and an <code>emptyDir</code> <code>volumeMount</code> that allows the two containers to "communicate" over a private shared piece of disk:</p>

<pre class="lang-yaml prettyprint-override"><code>spec:
  initContainers:
  - name: config-gen
    image: docker.io/library/busybox:latest
    command:
    - /bin/sh
    - -ec
    # now you can use whatever magick you wish to generate the config
    - |
      echo "my-config: is-generated"   &gt; /generated/sample.yaml
      echo "some-env: ${SOME_CONFIG}" &gt;&gt; /generated/sample.yaml
    env:
    - name: SOME_CONFIG
      value: subject to injection like any other kubernetes env var
    volumeMounts:
    - name: shared-space
      mountPath: /generated
  containers:
  - name: main
    image: docker.example.com:1234
    # now you can do whatever you want with the config file
    command:
    - /bin/cat
    - /config/sample.yaml
    volumeMounts:
    - name: shared-space
      mountPath: /config
  volumes:
  - name: shared-space
    emptyDir: {}
</code></pre>
