Skip to main content

What is a volume in Docker?

A volume is a Docker mechanism for persistently storing data that must not disappear when a container is removed or recreated.

Why it's needed:

1. Data lives outside the container The container can be deleted, rebuilt, or updated, while the database, files, and logs are preserved.

2. Convenient work with databases Postgres, MySQL, Redis, and other services use a volume so their data doesn't disappear on restart.

3. Separation of data and application The container handles logic, the volume handles storage.

Examples in Compose:

yaml
services: db: image: postgres volumes: - pgdata:/var/lib/postgresql/data volumes: pgdata:

Summary: a volume is a persistent, reliable storage that doesn't depend on the container's lifecycle.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.