<p>I need to share a directory between two containers: myapp and monitoring and to achieve this I created an emptyDir: {} and then volumeMount on both the containers.</p>
<pre><code>spec:
      volumes:
        - name:  shared-data
          emptyDir: {}
      containers:
      - name: myapp
        volumeMounts:
          - name:  shared-data
            mountPath:  /etc/myapp/
      - name: monitoring
        volumeMounts:
          - name: shared-data
            mountPath: /var/read 
</code></pre>
<p>This works fine as the data I write to the shared-data directory is visible in both containers. However, the config file that is created when creating the container under /etc/myapp/myapp.config is hidden as the shared-data volume is mounted over /etc/myapp path (overlap).</p>
<p>How can I force the container to first mount the volume to /etc/myapp path and then cause the docker image to place the myapp.config file under the default path /etc/myapp except that it is the mounted volume thus allowing the config file to be accessible by the monitoring container under /var/read?</p>
<p>Summary: let the monitoring container read the /etc/myapp/myapp.config file sitting on myapp container.</p>
<p>Can anyone advice please?</p>

<p>Consider using <a href="https://kubernetes.io/docs/concepts/configuration/configmap/" rel="nofollow noreferrer">ConfigMaps</a> with <a href="https://kubernetes.io/docs/concepts/storage/volumes/#using-subpath" rel="nofollow noreferrer">SubPaths</a>.</p>
<blockquote>
<p>A ConfigMap is an API object used to store non-confidential data in
key-value pairs. Pods can consume ConfigMaps as environment variables,
command-line arguments, or as configuration files in a volume.</p>
</blockquote>
<blockquote>
<p>Sometimes, it is useful to share one volume for multiple uses in a
single pod. The <code>volumeMounts.subPath</code> property specifies a sub-path
inside the referenced volume instead of its root.</p>
</blockquote>
<p>ConfigMaps can be used as volumes. The <code>volumeMounts</code> inside the <code>template.spec</code> are the same as any other volume. However, the volumes section is different. Instead of specifying a <code>persistentVolumeClaim</code> or other volume type you reference the configMap by name. Than you can add the <code>subPath</code> property which would look something like this:</p>
<pre><code>volumeMounts:
  - name:  shared-data
    mountPath:  /etc/myapp/
    subPath: myapp.config
</code></pre>
<p>Here are the resources that would show you how to set it up:</p>
<ul>
<li><p><a href="https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/" rel="nofollow noreferrer">Configure a Pod to Use a ConfigMap</a>: official docs</p>
</li>
<li><p><a href="https://dev.to/joshduffney/kubernetes-using-configmap-subpaths-to-mount-files-3a1i" rel="nofollow noreferrer">Using ConfigMap SubPaths to Mount Files</a>: step by step guide</p>
</li>
<li><p><a href="https://carlos.mendible.com/2019/02/10/kubernetes-mount-file-pod-with-configmap/" rel="nofollow noreferrer">Mount a file in your Pod using a ConfigMap</a>: supplement</p>
</li>
</ul>
