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



