<p>I'm having an issue with volumes on Kubernetes when I'm trying to mount hostPath volumes. (i also tried with PVC, but no success)</p>
<p>Dockerfile:</p>
<pre><code>FROM node:16
WORKDIR /usr/src/app
COPY package.json /usr/src/app
RUN yarn install
COPY . /usr/src/app
EXPOSE 3000
ENTRYPOINT [&quot;yarn&quot;, &quot;start:dev&quot;]
</code></pre>
<p>docker-compose.yml:</p>
<pre><code>version: '3.8'

services:
  api:
    container_name: api
    build:
      context: .
      dockerfile: Dockerfile
    volumes:
      - .:/usr/src/app
      - /usr/src/app/node_modules
    ports:
      - 3000:3000
    restart: always
    labels:
      kompose.volume.type: 'hostPath'
  database:
    container_name: database
    image: postgres:latest
    ports:
      - 5432:5432
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: task-management
</code></pre>
<p>api-development.yml</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    kompose.cmd: kompose -f docker-compose.yml convert
    kompose.version: 1.26.1 (HEAD)
    kompose.volume.type: hostPath
  creationTimestamp: null
  labels:
    io.kompose.service: api
  name: api
spec:
  replicas: 1
  selector:
    matchLabels:
      io.kompose.service: api
  strategy:
    type: Recreate
  template:
    metadata:
      annotations:
        kompose.cmd: kompose -f docker-compose.yml convert
        kompose.version: 1.26.1 (HEAD)
        kompose.volume.type: hostPath
      creationTimestamp: null
      labels:
        io.kompose.service: api
    spec:
      containers:
        - image: task-management_api
          name: api
          imagePullPolicy: Never
          ports:
            - containerPort: 3000
          resources: {}
          volumeMounts:
            - mountPath: /usr/src/app
              name: api-hostpath0
            - mountPath: /usr/src/app/node_modules
              name: api-hostpath1
      restartPolicy: Always
      volumes:
        - hostPath:
            path: /Users/handrei/workspace/devs/nest-ws/task-management
          name: api-hostpath0
        - hostPath:
          name: api-hostpath1
status: {}
</code></pre>
<p>the error I received from the pod is the next one:</p>
<p>kubectl logs api-84b56776c5-v86c7</p>
<pre><code>yarn run v1.22.17
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
error Couldn't find a package.json file in &quot;/usr/src/app&quot;
</code></pre>
<p>I assume that's something wrong with volumes because applying the deployment and service without volumes it's working</p>

andrei
<blockquote>
<p>A  <a href="https://kubernetes.io/docs/concepts/storage/volumes/#hostpath" rel="nofollow noreferrer"><code>hostPath</code></a>  volume mounts a file or directory from the host node's filesystem into your Pod.</p>
</blockquote>
<p>To the required  <code>path</code>  property, you can also specify a  <code>type</code>  for a  <code>hostPath</code>  volume.</p>
<blockquote>
<p><strong>NOTE</strong>: HostPath volumes present many security risks, and it is a best practice to avoid the use of HostPaths when possible. When a HostPath volume must be used, it should be scoped to only the required file or directory, and mounted as  <strong>ReadOnly</strong>.</p>
</blockquote>
<hr />
<p>As @<a href="https://stackoverflow.com/users/10008173/david-maze" title="87,783 reputation">David Maze</a> mentioned before, It's better idea to</p>
<blockquote>
<p>use Node locally for day-to-day development and use a self-contained image (without any volume mounts at all) in Kubernetes. (...)</p>
<p>The <code>node_modules</code> directory is empty and nothing in Kubernetes will every copy data there. You'll need to delete all of the volume declarations from your Deployment spec for this to run.</p>
</blockquote>
<hr />
<p><a href="https://kubernetes.io/docs/tasks/configure-pod-container/translate-compose-kubernetes/" rel="nofollow noreferrer">This quide</a> will help you to translate a Docker Compose File to Kubernetes Resources.</p>
<p>See also this questions on StackOverflow:</p>
<ul>
<li><a href="https://stackoverflow.com/questions/39651908/why-node-modules-is-empty-after-docker-build">Why <em>node_modules</em> is empty after docker build?</a></li>
<li><a href="https://stackoverflow.com/questions/43910919/kubernetes-volume-for-node-modules">Kubernetes volume for <em>node_modules</em></a></li>
</ul>
