liveBook · Manning

Docker volume

As explained in Rust Servers, Services, and Apps

A Docker volume is Docker-managed persistent storage whose contents exist independently of a container's lifecycle.

A Docker volume is storage managed by Docker for data used by containers. Its contents exist outside a container’s lifecycle, so they can persist between container runs. Volumes are also described as easy to back up, and volume drivers can store data on remote hosts or cloud providers. section 12.3.1

Persisting PostgreSQL data

Create a named volume such as tutor-data, then mount it at /var/lib/postgresql/data, PostgreSQL’s default database directory. The Compose configuration maps the volume to that path in the database container, keeping the storage separate from the container’s lifecycle. section 12.3.1 section 12.5

version: '3'
services:
  db:                                         #1
    container_name: tutor-postgres
    restart: always
    image: postgres:latest
    environment:
     - POSTGRES_USER=postgres
     - POSTGRES_PASSWORD=postgres
     - POSTGRES_DB=ezytutors
    volumes:
     - tutor-data:/var/lib/postgresql/data
     - ./c12-data/initdb.sql:/docker-entrypoint-initdb.d/initdb.sql
     - ./c12-data/init-tables.sql:/docker-entrypoint-initdb.d/init-tables.sql
    ports:
     - 5432:5432
    networks:
     - tutor-network
  api:                                       #2
    restart: on-failure
    container_name: tutor-webservice
    build:                                   #3
      context: ./
      dockerfile: Dockerfile-tutor-webservice
      network: host
    environment:                             #4
     - DATABASE_URL=${DATABASE_URL}
     - HOST_PORT=${HOST_PORT}
    depends_on:
     - db                                    #5
    ports:
     - ":3000:3000"                          #6
    networks:
     - tutor-network                         #7
volumes:
        tutor-data:
networks:
        tutor-network:

The Compose configuration shows tutor-data mounted in the database service alongside initialization-file mounts. section 12.5

How Rust Servers, Services, and Apps covers it

How deep it goes
Explains Docker-managed persistence, volume creation, and a Compose mount, but not backup or remote-driver procedures.
Examples
Mounts tutor-data at /var/lib/postgresql/data for a PostgreSQL container.
What you can do after
Configure a named volume and mount it as PostgreSQL’s data directory in Docker Compose.
How it connects
Connects Docker volumes with Docker Compose, PostgreSQL, and container lifecycle management.

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 Rust Servers, Services, and Apps. The section links point to the parts of the book it draws on.