A container can write a file in its own writable layer. If you remove that container, Docker removes that layer. A named volume is storage that Docker manages separately. You can attach it to a replacement container.
Docker Compose · Lesson 4
Volumes
Learn why a named volume keeps files across container replacements.