0Pricing
Docker & Kubernetes for Developers · Lesson

Persisting Data with Docker Volumes

Learn how Docker volumes and bind mounts keep your data alive beyond a container's lifetime and let you share files between host and container.

Persisting Data with Docker Volumes is a free Docker & Kubernetes for Developers lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Docker & Kubernetes for Developers learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Containers Are Ephemeral

When a container is removed, everything written inside its writable layer is gone. For databases, uploads, or logs you need storage that survives the container.

Two Storage Options

Docker offers two main mechanisms:

  • Volumes: managed by Docker, stored in a dedicated area
  • Bind mounts: map a specific host folder into the container

Volumes are the recommended default.

Creating a Volume

Create a named volume with docker volume create. Docker manages where it physically lives.

docker volume create app_data

Mounting a Volume

Attach the volume to a container path with -v. Anything written to that path persists in the volume.

docker run -d -v app_data:/var/lib/data my_app

Bind Mounts for Development

A bind mount maps a host directory into the container, great for live-editing source code during development.

docker run -v /home/me/project:/app node:20

Listing and Inspecting Volumes

See all volumes with docker volume ls, and view details, including the mount point, with docker volume inspect.

docker volume ls
docker volume inspect app_data

Volumes in Compose

In docker-compose.yml you declare volumes once and reference them in services. Use single quotes and indentation, never tabs.

services:
  db:
    image: postgres:16
    volumes:
      - db_data:/var/lib/postgresql/data
volumes:
  db_data:

Sharing a Volume Between Containers

Multiple containers can mount the same volume to share files, for example an app writing files and a backup container reading them.

docker run -v shared:/data writer
docker run -v shared:/data reader

Read-Only Mounts

Append :ro to mount a volume read-only when a container should never modify the data, adding a safety guard.

docker run -v config:/etc/app:ro my_app

Backing Up a Volume

Back up a volume by running a throwaway container that tars its contents to the host.

docker run --rm -v app_data:/data -v $(pwd):/backup \
  alpine tar czf /backup/data.tar.gz /data

Cleaning Up Unused Volumes

Orphaned volumes waste disk. Remove a named one with docker volume rm, or clear all unused volumes with docker volume prune, carefully.

docker volume prune

Quick Check

Test your understanding of Docker storage.

Recap

You learned to persist data in Docker:

  • Containers are ephemeral; use volumes or bind mounts for durable data
  • Mount with -v name:/path; use bind mounts for live dev
  • Declare volumes in Compose and share them between containers
  • Back up, mount read-only, and prune unused volumes

Volumes let your databases and files outlive any single container.

Frequently asked questions

Is the “Persisting Data with Docker Volumes” lesson free?

Yes — the full text of “Persisting Data with Docker Volumes” is free to read here on the web, and the Docker & Kubernetes for Developers course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Docker & Kubernetes for Developers course, upgrade to CoddyKit PRO.

What will I learn in “Persisting Data with Docker Volumes”?

Learn how Docker volumes and bind mounts keep your data alive beyond a container's lifetime and let you share files between host and container. You practise Docker & Kubernetes for Developers with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Docker & Kubernetes for Developers?

No prior experience is required. Docker & Kubernetes for Developers on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Persisting Data with Docker Volumes” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Docker & Kubernetes for Developers lesson?

Yes. Every Docker & Kubernetes for Developers lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Crafting Dockerfiles for Images
  2. Building Custom Docker Images
  3. Multi-Container Apps with Compose
  4. Persisting Data with Docker Volumes
← Back to Docker & Kubernetes for Developers