Container filesystem is ephemeral in Docker - Time & Space Complexity
We want to understand how the time cost changes when working with container filesystems that do not keep data after the container stops.
How does the ephemeral nature affect operations inside the container as data size grows?
Analyze the time complexity of the following Dockerfile snippet.
FROM alpine:latest
RUN echo "Hello World" > /tmp/hello.txt
RUN cat /tmp/hello.txt
CMD ["cat", "/tmp/hello.txt"]
This snippet creates a file inside the container, reads it, and tries to keep it available when the container runs.
Look for repeated actions that affect time cost.
- Primary operation: Writing and reading files inside the container filesystem.
- How many times: Each RUN command executes once during build; CMD runs once per container start.
As the file size inside the container grows, the time to write and read the file grows roughly in proportion to the file size.
| Input Size (file size in KB) | Approx. Operations (time to write/read) |
|---|---|
| 10 | Small, fast write and read |
| 100 | About 10 times longer than 10 KB |
| 1000 | About 100 times longer than 10 KB |
Pattern observation: Time grows linearly with file size inside the ephemeral container filesystem.
Time Complexity: O(n)
This means the time to write or read files grows directly with the size of the data inside the container.
[X] Wrong: "Files inside a container stay forever, so writing once means no time cost later."
[OK] Correct: Containers lose their filesystem changes when stopped, so files must be recreated or stored outside, causing repeated time costs.
Understanding ephemeral filesystems helps you explain how container data handling affects performance and persistence, a useful skill for real projects.
"What if we used a Docker volume to store files instead? How would the time complexity of file access change inside the container?"