Here’s how you can track the number of times the volume has been accessed using a Python program inside a Docker container based on python:3.7-alpine.
Run the following command to create the volume: refer code volume_creation.sh [click me...]
./volume_creation.shThis script will create a file inside the volume (/data_folder/access_count.txt) and increment the count each time the script runs.
see the code of app.py: Link [click me]
This Dockerfile sets up a Python 3.7 Alpine container with our script. dockerfile contant: Link [click me]
see the code of run build docker image : Link [click me] Run the following command to build the image:
./build_script.shsee the contant of run.sh: Link [click me]
Run the container and mount the volume:
./run.shEach time you run the container, the script inside will increment and display the access count.
To manually check the access count, you can start a temporary container and inspect the file:
docker run --rm --init \
--mount type=volume,source=python_data_volume,target=/data_folder \
python-volume-access-tracter cat /data_folder/access_count.txt- Link: [click me]
When you first run the container:
Volume accessed for the first time.
On subsequent runs:
Volume has been accessed 2 times.
Volume has been accessed 3 times.
...
- The script ensures that the access count persists across container runs using Docker volumes.
- Python 3.7 Alpine is used for a lightweight environment.
- The
track_access.pyscript automatically updates and logs access counts in/data_folder/access_count.txt.