
Docker volumes are a persistent storage mechanism that decouples your data from the container lifecycle. Volumes let containers retain data across restarts and removals, and allow multiple containers to share a common data source. Unlike bind mounts, Docker manages volumes internally and stores them under /var/lib/docker/volumes/ on Linux systems.
This article shows how to create and inspect Docker volumes, interact with their contents, and use them in Docker Compose workflows. You'll also learn how to back up and restore volume data, and how to reference volumes using the VOLUME instruction in a Dockerfile.
The following Docker commands demonstrate how to manage persistent data using volumes:
Use Docker volumes instead of bind mounts in the following situations:
Bind mounts are more suitable for development environments where code changes on the host need to reflect immediately in the container. For production environments, Docker volumes are the preferred choice due to their portability, safety, and lifecycle integration.
Docker volumes allow you to persist container data independently of the container lifecycle. This section demonstrates how to manually create a volume, attach it to a container, inspect it, and remove both the container and the volume.
Create a Docker volume.
This command creates a Docker-managed volume named web_content. Docker stores it internally and tracks its metadata.
volume create: Initializes a new volume.web_content: The name assigned to the volume.Output:
Run a container and mount the volume.
This launches a container with the httpd image and mounts the named volume at the web server's root, making it available for data persistence.
-d: Runs the container in detached mode.-v web_content:/usr/local/apache2/htdocs: Mounts the web_content volume to Apache’s web root.--name apache_with_volume: Assigns a custom name to the container.httpd: Uses the official Apache HTTP server image.Output:
List all volumes.
This command verifies that the volume was created and is currently available.
volume ls: Lists all volumes managed by Docker.Output:
Inspect the volume.
Use this command to confirm the volume’s internal mount path and attributes, useful for debugging and manual access.
volume inspect: Displays low-level metadata about a volume.web_content: Target volume name.Output:
List all containers.
This confirms that the container is active and correctly linked to the volume.
container ls -a: Shows all containers, running or stopped.Output:
Check volume usage by container.
This shows where the volume is mounted inside the container, validating its usage path.
inspect: Retrieves metadata about the container.--format={{.Mounts}}: Filters output to show mount points.Output:
Stop the container.
This halts the container without removing it, preserving the volume and data.
container stop: Gracefully stops a running container.Output:
Remove the container.
This removes the container while leaving the named volume intact for reuse.
container rm: Deletes the stopped container.Output:
Remove the volume.
This permanently removes the volume and its contents.
volume rm: Deletes the specified volume.Output:
Removing a volume deletes all stored data. Ensure backups exist before running this command in production environments.
While Dockerfiles can't create named volumes, they can define mount points expected at runtime using the VOLUME instruction. Docker automatically creates an anonymous volume for such paths unless you explicitly mount a named one when running the container.
Navigate to the user's home directory and create a docker directory.
Navigate into the new directory.
Create a Dockerfile.
Add the following content to the Dockerfile:
Save and close the file.
This Dockerfile builds an Apache-based image that treats /usr/local/apache2/htdocs as a volume path.
VOLUME ["/usr/local/apache2/htdocs"]: Declares a runtime mount point for persistent data.Build the image.
This builds the Dockerfile and creates a new image named apache_with_volume.
-t apache_with_volume: Tags the resulting image with a readable name.Output:
Run the container.
-d: Runs the container in detached mode.--rm: Automatically removes the container when it exits. Docker automatically creates an anonymous volume and mounts it at /usr/local/apache2/htdocs, as defined in the Dockerfile.
To use a named volume instead of an anonymous one, pass the -v flag explicitly:
This allows you to control the volume lifecycle while still benefiting from Dockerfile-defined mount points.
Docker does not let you directly mount volumes on the host system, but you can access and modify volume contents from within a running container.
Start a container with the volume mounted.
This launches the container and mounts the named volume to serve persistent content.
-d: Runs the container in detached mode.--name apache_with_volume: Assigns a custom name to the container.-v web_content:/usr/local/apache2/htdocs: Mounts the volume to Apache’s default web root.httpd: Uses the official Apache image.Output:
Create an HTML file on your host machine.
This creates a simple HTML file for demonstration.
Copy the file into the container.
This places your HTML file into the volume's mount point within the container.
docker cp: Copies files between host and container.index.html: Source file on the host.apache_with_volume:/usr/local/apache2/htdocs/: Destination path inside the container.Output:
View file contents inside the container.
This verifies that the file was successfully copied into the container.
docker exec: Runs a command inside a running container.cat /usr/local/apache2/htdocs/index.html: Prints the contents of the copied file.Output:
Back up the volume from inside the container.
This creates a backup archive of the volume’s contents inside the container.
tar czf: Creates a compressed .tar.gz archive.-C /usr/local/apache2/htdocs .: Archives the current directory (volume contents) into httpd_backup.tar.gz.Copy the backup file from the container to the host.
This allows you to save a local backup of the volume.
docker cp: Transfers files from the container to the host.apache_with_volume:/usr/local/apache2/htdocs/httpd_backup.tar.gz: Source archive inside the container..: Destination path on the host (current directory).Output:
Restore the volume by copying the archive back and extracting it.
These steps restore the previous data state into the mounted volume.
Docker Compose supports named volumes natively. These volumes are automatically created and managed alongside the service lifecycle.
Create a docker-compose directory in your home path.
Navigate to the new directory.
Create a docker-compose.yml manifest.
Add the following YAML content to the file:
This configuration tells Docker Compose to create a volume named web_content and mount it to the container at /usr/local/apache2/htdocs.
image: httpd: Uses the official Apache image.volumes:: Mounts the named volume web_content to Apache's web root.volumes: section at the root defines the named volume.Start the service.
This command builds and runs the container while automatically creating the declared volume and network.
up -d: Starts the services defined in the Compose file in detached mode.Output:
Inspect the volume created by Docker Compose.
Replace docker-compose_web_content with the name of your Docker Compose volume. This confirms that the named volume declared in the Compose file was created and is mounted as expected.
volume inspect: Displays detailed metadata about the named volume.Output:
Shut down the service and remove the volume.
This removes all Docker resources tied to the Compose application, including the named volume.
down: Stops and removes containers, networks, and volumes.-v: Ensures that named volumes declared in the Compose file are also deleted.Output:
In this article, you explored how to create and manage Docker volumes using the CLI, Dockerfiles, and Docker Compose. You learned how to mount volumes to containers, inspect and remove them, and back up or restore volume data using practical workflows. Docker volumes are essential for separating application logic from persistent data. They improve data durability, simplify container management, and support scalable, production-ready deployments.
0 Comments
Be the first to comment and share your perspective with the community.