liveBook · Manning

Docker volume

As explained in Bootstrapping Microservices, Second Edition: With Docker, Kubernetes, GitHub Actions, and Terraform

A Docker volume is persistent storage managed separately from a container's writable filesystem and lifecycle.

A Docker volume stores or shares files outside a container’s ordinary filesystem. In development, it connects the host filesystem to a container so code edited in VS Code appears quickly in the running microservice. section 5.5.4

Use Docker volumes to:

  • Mount source code directly into a development container instead of baking it into the image. section 5.5.4
  • Persist the npm cache on the host so recreating a container can make later package installation faster. section 5.5.4
  • Remove volumes with docker compose down --volumes when a clean application restart is required. section 4.3.5

Mount source and npm cache with Compose

Configure the development service with Dockerfile-dev and two volumes: /tmp/history/npm-cache:/root/.npm:z preserves the npm cache, while ./history/src:/usr/src/app/src:z shares source code. With live reload, edits on the host can reload the microservice without rebuilding and restarting the whole application. section 5.5.4 section 5.11

Listing 5.5 Updating the Docker Compose file for live reload
version: '3'
services:
 
  --snip--
 
  history:                                     #1
    image: history
    build: 
      context: ./history
      dockerfile: Dockerfile-dev               #2
    container_name: history
    volumes:                                   #3
      - /tmp/history/npm-cache:/root/.npm:z    #4
      - ./history/src:/usr/src/app/src:z       #5
    ports:
     - "4002:80"
    environment:
      - PORT=80
      - NODE_ENV=development
    restart: "no"

Inspect the complete Compose extract showing the development image, volume mappings, ports, environment, and restart policy. section 5.5.4

Stopping containers does not remove them, and restarting can restore their old filesystem state. For a clean development reset, run docker compose down --volumes; combine it with docker compose up --build when rebuilding is also needed. section 4.3.5

How Bootstrapping Microservices, Second Edition: With Docker, Kubernetes, GitHub Actions, and Terraform covers it

How deep it goes
Explains Compose mappings and cleanup behavior, stopping short of broader volume administration.
Examples
Uses source-code and npm-cache mounts in a history microservice Compose service.
What it assumes
Assumes Docker Compose development setup and familiarity with containers, host paths, Dockerfiles, npm, and VS Code.
What you can do after
Configure Docker Compose mounts for live reload, cache retention, and clean volume-backed restarts.
How it connects
Connects Docker Compose, containers, Dockerfile-dev, live reload, npm cache, and microservices.

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 Bootstrapping Microservices, Second Edition: With Docker, Kubernetes, GitHub Actions, and Terraform. The section links point to the parts of the book it draws on.