How to use a config map to create a single file within container?

1/16/2020

I'm trying to configure Nginx container on Openshift. My final goal is to overwrite Nginx config file. I know it's possible using config maps. Because any failure with modifying Nginx config directories is crashing the container, temporarily my goal is to just create an index.html file in /opt/app-root/src directory.

I'm facing two problems depends on the configuration

  • config map overwrite whole /opt/app-root/src directory
  • config map creates index.html directory with index file inside

Config map:

apiVersion: v1
data:
  index: |-
    <html>
    <body>
    yo yo!
    </body>
    </html>
kind: ConfigMap
metadata:
  creationTimestamp: '2020-01-16T12:53:25Z'
  name: index-for-nginx
  namespace: some-namespace

Deployment config (part, related to the topic):

spec:
  containers:
    - image: someimage
      ...
      volumeMounts:
        - mountPath: /opt/app-root/src/index.html
          name: index
  volumes:
    - configMap:
        defaultMode: 420
        name: index-for-nginx
      name: index

When:

volumeMounts:
            - mountPath: /opt/app-root/src/index.html

It creates index.html directory and index file (with proper content) in /opt/app-root/src

When:

volumeMounts:
            - mountPath: /opt/app-root/src/

It overwrites /opt/app-root/src directory

My question is - how should I configure it to create index.html file in /opt/app-root/src without overwriting the directory?

-- Landeeyo
kubernetes
nginx
openshift

2 Answers

1/16/2020

You can do the following, taken from this GitHub issue

containers:
- volumeMounts:
  - name: config-volumes
    mountPath: /opt/app-root/src/index.html
    subPath: index
volumes:
- name: config-volumes
  configMap:
    name: index-for-nginx

Note: A container using a ConfigMap as a subPath volume will not receive ConfigMap updates.

-- Arghya Sadhu
Source: StackOverflow

1/16/2020

You can use subPath to mount the single file you want.

volumeMounts:
  - mountPath: /opt/app-root/src/index.html
    subPath: index
    name: index

https://kubernetes.io/docs/concepts/storage/volumes/#using-subpath

-- Shashank V
Source: StackOverflow