Course contentsShow
← Cloud and Infrastructure
Lesson 1012 of 2,98920. Docker in DepthFree lesson

VOLUME: Mount Points

Learn VOLUME instruction for defining mount points, anonymous volumes, and data persistence patterns.

VOLUME: Mount Points

What you'll learn: How to declare volume mount points in Dockerfiles for data that should outlive containers.

The Core Problem

By default, all files written inside a container are lost when that container stops. If your application generates logs, stores user uploads, or maintains a database, you need a way to preserve that data across container restarts and updates.

What VOLUME Does

The VOLUME instruction tells Docker: "This directory contains important data that should live outside the container's filesystem." It creates a mount point — a designated location where external storage can attach.

FROM ubuntu:22.04
WORKDIR /app

# Mark /app/data as a volume
VOLUME /app/data

COPY myapp .
CMD ["./myapp"]

When you run this image without specifying a volume, Docker automatically creates an anonymous volume — a randomly-named storage area on the host that persists even after the container stops.

Why This Matters

Think of a container like a hotel room. Everything inside gets cleaned out when you check out. A VOLUME is like a safety deposit box — it exists separately from the room, survives checkout, and can even be transferred to your next room (container).

Common uses:

  • Database files (/var/lib/mysql)
  • Application logs (/var/log)
  • User-generated content (/uploads)

Anonymous vs Named Volumes

The VOLUME instruction creates anonymous volumes. Later, at runtime, you can map these to named volumes for easier management, but the Dockerfile's job is simply to declare "this path contains persistent data."

Key Takeaway: Use VOLUME to declare mount points for data that must survive container restarts — Docker will ensure that data persists in external storage rather than disappearing with the container.