<p>There is a folder name &quot;data-persistent&quot; in the running container that the code reads and writes from, I want to save the changes made in that folder. when I use persistent volume, it removes/hides the data from that folder and the code gives an error. So what should be my approach.</p>
<pre><code>FROM python:latest
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
#RUN mkdir data-persistent
ADD linkedin_scrape.py .
COPY requirements.txt ./requirements.txt
COPY final_links.csv ./final_links.csv
COPY credentials.txt ./credentials.txt
COPY vectorizer.pk ./vectorizer.pk
COPY model_IvE ./model_IvE
COPY model_JvP ./model_JvP
COPY model_NvS ./model_NvS
COPY model_TvF ./model_TvF
COPY nocopy.xlsx ./nocopy.xlsx
COPY data.db /data-persistent/
COPY textdata.txt /data-persistent/
RUN ls -la /data-persistent/*
RUN pip install -r requirements.txt
CMD python linkedin_scrape.py --bind 0.0.0.0:8080 --timeout 90
</code></pre>
<p>And my deployment file</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-first-cluster1
spec:
  replicas: 2
  selector:
    matchLabels:
      app: scrape
  template:
    metadata:
      labels:
        app: scrape
    spec:
      containers:
      - name: scraper
       
        image: image-name
        #
        ports:
        - containerPort: 8080
        env:
        - name: PORT
          value: &quot;8080&quot;

        volumeMounts:
        - mountPath: &quot;/dev/shm&quot;
          name: dshm
        - mountPath: &quot;/data-persistent/&quot;
          name: tester
      volumes:
      - name: dshm
        emptyDir:
          medium: Memory
      - name: tester
        persistentVolumeClaim:
          claimName: my-pvc-claim-1
</code></pre>
<p>Let me explain the workflow of the code. The code reads from the textdata.txt file which contains the indices of links to be scraped e.g. from 100 to 150, then it scrapes the profiles, inserts them to data.db file and then writes to the texdata.txt file the sequence to be scraped in next run e.g. 150 to 200.</p>

Sardar Arslan
<p>First , k8s volume mounting point overwrite the original file system /data-persistent/</p>
<p>To solve such a case you have many options</p>
<p><strong>Solution 1</strong></p>
<ul>
<li>edit your docker file to copy local data to /tmp-data-persistent</li>
<li>then add &quot;init container&quot; that copy content of /tmp-data-persitent to /data-persistent that will copy the data to the volume and apply persistency</li>
</ul>
<p><strong>Solution 2</strong></p>
<ul>
<li><p>its not good to copy data in docker images , that will increase images sizes and also unityfing code and data change pipelines</p>
</li>
<li><p>Its better to keep data in any shared storage like &quot;s3&quot; , and let the &quot;init container&quot; compare and sync data</p>
</li>
</ul>
<p>if cloud services like s3 not available</p>
<ul>
<li><p>you can use persistent volume type that support multipe r/w mounts</p>
</li>
<li><p>attach same volume to another deployment { use busybox image as example } and do the copy with &quot;kubectl cp&quot;</p>
</li>
<li><p>scale temp deployments to zero after finalizing the copy , also you
can make it as part of CI pipeline</p>
</li>
</ul>
