Let's understand Docker tmpfs Mount from the basics and then perform practical exercises using your Windows machine and Docker Desktop.
1. What is a tmpfs Mount?
A tmpfs Mount is a temporary storage mechanism that stores data in memory (RAM) instead of persistent disk storage.
The data is available inside the container while the tmpfs mount exists. When the container stops, the tmpfs data is lost.
In simple words:
-
Docker Volume = Persistent storage.
-
Bind Mount = Host machine storage.
-
tmpfs Mount = Temporary memory-based storage.
Architecture
Docker Host
|
V
Docker Container
|
V
/app/temp
|
V
tmpfs Mount
|
V
RAM
|
Temporary Files
Unlike Docker Volumes and Bind Mounts, tmpfs does not provide persistent disk storage.
2. Why do we need tmpfs Mount?
Consider an ASP.NET Core application.
Your application needs to generate a temporary file while processing a request.
For example:
-
User uploads an Excel file.
-
Application temporarily processes the Excel file.
-
Application extracts required information.
-
Application generates a response.
-
Temporary file is no longer required.
Do you need to store this temporary file permanently?
No.
You can use tmpfs Mount to store temporary files in memory.
Real-world use cases
|
Use Case |
Example |
|---|---|
|
Temporary files |
Intermediate processing files |
|
Cache |
Temporary application cache |
|
Sensitive temporary data |
Short-lived processing data |
|
File processing |
Temporary PDF or Excel files |
|
Session processing |
Temporary session-related files |
Important: tmpfs can reduce persistent disk writes, but it is not automatically secure storage. Sensitive data may still be exposed through memory, swap or other host-level mechanisms.
3. Practical Example – Create a tmpfs Mount
Let's perform a practical exercise.
We will:
-
Create a Docker container.
-
Attach a tmpfs Mount.
-
Create files inside it.
-
Read and modify files.
-
Stop the container.
-
Verify that the temporary data is gone.
Step 1: Create a Docker container
Open Windows CMD:
docker run -it --name tmpfs-demo --tmpfs /app/temp ubuntu bash
Let's understand the command:
|
Parameter |
Explanation |
|---|---|
|
|
Create and start container |
|
|
Interactive terminal |
|
|
Container name |
|
|
Create tmpfs Mount |
|
|
Mount location inside container |
|
|
Docker image |
|
|
Open Linux terminal |
Now you are inside the Ubuntu container.
Step 2: Create a file inside tmpfs
Execute:
echo "Hello from tmpfs Mount" > /app/temp/message.txt
Read the file:
cat /app/temp/message.txt
Output:
Hello from tmpfs Mount
The file is stored in memory through the tmpfs filesystem.
Step 3: Create additional files
echo "Temporary Application Log" > /app/temp/app.log
echo "Temporary User Data" > /app/temp/user.txt
Check all files:
ls -l /app/temp
Output:
app.log
message.txt
user.txt
Step 4: Check filesystem information
Execute:
df -h /app/temp
This displays filesystem information for the tmpfs mount.
You should see tmpfs as the filesystem type.
Step 5: Exit the container
exit
Now the container stops.
Step 6: Start the same container again
Execute from Windows CMD:
docker start -ai tmpfs-demo
Now check:
ls -l /app/temp
The directory will be empty.
Why?
Because the tmpfs data was lost when the container stopped.
The container still exists, but its previous tmpfs contents do not.
Step 7: Delete the container
Exit again:
exit
Then:
docker rm tmpfs-demo
The container is deleted.
4. tmpfs Mount vs Docker Volume vs Bind Mount
This is one of the most important Docker concepts.
Docker Volume
Data is stored in Docker-managed storage.
-
Survives container deletion.
-
Suitable for databases and application data.
Bind Mount
Data is stored in a host directory.
-
Accessible directly from Windows.
-
Suitable for source code and logs.
tmpfs Mount
Data is stored in memory.
-
Not persistent.
-
Suitable for temporary processing data.
|
Feature |
Volume |
Bind Mount |
tmpfs |
|---|---|---|---|
|
Storage |
Docker-managed disk |
Host filesystem |
Memory |
|
Persistent |
Yes |
Yes |
No |
|
Survives container deletion |
Yes |
Yes |
No |
|
Host directory required |
No |
Yes |
No |
|
Database storage |
Yes |
Possible |
No |
|
Temporary processing |
Possible |
Possible |
Yes |
5. tmpfs Mount with Size Limit
You can specify the maximum size of a tmpfs Mount.
For example:
docker run -it --name tmpfs-size-demo --tmpfs /app/temp:size=64m ubuntu bash
Here:
-
/app/temp= Mount location. -
size=64m= Maximum tmpfs size of 64 MB.
Check it:
df -h /app/temp
You should see approximately 64 MB as the configured size.
Why specify a size limit?
Without a reasonable limit, temporary data could consume excessive memory and affect other applications.
Note that the size is a maximum limit, not memory reserved immediately. Actual memory consumption depends on the data written.
6. tmpfs Mount Using --mount Syntax
You can also use the more explicit --mount syntax.
Example:
docker run -it --name tmpfs-mount-demo --mount type=tmpfs,destination=/app/temp ubuntu bash
To specify size and permissions:
docker run -it --name tmpfs-mount-demo --mount "type=tmpfs,destination=/app/temp,tmpfs-size=67108864,tmpfs-mode=1777" ubuntu bash
Explanation:
|
Parameter |
Meaning |
|---|---|
|
|
Mount type |
|
|
Container directory |
|
|
Maximum size: 64 MB |
|
|
Directory permissions |
1777 means all users can read, write and access the directory, while the sticky bit restricts deletion of other users' files.
7. Important: What Happens in Different Container States?
Understand this carefully.
|
Container Operation |
tmpfs Data |
|---|---|
|
Create container |
Empty |
|
Write a file |
Available |
|
Read a file |
Available |
|
Restart container |
Lost |
|
Stop container |
Lost |
|
Delete container |
Lost |
Unlike Docker Volumes, tmpfs does not preserve data when the container stops.
8. ASP.NET Core Practical Use Case
Consider your DigitalLearningPlatform application.
Suppose users upload PDF documents, and your API needs to process them temporarily.
You can use this architecture:
User Uploads PDF
|
V
ASP.NET Core Web API
|
V
Docker Container
|
V
/app/temp
|
V
tmpfs Mount
|
V
Process PDF File
|
V
Generate Response
|
V
Temporary Data Removed
Example Docker command:
docker run -d --name learning-api --tmpfs /app/temp:size=128m mylearningapi:v1
Your ASP.NET Core application can use:
string tempPath = "/app/temp";
string filePath = Path.Combine(tempPath, "processing.txt");
await File.WriteAllTextAsync(
filePath,
"Temporary processing data"
);
After processing, you can remove the file:
if (File.Exists(filePath))
{
File.Delete(filePath);
}
Important: If your API needs to retain uploaded PDFs or videos, use a Docker Volume or external object storage such as Azure Blob Storage instead. tmpfs is only for temporary processing.
9. Important Interview Questions
|
No. |
Question |
Severity |
|---|---|---|
|
1 |
What is a tmpfs Mount in Docker? |
High |
|
2 |
Why do we use tmpfs Mounts? |
High |
|
3 |
Where does tmpfs store data? |
High |
|
4 |
What happens to tmpfs data when a container stops? |
High |
|
5 |
What is the difference between tmpfs and Docker Volume? |
High |
|
6 |
Can we specify a size limit for tmpfs? |
Medium |
|
7 |
What is the difference between |
Medium |
|
8 |
Can tmpfs be used for database storage? |
Medium |
|
9 |
Is tmpfs data automatically secure? |
Medium |
|
10 |
Does tmpfs reserve its entire configured size immediately? |
Medium |
10. Final Summary
Remember these three storage mechanisms:
|
Storage |
Think of it as |
Best suited for |
|---|---|---|
|
Docker Volume |
External hard disk |
Persistent application data |
|
Bind Mount |
Shared host folder |
Development and file sharing |
|
tmpfs Mount |
Temporary RAM storage |
Temporary processing data |