03 / 06

How do you create a Docker volume?

Difficulty: 5/10
named volumes, bind mounts, volume drivers

You can create a Docker volume using the docker volume create command, or automatically by mounting a volume when running a container with the -v or --mount flag.

Docker provides multiple ways to create volumes depending on your workflow and whether you need to manage the volume separately from containers. You can explicitly create a named volume before using it, or let Docker create a volume automatically when you first reference it in a container run command. Both approaches result in volumes stored in Docker's managed storage area (/var/lib/docker/volumes/ on Linux).

Method 1: Create a named volume explicitly
Method 2: Create a volume automatically with a container
Volume Creation Methods Explained
  1. 1

    Named volumes (docker volume create) give you explicit control over volume names and options, making them easier to reference later in multiple containers .

  2. 2

    Anonymous volumes (just -v /container/path) create volumes with random names, useful for temporary data where you don't need to reference the volume by name .

  3. 3

    Automatic named volumes (mount a non-existent volume name) create the volume if it doesn't exist when you first run a container, combining convenience with identifiable names .

After creation, volumes appear in docker volume ls and can be managed independently of containers. They persist even after all containers using them are removed, until you explicitly delete them with docker volume rm or docker volume prune. You can also back up, restore, and migrate volumes using temporary containers that mount both the volume and a host directory for the backup archive.

Scenario Questions

0-2 years experience

  1. 1We need to persist a MySQL data directory in a Docker container. How would you create and attach a Docker volume for that container?
  2. 2If you run `docker run -v /data myimage` without specifying a volume name, what does Docker do, and how would you explicitly create a named volume first?

2-5 years experience

  1. 1Your CI pipeline builds a container that writes logs to `/var/log/app`. After a few runs, you notice logs are missing. Walk me through how you'd create a Docker volume to retain those logs across container restarts, and what options you might consider.
  2. 2A teammate added a `docker-compose.yml` that defines a volume but the service fails to start, complaining about permission errors. How would you debug the volume creation and what flags might you use to set correct permissions?

5-8 years experience

  1. 1We're deploying a stateful microservice on a Swarm cluster that needs high‑availability storage. Explain how you would design the volume strategy, including which Docker volume driver you’d pick, how you’d create the volumes, and how you’d handle node failures.
  2. 2Our legacy application uses host‑bind mounts for data, but we want to migrate to named volumes for better portability. Describe the steps and any risks you’d watch for during the migration.

8+ years experience

  1. 1At a large organization we have multiple teams running workloads on Kubernetes with Docker as the runtime. How would you define a cross‑team policy for volume provisioning, including driver selection, naming conventions, and lifecycle management, to ensure security and compliance?
  2. 2We need to move from local Docker volumes to a distributed storage solution (e.g., CSI with Ceph) without downtime. Outline the architectural plan, migration path, and how you’d coordinate with other teams to validate data integrity.

Follow-up Questions

  • What’s the practical difference between a bind mount and a named volume?
  • Where on the host does Docker store the data for a named volume by default?
  • How would you choose a volume driver when you need storage that spans multiple nodes?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.