A common way to create a Docker volume is to use the docker volume create command and specify the volume name. The steps include: 1. Create a named volume using docker volume create my-volume; 2. Mount the volume to the container through docker run -v my-volume:/path/in/container; 3. Verify the volume using docker volume ls and clean useless volumes with docker volume prune. In addition, anonymous volume or binding mount can be selected. The former automatically generates an ID by Docker, and the latter maps the host directory directly to the container. Note that the volume is only valid locally, and external storage schemes are required across nodes. During testing, you can use the Alpine container to check the volume content.
Creating a Docker volume is straightforward, and it's one of the most common ways to persist data when working with containers. The main idea is to set up a directory that exists outside the container's filesystem so data survives even after the container stops or gets removed.
Basic Command: docker volume create
The simplest way to create a Docker volume is by using the docker volume create
command. You just need to give your volume a name:
docker volume create my-volume
That's it — you now have a named volume called my-volume
. This volume can be mounted into one or more containers later.
You won't see much output unless something goes wrong, but you can verify the volume was created with:
docker volume ls
This lists all volumes currently available on your system.
Mounting a Volume When Running a Container
Creating the volume is only half the job — you'll want to use it in a container. To do that, use the -v
flag when running a container:
docker run -d \ --name my-container \ -v my-volume:/path/in/container \ my-image
Here:
-
-v my-volume:/path/in/container
tells Docker to mount the volumemy-volume
to a specific path inside the container. -
/path/in/container
depends on what your app expects — for example, if you're running a web server, this might be/usr/share/nginx/html
.
Once mounted, any data written to that directory inside the container will be stored in the volume and preserved across container restarts or replacements.
Types of Volumes You Can Use
Docker supports a few types of volumes, and it's helpful to know which one fits your use case:
- Named volumes – like we used earlier (
my-volume
). These are managed by Docker and are great for databases, config files, etc. - Anonymous volumes – when you don't specify a name, Docker gives it a random ID. Useful for temporary storage.
- Bind mounts – instead of letting Docker manage the storage, you map a specific host directory directly into the container (eg,
-v /your/host/dir:/container/path
).
If you're not sure which to pick:
- Use named volumes for most services where persistence matters.
- Use bind mounts when you need direct access to your host's files, like during development.
Some Gotchas and Tips
There are a few small things that often trip people up:
- If you try to mount a volume that doesn't exist when running a container, Docker will automatically create an anonymous volume — not always what you want.
- Volumes are local to the Docker host. If you're using Docker Swarm or Kubernetes, you'll need external storage solutions for multi-node setups.
- Cleaning up unused volumes is easy with:
docker volume prune
Also, if you're testing and want to make sure your volume works, you can spin up a quick Alpine container to inspect its contents:
docker run -it --rm -v my-volume:/test alpine sh ls /test
That should show you what's actually inside the volume.
Basically that's it.
The above is the detailed content of How do you create a Docker volume?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Four ways to exit Docker container: Use Ctrl D in the container terminal Enter exit command in the container terminal Use docker stop <container_name> Command Use docker kill <container_name> command in the host terminal (force exit)

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".

Methods for copying files to external hosts in Docker: Use the docker cp command: Execute docker cp [Options] <Container Path> <Host Path>. Using data volumes: Create a directory on the host, and use the -v parameter to mount the directory into the container when creating the container to achieve bidirectional file synchronization.

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

How to restart the Docker container: get the container ID (docker ps); stop the container (docker stop <container_id>); start the container (docker start <container_id>); verify that the restart is successful (docker ps). Other methods: Docker Compose (docker-compose restart) or Docker API (see Docker documentation).

Create a container in Docker: 1. Pull the image: docker pull [mirror name] 2. Create a container: docker run [Options] [mirror name] [Command] 3. Start the container: docker start [Container name]

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

The methods to view Docker logs include: using the docker logs command, for example: docker logs CONTAINER_NAME Use the docker exec command to run /bin/sh and view the log file, for example: docker exec -it CONTAINER_NAME /bin/sh ; cat /var/log/CONTAINER_NAME.log Use the docker-compose logs command of Docker Compose, for example: docker-compose -f docker-com
