Docker Volume Management
Docker volumes are the preferred mechanism for persisting data generated by and used by Docker containers. Volumes are completely managed by Docker and have several advantages over bind mounts.
Key Benefits:
- Volumes are easier to back up or migrate than bind mounts
- You can manage volumes using Docker CLI commands or the Docker API
- Volumes work on both Linux and Windows containers
- Volumes can be more safely shared among multiple containers
- Volume drivers let you store volumes on remote hosts or cloud providers
Working with Docker Volumes
Follow these steps to create, use, and manage Docker volumes.
Create a Volume
Creates a new Docker volume named "myvol". Volumes are the preferred way to persist data in Docker containers.
Explanation:
- docker volume create → Command to create a new volume
- myvol → Name of the volume to create
List Volumes
Lists all Docker volumes on your system, showing their names and drivers.
Usage:
Use this command to see all available volumes and verify that your volume was created successfully.
Run Container with Volume
Starts a new Nginx container named "voltest" that uses the "myvol" volume mounted at the "/app" directory inside the container.
Explanation:
- docker run → Start a new container
- -d → Detached mode (runs in background)
- --name voltest → Names the container "voltest"
- -v myvol:/app → Mounts volume "myvol" to "/app" directory in container
- nginx:latest → The image to use
Connect to Container
Opens an interactive bash shell inside the running "voltest" container.
Explanation:
- docker exec → Execute a command in a running container
- -it → Interactive mode with pseudo-TTY
- voltest → Container name to connect to
- bash → Command to run inside container
Install Nano and Create File
First, update package lists and install Nano:
Then navigate to the app folder and create a file:
Updates package lists, installs Nano text editor, navigates to the /app directory (where our volume is mounted), and creates a text file using Nano.
Nano Commands:
- Type some text in the editor
- CTRL-O → Write out (save) the file
- CTRL-X → Exit Nano
- exit → Exit the container shell
Stop and Remove Container
First, stop the container:
Then remove the container:
Stops the "voltest" container and then removes it. The volume persists independently of the container.
Note:
Even though we're removing the container, the volume "myvol" and the file we created ("test.txt") still exist because volumes are independent of container lifecycle.
Verify Data Persistence
Run a new container with the same volume:
Check if the file still exists:
Creates a new container using the same volume and checks if the file we created earlier still exists with its content intact.
Result:
The "test.txt" file should still exist with the content you previously added, demonstrating that Docker volumes persist data independently of containers.
Cleanup
Stop and remove the container:
Remove the volume:
Stops and removes the container, then removes the volume to clean up resources.
Note:
You cannot remove a volume that is currently in use by a container. Make sure to stop and remove any containers using the volume first.
Summary
This exercise demonstrates how Docker volumes work and how they provide persistent storage that survives container lifecycle changes. Key takeaways:
- Volumes are created independently of containers
- Data stored in volumes persists even when containers are removed
- Multiple containers can use the same volume simultaneously
- Volumes need to be explicitly removed when no longer needed
- This makes volumes ideal for databases, file storage, and other persistent data needs