Objective: Understand how Docker stores application data permanently, even when containers are removed.
1. Why do we need Docker Volumes?
Consider this scenario:
You have an ASP.NET Core application running inside a Docker container. Your application generates uploaded files.
What happens if you remove the container?
Normally, files stored only inside the container are lost when the container is removed.
Docker Volumes solve this problem. They allow data to persist independently of the container lifecycle.
2. Create a Docker Volume
Execute:
docker volume create mydata
This creates a Docker-managed volume named mydata.
3. List Docker Volumes
docker volume ls
You should see:
DRIVER VOLUME NAME
local mydata
4. Inspect the Volume
docker volume inspect mydata
This displays:
-
Volume name
-
Mount point
-
Storage driver
-
Creation details
5. Run Nginx with a Docker Volume
Execute:
docker run -d -p 8080:80 --name nginx-volume -v mydata:/usr/share/nginx/html nginx
Explanation:
|
Parameter |
Description |
|---|---|
|
|
Mount a volume |
|
|
Docker volume name |
|
|
Nginx container directory |
Now the volume is connected to Nginx's website directory.
6. Verify Volume Mounting
Execute:
docker inspect nginx-volume
Find the Mounts section.
You should see:
-
Type: volume
-
Name: mydata
-
Destination: /usr/share/nginx/html
7. Remove the Container
docker stop nginx-volume
docker rm nginx-volume
8. Verify the Volume Still Exists
docker volume ls
You should still see mydata.
This is the important concept: Removing a container does not automatically remove its Docker volume.
Docker Volumes – Explained Clearly with Practical Examples
Let's understand Docker Volumes using a simple real-world example and then implement it step by step.
1. First, understand the problem
Imagine you have created a simple ASP.NET Core Web API application and deployed it inside a Docker container.
Your application creates a file called message.txt.
Initially, the file contains:
Hello from Docker Container!
Now, let's say you stop and delete the container.
What happens to message.txt?
The file will also be deleted because it was stored inside the container's writable filesystem.
This is the main problem Docker Volumes solve.
2. What is a Docker Volume?
A Docker Volume is a storage mechanism that allows us to store data outside the container's lifecycle.
Even if you stop, delete, or recreate a container, the data stored in the volume remains available.
Think of it like this:
-
Container = Temporary computer.
-
Volume = Permanent hard disk.
-
Application = Program running inside the computer.
Without Docker Volume
Docker Host
|
+---- Container
|
+---- Application
|
+---- message.txt
Delete Container
|
V
Application deleted
message.txt deleted
With Docker Volume
Docker Host
|
+---- Container
| |
| +---- Application
| |
| +---- Reads/Writes data
| |
+--------------------+
|
+---- Docker Volume
|
+---- message.txt
Delete Container
|
V
Application deleted
Docker Volume remains
message.txt remains
Important: The container and volume have separate lifecycles.
3. Real-world example
Consider an e-commerce application.
Your application stores:
-
Customer information
-
Order information
-
Product information
-
Uploaded product images
If you store these files directly inside a container, deleting the container can result in data loss.
Instead, you can use Docker Volumes.
E-Commerce Application
|
V
Docker Container
|
+---- Application
|
+---- Database
|
V
Docker Volume
|
+---- Customer Data
+---- Order Data
+---- Product Data
Even if you recreate the container, your database data remains in the volume.
4. Practical Example – Let's implement it
We will create a Docker Volume and store a message.txt file inside it.
Step 1: Create a Docker Volume
Open CMD or PowerShell:
docker volume create mydata
This creates a volume named mydata.
Step 2: Check available volumes
docker volume ls
Output:
DRIVER VOLUME NAME
local mydata
Step 3: Create a container and connect the volume
docker run -it --name volume-demo -v mydata:/app/data ubuntu bash
What does this command mean?
|
Command |
Purpose |
|---|---|
|
|
Create and start a container |
|
|
Interactive terminal |
|
|
Container name |
|
|
Mount a volume |
|
|
Docker Volume name |
|
|
Container directory |
|
|
Docker image |
|
|
Open terminal |
Step 4: Create a file inside the container
You are now inside the Ubuntu container.
Execute:
echo "Hello from Docker Volume!" > /app/data/message.txt
Read the file:
cat /app/data/message.txt
Output:
Hello from Docker Volume!
Step 5: Exit the container
exit
Step 6: Delete the container
Execute this from your Windows CMD:
docker rm volume-demo
We have deleted the container. But what about our file?
Step 7: Create another container using the same volume
docker run -it --name volume-demo2 -v mydata:/app/data ubuntu bash
Now execute:
cat /app/data/message.txt
Hello from Docker Volume!
Surprise! The file is still available even though we deleted the previous container.
Why? Because the file was stored in the Docker Volume, not in the container's own filesystem.
5. Where does Docker store volumes?
On Windows with Docker Desktop, Docker manages volume storage inside its Linux environment (typically its WSL2 virtual disk).
You can inspect a volume using:
docker volume inspect mydata
Docker displays information such as:
-
Volume name
-
Driver
-
Mountpoint
-
Creation date
You generally don't need to manually access this internal storage location.
6. Types of Docker Storage
Docker provides three important storage mechanisms.
1. Docker Volumes
Docker manages the storage location.
Example: Database files, persistent application data.
docker run -v mydata:/app/data ubuntu
2. Bind Mounts
You specify an existing directory on your computer and connect it to a container directory.
Example: Share your source code with a container.
docker run -v "D:\MyApp:/app" ubuntu
3. tmpfs Mounts
Stores data in memory instead of persistent disk storage.
Example: Temporary sensitive data.
docker run --tmpfs /app/temp ubuntu
Data does not survive container termination.
7. Docker Volume vs Bind Mount
|
Feature |
Docker Volume |
Bind Mount |
|---|---|---|
|
Storage location |
Managed by Docker |
Specified by user |
|
Persistent |
Yes |
Yes |
|
Easy to manage |
Yes |
Depends on user |
|
Host directory required |
No |
Yes |
|
Common usage |
Database storage |
Source code sharing |
8. Important Docker Volume Commands
|
Command |
Purpose |
|---|---|
|
|
Create volume |
|
|
List volumes |
|
|
Inspect volume |
|
|
Delete volume |
|
|
Remove unused volumes |
Important: Removing a volume permanently deletes its stored data. Make sure you no longer need the data before removing it.
9. Interview Questions
-
What is a Docker Volume?
-
Why do we need Docker Volumes?
-
What happens to volume data when a container is deleted?
-
What is the difference between Docker Volume and Bind Mount?
-
Where does Docker store volume data?
-
Can multiple containers share the same Docker Volume?
-
What is the difference between named volumes and anonymous volumes?
-
How do you create, inspect and delete Docker Volumes?
-
Can we use Docker Volumes with SQL Server or MySQL containers?
-
What is tmpfs mount in Docker?
10. Check your understanding
Test your understanding – 3 questions
Q1. You created a file inside a Docker container using a Docker Volume. Then you deleted the container. What happens to the file?
Answer B: The volume has an independent lifecycle, so its data remains even after the container is deleted.
Q2. Which Docker storage mechanism is managed by Docker itself?
Answer C: Docker manages the volume's storage location and lifecycle.
Q3. You want to share your Windows source-code folder with a container. Which storage mechanism should you use?
Answer B: Bind Mount connects a specific host directory to a directory inside the container.