CodePark/ questions / Volumes & bind mounts
START PRACTICINGPRACTICE
Backend question · Docker › Volumes & bind mounts

A container writes uploaded files to /app/uploads with no volume mounted. What happens to those files after docker stop, then after docker rm, and after deploying a new image version?

The files are written to the container's writable layer (the container's own private layer on top of the image).

  • After docker stop: the files are still there. A stopped container keeps its writable layer. docker start brings the container back with the files.
  • After docker rm: Docker deletes the writable layer. The files are gone for good.
  • After deploying a new image version: this creates a new container. It starts with a fresh, empty writable layer. So the old uploads are not there either.

To keep data when you replace containers, mount a named volume at /app/uploads. For a service that scales to many containers, it is better to store uploads in external object storage, such as S3. Treat containers as disposable.

  • The data goes into the container's writable layer.
  • The data survives docker stop and docker start.
  • The data is lost on docker rm.
  • A new container from a new image starts with no data.
  • Use a volume or external storage to keep the data.