A container’s own files are temporary: stopping, destroying, or rebuilding the container can discard changes. A volume gives the container a mounted place for data that can outlive it. This makes an otherwise ephemeral container useful for stateful tasks, such as storing database data. section 6.3 section 6.4.2
Mount a host folder with Docker
Run `docker run -v ~/dev/roadtok8s/py/data/:/app/data/ --name my-rk8s-py rk8s-py`. The path before the colon is the host folder; the path after it is where that folder appears inside the container. Append text to `~/dev/roadtok8s/py/data/sample.txt`, then run `docker exec my-rk8s-py cat /app/data/sample.txt` to read it inside the container. Changes made at the mounted path inside the container can likewise be read from the host folder. The name supplied by `--name` identifies the running container; `rk8s-py` is its image. section 6.3.1
For a repeatable setup, put the mount in a Compose service’s `volumes` list instead of remembering a long `docker run` command. A host-folder mount such as `~/dev/roadtok8s/py/data:/app/data` keeps that folder outside Compose’s volume management. Run `docker compose up` to start the service and `docker compose exec web ls /app/data` to check the mounted files. section 6.3.2
Listing 6.7 Managing volumes with Docker Compose
services:
web:
build:
context: .
dockerfile: Dockerfile
ports:
- "8080:8080"
environment:
- PORT=8080
volumes:
- mywebdata:/app/data
volumes:
mywebdata:
This Compose configuration shows a different choice: declare `mywebdata` at the top level, then mount it into the `web` service at `/app/data`. Compose manages this named volume rather than using a project folder. section 6.3.3
Choose the volume’s owner before deleting a stack. `docker compose down` leaves data in Compose-managed mounted volumes; `docker compose down -v` removes those volumes and their data. It does not remove a self-managed host directory. A volume declared `external: true` is also outside Compose’s management and must already exist. section 6.3.3 section 6.4.2
Keep a Redis container’s data
In a Compose setup with a Python web service, mount `myredisdata:/data` on the Redis service and declare `myredisdata` under the top-level `volumes` key. The mount gives the database a place for persistent data; avoid `docker compose down -v` when that data must survive. section 6.4.2



