Docker Tutorial 0/48 lessons ~6 min read Lesson 28

    Volumes Overview

    Container filesystems are ephemeral — delete the container and data inside dies with it.

    Course progress0%
    Focus
    15 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    Introduction

    Container filesystems are ephemeral — delete the container and data inside dies with it. Volumes persist data outside the container's writable layer.

    Purpose of this lesson

    This lesson teaches Volumes Overview as an engineering decision: what problem it solves, when to use it, how to implement it safely, and what signals tell you it is failing.

    Understanding the topic

    Use this when containers must communicate, expose traffic, or persist state. Most real Docker incidents come from a wrong port, missing DNS name, unsafe bind mount, or data stored in the container writable layer.

    Core concepts to understand:

    • Named volumes — managed by Docker (/var/lib/docker/volumes/…).
    • Bind mounts — direct host path (great for dev).
    • tmpfs — in-memory, never on disk.
    • Volumes survive container removal; explicit docker volume rm needed.

    Visual explanation

    Architecture or command flow to keep in mind:

    bash
    # Named volume (best for prod data)
    docker volume create pgdata
    docker run -d -v pgdata:/var/lib/postgresql/data postgres
    # Bind mount (best for dev — live code reload)
    docker run -v $(pwd):/app node:20 npm run dev

    Step-by-step explanation

    1. Run the command or manifest exactly once on a clean Docker host and read the output carefully.
    2. Inspect the object Docker created: image, container, network, volume, port mapping, process, or registry tag.
    3. Break one realistic assumption such as a missing port, bad tag, stopped daemon, wrong network, or deleted volume.
    4. Use docker ps, logs, inspect, stats, and system df to locate the failure.
    5. Write the final command or configuration into a repeatable script, Compose file, or CI job.

    Informative example

    Use the example below as a working baseline, then verify the runtime behavior instead of assuming the command or file is correct.

    bash
    # Named volume (best for prod data)
    docker volume create pgdata
    docker run -d -v pgdata:/var/lib/postgresql/data postgres
    # Bind mount (best for dev — live code reload)
    docker run -v $(pwd):/app node:20 npm run dev

    A production-minded check usually includes docker ps, docker logs, docker inspect, and one validation from outside the container such as curl, a database connection, or a registry pull.

    bash
    # Verification loop for Volumes Overview
    docker ps -a
    docker logs --tail=100 <container-name>
    docker inspect <container-or-image-name>
    docker system df

    Real-world use

    Production DBs always use named volumes. Local development always uses bind mounts so editor edits show up instantly in the container.

    Enterprise use cases

    In a mature engineering organization, Volumes Overview is documented as a repeatable pattern with approved base images, ownership labels, CI checks, security expectations, rollback notes, and troubleshooting commands. The difference between a tutorial and production practice is that every container decision must be observable, reviewable, and reversible.

    Best practices

    • Named volumes for state (DB, queue data).
    • Bind mounts for code in development.
    • Back up named volumes regularly (docker run --rm -v vol:/data -v $(pwd):/out alpine tar czf /out/backup.tgz /data).

    Common mistakes

    • Bind-mounting over a populated directory hides the original files.

    Debugging tips

    • Use docker inspect to verify the exact mount source, destination, mode, and propagation.
    • Check UID/GID permissions when the app can read locally but cannot write inside the container.
    • Before deleting containers or running prune commands, confirm whether important data lives in a named volume.

    Optimization strategies

    • Prefer explicit names, labels, tags, and networks so cleanup and debugging stay predictable.
    • Pin versions for repeatability, then update intentionally through a scheduled base-image refresh.
    • Use docker system df and targeted prune commands to control local and CI disk growth.

    Advanced interview questions

    Interview Prep

    Practice concise answers, then expand each card for the explanation.

    3 questions
    1QuestionWhy do containers need volumes?+

    Answer

    A strong answer for Volumes Overview should define the concept, explain the Docker component involved, give one real use case, and name at least one failure mode plus the command you would use to investigate it.
    2QuestionDifference between bind mount and named volume?+

    Answer

    A strong answer for Volumes Overview should define the concept, explain the Docker component involved, give one real use case, and name at least one failure mode plus the command you would use to investigate it.
    3QuestionWhat survives container removal?+

    Answer

    A strong answer for Volumes Overview should define the concept, explain the Docker component involved, give one real use case, and name at least one failure mode plus the command you would use to investigate it.

    Hands-on exercise

    Create a small lab for Volumes Overview: run the example, inspect the created Docker object, intentionally introduce one mistake, and record the command that reveals the failure. The goal is not just to make the happy path work; it is to build operational reflexes.

    bash
    # Hands-on lab scaffold
    mkdir -p docker-volumes-overview-lab
    cd docker-volumes-overview-lab
    # Add the Dockerfile, compose.yml, or command from this lesson.
    # Then run one happy-path test and one broken-path test.
    docker version
    docker info
    docker system df

    Summary

    Volumes Overview matters because Docker is not only a packaging tool; it is a runtime, build, networking, storage, and delivery workflow. Treat each lesson as a production habit: make it repeatable, inspectable, secure, and easy to debug.

    Ready to mark this lesson complete?Track your journey across the entire course.