cp: can't create '/node_modules/mongo-express/config.js': File exists

8/10/2021

Problem with kubernetes volume mounts.

The mongo-express container has a file /node-modules/mongo-express/config.js

I need to overwrite the /node-modules/mongo-express/config.js with my /tmp/config.js

I am trying to copy my custom config.js under /tmp (volume mount by ConfigMaps) to the folder under the container path /node-modules/mongo-express.

But I am not able to do that and get the below error:

cp: can't create '/node_modules/mongo-express/config.js': File exists

Below we can find the deployment.yaml I am using to achieve this.

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mongo-express
spec:
  selector:
    matchLabels:
      app: mongo-express
  template:
    metadata:
      labels:
        app: mongo-express
    spec:
      containers:
        - name: mongo-express
          image: mongo-express:latest
          command:
            - sh
            - -c
            - cp /tmp/config.js /node_modules/mongo-express
          ports:
            - name: mongo-express
              containerPort: 8081
          volumeMounts:
            - name: custom-config-js
              mountPath: /tmp
      volumes:
        - name: custom-config-js
          configMap:
            name: mongodb-express-config-js

I tried:

  • cp -f /tmp/config.js /node_modules/mongo-express
  • cp -r /tmp/config.js /node_modules/mongo-express
  • \cp -r /tmp/config.js /node_modules/mongo-express

and much more. But with no success. Any help is much appreciated.

-- samarth urs
docker
kubernetes
mongo-express

1 Answer

8/10/2021

Most container images are immutable.

What you probably want here is a subPath mount instead:

volumeMounts:
- mountPath: /node_modules/mongo-express/config.js
  name: custom-config-js
  subPath: config.js
-- coderanger
Source: StackOverflow