Create a Docker volume using the docker volume create command

Discover how to create a Docker volume with the docker volume create [volume_name] command. This quick guide explains data persistence for containers, how volumes are mounted, and practical tips for naming and managing volumes in the Docker CLI for reliable stateful apps. It helps teams manage state.

Docker Volumes: The Simple Path to Data That Outlives containers

If you’ve ever wrestled with data disappearing the moment a container stops, you’re not alone. Containers are brilliant at being fast and disposable, but that can be a headache when you need to keep files, databases, or logs around. That’s where Docker volumes come in. They’re Docker’s built-in way to store data beyond the life of any single container, so your apps can start, stop, and move without losing what matters.

Here’s the core idea in plain language: a volume is a named storage area managed by Docker. You attach that storage to a container, point it at a path inside the container, and Docker handles the rest. The data lives on the host machine (or in whatever storage driver Docker is configured to use), and the container can read or write to it as needed. Simple. Powerful. Reliable.

The exact command you’ll use to create that storage is clean and direct: docker volume create [volume_name]. That’s the thing you type to set up a new, named volume that you can reuse across containers. For example, you could run:

  • docker volume create my_data

After that, you’ll reference that volume when you run a container, so data goes to “my_data” instead of vanishing with the container itself. There are a couple of common ways to wire the volume to the container, and a quick note on what each one means.

Two quick ways to attach a volume

  • The classic binding style:

  • docker run -d --name app -v my_data:/path/in/container your-image

  • Here, my_data is the volume name, and /path/in/container is where the app expects to read and write data inside the container.

  • The newer, explicit style using a mount:

  • docker run -d --name app --mount source=my_data,target=/path/in/container your-image

  • The --mount syntax is a bit more verbose, but it’s clearer at a glance what’s being mounted and where.

In both cases, the Docker engine ensures that the data in that path persists even if you stop or remove the container. If you fire up a new container and attach the same volume, it’ll see the same data. That’s the core value: persistence without fragile, container-scoped file systems.

A little tour of the basics

  • Where is the data stored on your machine? By default, Docker keeps volumes in a location under the host’s filesystem (often something like /var/lib/docker/volumes for the local driver). The exact path can depend on your OS and how Docker is configured, but the behavior remains the same: volumes are separate from containers and survive container lifecycle events.

  • How do you manage volumes? There are a few handy commands:

  • docker volume ls — lists all volumes Docker knows about.

  • docker volume inspect [volume_name] — shows details, like where the data lives and which containers are using it.

  • docker volume rm [volume_name] — removes a volume when you’re sure it’s no longer needed.

  • docker volume prune — clears out unused volumes to reclaim space (use with caution).

  • When should you use volumes? Think databases, caches, logs, and any data you don’t want to lose if a container restarts. If the data is truly ephemeral, a temporary file inside a container might be fine, but for anything that needs durability, a volume is the better choice.

A quick real-world scenario

Imagine you’re running a PostgreSQL container for a small project. You don’t want the database files to vanish if the container is restarted or recreated. Create a volume named pgdata, then start PostgreSQL with that volume attached:

  • docker volume create pgdata

  • docker run -d --name mydb -e POSTGRES_PASSWORD=secret -v pgdata:/var/lib/postgresql/data postgres

Now, even if you spin up a new container using the same image, your data is still intact in pgdata. That’s the kind of resilience you want in a real project, and it’s something Docker volumes make almost effortless.

Volumes vs bind mounts: a quick comparison

You’ll hear about both volumes and bind mounts when you work with Docker. They both give containers access to external storage, but they behave a bit differently.

  • Volumes (the topic we’re focused on) are Docker-managed storage. They’re portable, easy to back up, and designed to work consistently across environments. You don’t have to worry about the exact path on the host, and Docker handles the grit of storage for you.

  • Bind mounts map a path on the host into the container. They’re great when you need to work directly with files you’re editing on your machine (think source code projects). But they’re more sensitive to host changes and can be less portable across different hosts or cloud environments.

In practice, for data you want to persist and share across containers, volumes are usually the smoother choice. They’re predictable, which is a big win in any real-world workflow, whether you’re deploying locally, on a private server, or in the cloud.

A few practical tips to keep things sane

  • Name thoughtfully. A clear, consistent naming convention helps you remember what each volume holds. “dbdata,” “logs,” or “cache” is usually enough to convey intent.

  • Don’t tie volumes to a single container. The beauty of a named volume is that any compatible container can mount it and reuse the data. That makes upgrades and replacement much less painful.

  • Backup like a pro. Since volumes live on the host, you can back them up with your favorite filesystem backup tools, or snapshot strategies your environment uses. If you’re in a team, document which volumes store what data—team members will thank you later.

  • Think about drivers. The local driver (the default) is simple and solid for most cases. If you’re running Docker on a bigger scale or in the cloud, you might explore volume drivers that integrate with cloud storage or networked file systems. It’s worth knowing what options exist, because the right driver can simplify disaster recovery and portability.

A little context for the DCA landscape

For professionals navigating the Docker Certified Associate terrain, mastering volumes is a foundational move. It touches on core themes like container lifecycle, data persistence, and operations hygiene. When you pair a solid understanding of volumes with knowledge about container orchestration, logging, and backups, you build a practical toolkit that translates to real projects—whether you’re deploying a microservice stack or a monolith with a database in the mix. The moment you attach a volume to your container, you’re stepping into the stateful side of Docker, and that’s where a lot of modern deployments do their best work.

A friendly nudge to nudge you forward

If you haven’t tried a quick hands-on with volumes lately, consider this little thought experiment: pull up a small database image, create a volume, attach it, and run a container. Then spin up a second container that uses the same volume for data. Stop and remove the first container and watch how the second one picks right back up with the same data. It’s a tiny demo, but it’s a powerful reminder of why volumes exist in the first place.

Closing thoughts: data, durability, and Docker’s quiet power

In the end, the command docker volume create [volume_name] is more than just a line of syntax. It’s a doorway to making your containers less fragile and your data more trustworthy. Data doesn’t have to vanish the moment a container ends. With volumes, you give your apps a steady home for the things they produce and rely on.

If you’re exploring Docker in a broader sense, this idea fits neatly with other Docker topics you’re likely to encounter—networking, images, and the right balance between stateless and stateful designs. The more you practice connecting these pieces, the more natural your workflows will feel. And yes, every once in a while you’ll discover a small nuance—like a driver option or a handy inspection flag—that makes a real difference in day-to-day operations.

So next time you start thinking about data persistence in Docker, remember the simple, sturdy choice: docker volume create [volume_name], attached to the path that matters inside your container. It’s a small step, but it pays off in clarity, reliability, and a smoother development-to-deploy journey.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy