Prompt
troubleshoot app files disappearing after container restart
Latest observation
If files inside your app container disappear after a restart, it’s usually because they were written to the container’s writable layer, which is temporary. Containers are meant to be ephemeral unless you use a volume or bind mount.
Most common causes
-
No persistent volume mounted
- Files created in
/app,/data, etc. inside the container vanish when the container is removed/recreated.
- Files created in
-
App writes to the wrong path
- Your app may be writing to a directory that isn’t mounted to persistent storage.
-
Container is being recreated, not just restarted
docker compose up --build, redeploys, or Kubernetes pod reschedules create a new container filesystem.
-
Permissions or startup script overwrites files
- An entrypoint/init process may recreate or replace files on each start.
-
Volume is mounted over existing files
- If you mount an empty volume onto
/app, it can hide files that were baked into the image.
- If you mount an empty volume onto
How to troubleshoot
1) Confirm where the files are being written
Check inside the container:
docker exec -it <container> sh
pwd
ls -la
mount
If your app stores files in /app/uploads, /data, etc., verify that exact directory.
2) Check whether a volume is mounted
Run:
docker inspect <container> --format '{{json .Mounts}}' | jq
Look for:
Type: volumeType: bind
If there’s no mount for the path where files are saved, they won’t persist.
3) Test persistence
Create a file in the suspected path:
docker exec -it <container> sh -c 'echo test > /app/uploads/test.txt'
docker restart <container>
docker exec -it <container> sh -c 'cat /app/uploads/test.txt'
If it’s gone, that path isn’t persistent.
4) Inspect your Compose or run command
Example with Docker Compose:
services:
app:
image: myapp
volumes:
- app-data:/app/uploads
volumes:
app-data:
Or bind mount:
volumes:
- ./uploads:/app/uploads
If you’re using docker run, make sure you have something like:
docker run -v mydata:/app/uploads myapp
5) Check app startup behavior
Look at logs and entrypoint scripts:
docker logs <container>
Search for actions like:
- cleaning temp directories
- reinitializing the database
- copying default files over existing ones
6) If using Kubernetes
Use:
PersistentVolume/PersistentVolumeClaim- mount the PVC to the directory where the app writes data
Example:
volumeMounts:
- name: app-storage
mountPath: /app/uploads
volumes:
- name: app-storage
persistentVolumeClaim:
claimName: app-pvc
Quick rule of thumb
- Code → baked into the image
- Generated/uploads/state/data → store in a volume or external storage
- Logs → send to stdout/stderr or persistent log storage
If you want, I can help you debug your exact setup
Paste one of these:
- your
docker runcommand - your
docker-compose.yml - your Kubernetes Deployment/StatefulSet YAML
- the path where files disappear
and I’ll pinpoint the issue.
Brands