liveBook · Manning

Docker volume

As explained in Road to Kubernetes

A Docker volume is storage mounted into a container that can persist data independently of the container filesystem and may be shared with the host or other containers.

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

How Road to Kubernetes covers it

How deep it goes
Works through CLI mounts and Compose declarations, including ownership and deletion behavior; does not develop storage internals.
Examples
Uses a Python container’s shared sample file, Compose YAML mounts, and a Redis service with a `/data` mount.
What it assumes
Introduces volume mounts from basic container behavior while using a previously built image for the CLI example.
What you can do after
Operate/configure it with `docker run -v` or Compose, verify mounted files, and avoid deleting needed managed-volume data.
How it connects
Connects container lifecycle and stateful databases to Docker Compose; distinguishes these mounts from Kubernetes ConfigMap and persistent-volume uses.

Also explained in

Compare how 4 books explain Docker volume →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Road to Kubernetes. The section links point to the parts of the book it draws on.