Video in progress

What happens to the files inside a container when it is removed, and how does a volume solve this problem?

The container's writable layer is deleted with it, and the files inside are lost. A volume is storage outside the container, and it survives when the container is replaced.

📖 Answer

For example, the app writes images uploaded by users to /app/uploads. A new version is deployed, the old container is removed, and the images go with it.

# named volume
docker volume create shop-uploads
docker run -v shop-uploads:/app/uploads shop-api:1.4.0

# mount a host folder (bind mount)
docker run -v /srv/shop/uploads:/app/uploads shop-api:1.4.0

Now /app/uploads actually lives outside the container, and it is attached to the next container as well.

The Kubernetes analog is a PersistentVolumeClaim. But there is a catch: if you run the app on several replicas, a local disk becomes awkward. That is why files are often written to object storage such as S3 or MinIO, and the app is kept stateless.