<p>I'm trying to change my express server deployment from Deployment to Statefulsets in my Kubernetes cluster. After I added the volumeMounts to the yaml file, I gets the following error from the pod's log:</p>

<pre><code>npm ERR! path /usr/src/app/package.json
npm ERR! code ENOENT
npm ERR! errno -2
npm ERR! syscall open
npm ERR! enoent ENOENT: no such file or directory, open '/usr/src/app/package.json'
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent 

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2019-08-23T13_55_03_058Z-debug.log
</code></pre>

<p><strong>My previous files:</strong></p>

<p>Dockerfile</p>

<pre><code>FROM node:10.16-alpine

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install

COPY . .

CMD [ "npm", "start" ]
</code></pre>

<p>Service yaml file</p>

<pre><code>apiVersion: v1
kind: Service
metadata:
  name: image-server-cluster-ip-service
spec:
  type: ClusterIP
  selector:
    component: image-server
  ports:
  - port: 3001
    targetPort: 3001
</code></pre>

<p>deployment yaml file</p>

<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: image-server-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      component: image-server
  template:
    metadata:
      labels:
        component: image-server
    spec:
      containers:
      - name: image-server
        image: gcr.io/my-project/image-server:v0.0.10
        ports:
        - containerPort: 3001
        env:
          - name: MONGO_HOST
            value: mongo-cluster-ip-service
          - name: MONGO_PORT
            value: '27017'
</code></pre>

<p><strong>My current file:</strong></p>

<pre><code>headless service file

apiVersion: v1
kind: Service
metadata:
  name: image-server-cluster-ip-service
spec:
  clusterIP: None
  selector:
    component: image-server
  ports:
  - port: 3001
    targetPort: 3001
</code></pre>

<p>statefulsets yaml file</p>

<pre><code>apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: image-server-deployment
spec:
  serviceName: "image-server-cluster-ip-service"
  replicas: 2
  selector:
    matchLabels:
      component: image-server
  template:
    metadata:
      labels:
        component: image-server
    spec:
      containers:
      - name: image-server
        image: gcr.io/my-project/image-server:v0.0.10
        ports:
        - containerPort: 3001
        env:
          - name: MONGO_HOST
            value: mongo-cluster-ip-service
          - name: MONGO_PORT
            value: '27017'
        volumeMounts:
          - mountPath: /usr/src/app
            name: image-server-vol
  volumeClaimTemplates:
    - metadata:
        name: image-server-vol
      spec:
        accessModes: ["ReadWriteOnce"]
        resources:
          requests:
            storage: 1Gi
</code></pre>

<p>I think the package.json file might be deleted when I added the volume mount. </p>

<p>What is the proper way to mount the volume to my pod?</p>

Jack
<p>From the comment discussion, your use case is to handle users' uploaded files.</p>

<p>You should not use a PV for this use case, for various reasons. One reason is that your two StatefulSet replicas will have different volumes mounted, so if a user uploads a file and that request gets handled by one replica, and then later they try to view their file and that request is handled by the other replica, it won't be there. Consider using a blobstore service like S3, Google Cloud Filestore,  Minio, etc. This will require a bit more code for you to write, and probably pull in some client libraries, but it's a better practice.</p>

<p>For your edification, though you shouldn't use PVs for this use case, here's why it was causing the specific error you were seeing: You're mounting the volume to <code>/usr/src/app</code> so it's going to blow away whatever was at that place in the filesystem within the container, namely all your app source code you put there via the <code>COPY</code> commands when building your Docker image. In the future when using PVs, be sure to mount the volume to a different path that won't blow away the files you've put there intentionally.</p>
