OpenShift - Resolve "hostPath type check failed is not a file"
by
Jeremy Canfield |
Updated: December 30 2022
| OpenShift articles
Let's say you have a deployment configured to mount a file on the host system in the container, perhaps something like this using hostPath and volumeMounts.
~]$ oc get deployment my-deployment --output yaml
spec:
template:
spec:
containers:
volumeMounts:
- mountPath: /data/files/example.txt
name: myfile
volumes:
- name: myfile
hostPath:
path: /home/john.doe/example.txt
type: File
And when you attempt to create the pod, something like this is being returned.
MountVolume.SetUp failed for volume "my-file" : hostPath type check failed: /home/john.doe/example.txt is not a file
This occurs because the hostPath file needs to exist on the node the pod is running on.
Probably the most typical solution is to create a config map that contains the content of the file you want to mount in the container.
~]# oc create configmap my-config-map --from-file example.txt
configmap/my-config-map created
And then configure the deployment to use the config map.
AVOID TROUBLE
If subPath is not used, example.txt will be mounted as a directory. subPath must be used to mount example.txt as a file.
apiVersion: v1
kind: Deployment
spec:
replicas: 1
template:
spec:
containers:
- name: my-app
image: openshift/my-app:latest
ports:
- containerPort: 80
volumeMounts:
name: my-config-map
mountPath: /data/files/example.txt
subPath: example.txt
volumes:
- name: my-config-map
configMap:
name: my-config-map
Did you find this article helpful?
If so, consider buying me a coffee over at
Comments
January 03 2024 by Keshav Kashyap
You saved my day.