<p>I have a legacy app which keep checking an empty file inside a directory and perform certain action if the file timestamp is changed.</p>
<p>I am migrating this app to Kubernetes so I want to create an empty file inside the pod. I tried subpath like below but it doesn't create any file.</p>
<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: demo-pod
spec:
  containers:
    - name: demo
      image: alpine
      command: [&quot;sleep&quot;, &quot;3600&quot;]
      volumeMounts:
      - name: volume-name
        mountPath: '/volume-name-path'
        subPath: emptyFile
  volumes:
    - name: volume-name
      emptyDir: {} 
</code></pre>
<p>describe pods shows</p>
<pre><code>Containers:
  demo:
    Container ID:  containerd://0b824265e96d75c5f77918326195d6029e22d17478ac54329deb47866bf8192d
    Image:         alpine
    Image ID:      docker.io/library/alpine@sha256:08d6ca16c60fe7490c03d10dc339d9fd8ea67c6466dea8d558526b1330a85930
    Port:          &lt;none&gt;
    Host Port:     &lt;none&gt;
    Command:
      sleep
      3600
    State:          Running
      Started:      Wed, 10 Feb 2021 12:23:43 -0800
    Ready:          True
    Restart Count:  0
    Environment:    &lt;none&gt;
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-4gp4x (ro)
      /volume-name-path from volume-name (rw,path=&quot;emptyFile&quot;)
</code></pre>
<p>ls on the volume also shows nothing.
<code>k8 exec -it demo-pod -c demo ls /volume-name-path</code></p>
<p>any suggestion??</p>
<p>PS: I don't want to use a ConfigMap and simply wants to create an empty file.</p>

nothing_authentic
<p>If the objective is to create a empty file when the Pod starts, then the most easy way is to either use the entrypoint of the docker image or an init container.</p>
<p>With the initContainer, you could go with something like the following (or with a more complex init image which you build and execute a whole bash script or something similar):</p>
<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: demo-pod
spec:
  initContainers:
    - name: create-empty-file
      image: alpine
      command: [&quot;touch&quot;, &quot;/path/to/the/directory/empty_file&quot;]
      volumeMounts:
      - name: volume-name
        mountPath: /path/to/the/directory
  containers:
    - name: demo
      image: alpine
      command: [&quot;sleep&quot;, &quot;3600&quot;]
      volumeMounts:
      - name: volume-name
        mountPath: /path/to/the/directory
  volumes:
    - name: volume-name
      emptyDir: {}
</code></pre>
<p>Basically the init container gets executed first, runs its command and if it is successful, then it terminates and the main container starts running. They share the same volumes (and they can also mount them at different paths) so in the example, the init container mount the emptyDir volume, creates an empty file and then complete. When the main container starts, the file is already there.</p>
<p>Regarding your legacy application which is getting ported on Kubernetes:</p>
<p>If you have control of the Dockerfile, you could simply change it create an empty file at the path you are expecting it to be, so that when the app starts, the file is already created there, empty, from the beginning, just exactly as you add the application to the container, you can add also other files.</p>
<p>For more info on init container, please check the documentation (<a href="https://kubernetes.io/docs/concepts/workloads/pods/init-containers/" rel="nofollow noreferrer">https://kubernetes.io/docs/concepts/workloads/pods/init-containers/</a>)</p>
