<p>I am going to use K8S to orchestrate docker containers. In k8s, I need to copy a file from host directory (<code>/configs/nginx/cas-server.conf</code>) to pod container directory(<code>/etc/nginx/nginx.conf</code>), but the current k8s only allows mount a directory, not to mount/copy a file. How to solve this problem?  </p>

<p>Below is my nginx-cas-server-deply.yaml file. </p>

<pre><code>apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: nginx-cas-server-depl
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: nginx-cas-server-pod
    spec:
      containers:
      - name: nginx-cas-server-pod
        image: nginx
        imagePullPolicy: Never
        ports:
          - containerPort: 100
        volumeMounts:
        - mountPath: /etc/nginx/nginx.conf
          name: nginx-cas-server-conf
        - mountPath: /app/cas-server/public
          name: nginx-cas-server-public
      volumes:
      - name: nginx-cas-server-conf
        hostPath:
          path: /configs/nginx/cas-server.conf
      - name: nginx-cas-server-public
        hostPath:
          path: /cas-server/public
</code></pre>

<p>In a configuration for your Deployment, you need to use <code>mountPath</code> with <strong>directory</strong> and <strong>file</strong> names and <code>subPath</code> field with <strong>file</strong> name. Also, what is important, you need to have file on a Node named exactly as you want it to be mounted, therefore if you want to mount to <code>/etc/nginx/nginx.conf</code>, file should be named <code>nginx.conf</code></p>

<p>Here is the example:</p>

<p>Content of the directory on the Node:</p>

<pre><code># ls /config/
nginx.conf  some_dir
</code></pre>

<p>Configuration file for Nginx Deployment</p>

<pre><code>apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  labels:
    run: nginx
  name: nginx
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      run: nginx
  template:
    metadata:
      labels:
        run: nginx
    spec:
      containers:
      - image: nginx
        name: nginx
        volumeMounts:
        - mountPath: /etc/nginx/nginx.conf 
          name: test
          subPath: nginx.conf
      volumes:
      - hostPath:
          path: /config
        name: test
</code></pre>
