Unable to upload a file through a deployment yaml in kubernetes

2/2/2021

I am unable to upload a file through a deployment YAML in Kubernetes.
The deployment YAML

apiVersion: apps/v1
kind: Deployment
metadata:
  name: test
  labels:
    app: test
spec:
  replicas: 1
  selector:
    matchLabels:
      app: test
  template:
    metadata:
      labels:
        app: test
    spec:
      containers:
      - name: test
        image: openjdk:14
        ports:
        - containerPort: 8080
        volumeMounts: 
        - name: testing
          mountPath: "/usr/src/myapp/docker.jar"
        workingDir: "/usr/src/myapp"
        command: ["java"]
        args: ["-jar", "docker.jar"]        
      volumes: 
      - hostPath: 
          path: "C:\\Users\\user\\Desktop\\kubernetes\\docker.jar"
          type: File
        name: testing

I get the following error:

Events:
  Type     Reason     Age                From               Message
  ----     ------     ----               ----               -------
  Normal   Scheduled  19s                default-scheduler  Successfully assigned default/test-64fb7fbc75-mhnnj to minikube
  Normal   Pulled     13s (x3 over 15s)  kubelet            Container image "openjdk:14" already present on machine
  Warning  Failed     12s (x3 over 14s)  kubelet            Error: Error response from daemon: invalid mode: /usr/src/myapp/docker.jar

When I remove the volumeMount it runs with the error unable to access docker.jar.

        volumeMounts: 
        - name: testing
          mountPath: "/usr/src/myapp/docker.jar"
-- Yanir
kubernetes

1 Answer

2/4/2021

This is a community wiki asnwer. Feel free to expand it.

That is a known issue with Docker on Windows. Right now it is not possible to correctly mount Windows directories as volumes.

You could try some of the workarounds mentioned by @CodeWizard in this github thread like here or here.

Also, if you are using VirtualBox, you might want to check this solution:

On Windows, you can not directly map Windows directory to your container. Because your containers are reside inside a VirtualBox VM. So your docker -v command actually maps the directory between the VM and the container.

So you have to do it in two steps:

Map a Windows directory to the VM through VirtualBox manager Map a directory in your container to the directory in your VM You better use the Kitematic UI to help you. It is much eaiser.

Alternatively, you can deploy your setup on Linux environment to completely omit those specific kind of issues.

-- WytrzymaƂy Wiktor
Source: StackOverflow