Let's focus only on Docker Volumes Mount Type.
First, understand this distinction:
-
Volume is a Docker storage mechanism.
-
Mount is the process of connecting that storage to a directory inside a container.
For example:
docker run -v mydata:/app/data ubuntu
Here:
-
mydata= Docker Volume. -
/app/data= Mount destination inside the container. -
-v= Mount configuration.
1. What is a Volume Mount?
A Volume Mount connects a Docker-managed volume to a directory inside a container.
The data is stored independently of the container.
Consider this architecture:
Docker Host
|
+--------+--------+
| |
V V
Container Docker Volume
| |
| |
V |
/app/data <-----------+
|
V
message.txt
The container accesses the volume through /app/data.
If you delete the container, the volume remains.
Main purpose: To persist application data independently of the container lifecycle.
2. Types of Docker Volumes Based on Creation
There are two commonly used ways to create Docker Volumes.
A. Named Volume
A Named Volume is a volume that you explicitly create with a meaningful name.
Step 1: Create a volume
docker volume create mydata
Step 2: Verify the volume
docker volume ls
Output:
DRIVER VOLUME NAME
local mydata
Step 3: Attach the volume to a container
docker run -it --name volume-demo -v mydata:/app/data ubuntu bash
Here:
|
Part |
Meaning |
|---|---|
|
|
Volume name |
|
|
Container destination |
|
|
Image |
|
|
Terminal |
Step 4: Create a file inside the volume
Inside the container:
echo "Hello from Named Volume" > /app/data/message.txt
Read it:
cat /app/data/message.txt
Step 5: Exit and delete the container
exit
docker rm volume-demo
Step 6: Create another container using the same volume
docker run -it --name volume-demo2 -v mydata:/app/data ubuntu bash
Read the file:
cat /app/data/message.txt
Output:
Hello from Named Volume
The file is still available because it belongs to the Docker Volume.
B. Anonymous Volume
An Anonymous Volume is created without specifying a volume name.
Docker automatically generates a unique identifier.
Example:
docker run -it --name anonymous-demo -v /app/data ubuntu bash
Notice that we specified only /app/data, not a volume name.
Docker automatically creates an anonymous volume.
Check the volume:
docker volume ls
Example:
DRIVER VOLUME NAME
local 4c2f8a7d9e...
The volume has an automatically generated name.
Important: Anonymous Volumes can also persist after container deletion. However, they are harder to identify and reuse than Named Volumes.
Named Volume vs Anonymous Volume
|
Feature |
Named Volume |
Anonymous Volume |
|---|---|---|
|
Name |
User-defined |
Docker-generated |
|
Creation |
Explicit |
Automatic |
|
Easy to identify |
Yes |
No |
|
Reuse |
Easy |
Less convenient |
|
Persistence |
Yes |
Yes |
|
Recommended for databases |
Yes |
Less convenient |
3. Different Ways to Mount a Docker Volume
Now let's understand the actual mounting syntax.
Docker supports two commonly used syntaxes.
Method 1: Using -v
docker run -it -v mydata:/app/data ubuntu bash
Syntax:
-v volume-name:container-path
Method 2: Using --mount
docker run -it --mount type=volume,source=mydata,target=/app/data ubuntu bash
Syntax:
--mount type=volume,source=volume-name,target=container-path
Both commands achieve the same result.
Difference between -v and --mount
|
Feature |
-v |
--mount |
|---|---|---|
|
Syntax |
Short |
Detailed |
|
Readability |
Simple |
More explicit |
|
Configuration |
Less explicit |
More explicit |
|
Recommended |
Simple commands |
Complex configurations |
I recommend using --mount when learning Docker because each parameter clearly explains its purpose.
4. Volume Mount Options
Docker provides additional options when mounting volumes.
A. Read and Write Mount (Default)
By default, a volume is mounted with read/write access.
Example:
docker run -it --mount type=volume,source=mydata,target=/app/data ubuntu bash
You can:
-
Read files.
-
Create files.
-
Modify files.
-
Delete files.
B. Read-only Mount
Sometimes you want a container to read data but not modify it.
Use readonly.
docker run -it --mount type=volume,source=mydata,target=/app/data,readonly ubuntu bash
Now:
cat /app/data/message.txt
Works.
But:
echo "New Data" > /app/data/message.txt
Fails because the mount is read-only.
Real-world example: Sharing configuration files with an application without allowing the application to modify them.
C. Volume Subdirectory Mount
Suppose your Docker Volume contains:
mydata
|
+---- Documents
| |
| +---- file1.txt
|
+---- Images
|
+---- Logs
You may want to mount only the Documents directory.
You can use volume-subpath with a volume that already contains that directory.
Example:
docker run -it --mount type=volume,source=mydata,target=/app/documents,volume-subpath=Documents ubuntu bash
This mounts only the Documents subdirectory at /app/documents.
The subdirectory must already exist in the volume.
5. What Happens When We Mount an Existing Volume?
This is an important concept.
Suppose your Docker Volume already contains:
mydata
|
+---- message.txt
You mount it into a container:
docker run -it -v mydata:/app/data ubuntu bash
Inside the container:
ls /app/data
Output:
message.txt
The container can access the existing volume data.
Important: By default, Docker may copy existing files from the container's destination directory into an empty volume when first mounting it.
If you want to disable this behavior, use the volume-nocopy option:
docker run -it --mount type=volume,source=mydata,target=/app/data,volume-nocopy ubuntu bash
This prevents Docker from copying the destination directory's existing contents into the empty volume.
6. Can Multiple Containers Use the Same Volume?
Yes. Multiple containers can share the same Named Volume.
Consider two containers:
Docker Volume
mydata
|
+--------+--------+
| |
V V
Container 1 Container 2
| |
V V
/app/data /app/data
Let's implement this.
Step 1: Create a volume
docker volume create shareddata
Step 2: Create the first container
docker run -it --name container1 -v shareddata:/app/data ubuntu bash
Inside:
echo "Created by Container 1" > /app/data/message.txt
exit
Step 3: Create the second container
docker run -it --name container2 -v shareddata:/app/data ubuntu bash
Inside:
cat /app/data/message.txt
Output:
Created by Container 1
The second container can access the file created by the first container.
You can also modify the file from Container 2 and read the changes from Container 1.
Important: Sharing a volume does not automatically make simultaneous writes safe. Applications must handle concurrency and file locking where necessary.
7. Volume Mount vs Bind Mount
Both provide persistent storage, but their source locations are different.
Volume Mount
Docker-managed storage.
Bind Mount
User-specified host directory.
|
Feature |
Volume Mount |
Bind Mount |
|---|---|---|
|
Source |
Docker Volume |
Host directory/file |
|
Managed by |
Docker |
User / Host OS |
|
Host path required |
No |
Yes |
|
Persistent |
Yes |
Yes |
|
Database storage |
Recommended |
Possible |
|
Source-code sharing |
Less common |
Recommended |
8. Important Docker Volume Commands
|
Command |
Purpose |
|---|---|
|
|
Create volume |
|
|
List volumes |
|
|
View volume details |
|
|
Delete a volume |
|
|
Remove unused volumes |
|
|
Remove unused anonymous and named volumes |
|
|
Remove container and its anonymous volumes |
Warning: Never remove a volume containing important database data unless you have a backup.
9. Real-world ASP.NET Core + SQL Server Example
This is where Docker Volume Mounts become particularly useful in your development work.
Imagine your application architecture:
ASP.NET Core Web API
|
V
Docker Container
|
V
SQL Server
|
V
Docker Volume
|
V
Database Files
You can run SQL Server with a Named Volume.
For example:
docker volume create sqlserverdata
Then:
docker run -d ^
--name sqlserver-demo ^
-e ACCEPT_EULA=Y ^
-e MSSQL_SA_PASSWORD=YourStrong!Pass123 ^
-p 1433:1433 ^
--mount type=volume,source=sqlserverdata,target=/var/opt/mssql ^
mcr.microsoft.com/mssql/server:2022-latest
Here:
|
Parameter |
Purpose |
|---|---|
|
|
Named Volume |
|
|
SQL Server data directory |
|
|
SQL Server port mapping |
|
|
Accept SQL Server licence terms |
|
|
SA password |
Now SQL Server stores its persistent data in the Docker Volume.
If you delete and recreate the SQL Server container using the same volume, the database files remain available.
For real deployments, use a secure secret-management mechanism instead of placing passwords directly in commands. Back up databases independently of Docker volumes.
10. Interview Questions
|
No. |
Question |
Severity |
|---|---|---|
|
1 |
What is a Docker Volume Mount? |
High |
|
2 |
What is the difference between Named and Anonymous Volumes? |
High |
|
3 |
How do you mount a volume into a container? |
High |
|
4 |
What is the difference between |
High |
|
5 |
Can multiple containers share the same volume? |
High |
|
6 |
What is a read-only volume mount? |
Medium |
|
7 |
What is |
Medium |
|
8 |
What is |
Medium |
|
9 |
What happens to volume data when a container is deleted? |
High |
|
10 |
How do you remove unused Docker volumes? |
Medium |
You can store almost any type of file in a Docker Volume. Docker Volumes are not restricted to a particular file format or programming language.
A Docker Volume works like a normal filesystem directory. Your application or container can create, read, update and delete files inside it, subject to filesystem permissions.
1. What types of files can we store?
|
File Category |
Examples |
Real-world Usage |
|---|---|---|
|
Database files |
|
SQL Server, MySQL |
|
Text files |
|
Application logs |
|
Configuration |
|
Application settings |
|
Images |
|
User profile pictures |
|
Documents |
|
Uploaded documents |
|
Videos |
|
Course videos |
|
Audio |
|
Audio files |
|
Backup files |
|
Database backups |
|
Application data |
|
Persistent application data |
2. Practical example – Store different files in one Docker Volume
Let's create a volume called myappdata.
Step 1: Create a Docker Volume
docker volume create myappdata
Step 2: Create a container and mount the volume
docker run -it --name file-demo -v myappdata:/app/data ubuntu bash
Step 3: Create different folders
Execute inside the container:
mkdir -p /app/data/Logs
mkdir -p /app/data/Images
mkdir -p /app/data/Documents
mkdir -p /app/data/Backups
Step 4: Create different files
Text file:
echo "Application started" > /app/data/Logs/app.log
Configuration file:
echo '{"ApplicationName":"MyAPI"}' > /app/data/config.json
Image placeholder:
touch /app/data/Images/profile.png
PDF placeholder:
touch /app/data/Documents/report.pdf
Backup placeholder:
touch /app/data/Backups/database.bak
Note: touch creates empty files; it does not generate valid image, PDF or database backup content.
Step 5: Check the directory structure
ls -R /app/data
Expected structure:
/app/data
|
+---- Logs
| |
| +---- app.log
|
+---- Images
| |
| +---- profile.png
|
+---- Documents
| |
| +---- report.pdf
|
+---- Backups
| |
| +---- database.bak
|
+---- config.json
Step 6: Delete the container
exit
docker rm file-demo
Step 7: Recreate the container using the same volume
docker run -it --name file-demo2 -v myappdata:/app/data ubuntu bash
Execute:
ls -R /app/data
All your files and folders will still be available.
3. Real-world ASP.NET Core application example
Consider your DigitalLearningPlatform project.
You want to store uploaded course materials, application logs and database files.
You could organize your storage like this:
Docker Volumes
|
+---- application-data
| |
| +---- Uploads
| | +---- Images
| | +---- PDFs
| | +---- Videos
| |
| +---- Logs
|
+---- sqlserver-data
|
+---- Database Files
+---- Transaction Logs
You could use separate Named Volumes:
|
Volume |
Container Path |
Purpose |
|---|---|---|
|
|
|
Uploaded files |
|
|
|
Application logs |
|
|
|
SQL Server database files |
This separation makes storage management easier.
4. Important points
-
Docker Volumes can store text, images, videos, documents, database files and binary files.
-
The file extension does not matter to Docker.
-
Your application must know how to read and write the particular file format.
-
Volume storage capacity depends on the underlying Docker host storage.
-
Deleting a container does not normally delete its Named Volume.
-
Docker Volumes are not automatic backups. Maintain separate backups for important data.
A Docker Volume can store any type of file, including database files, application logs, configuration files, images, videos and documents. It provides persistent storage independent of the container lifecycle.