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?"
Practice
Solution
Step 1: Understand container filesystem behavior
Docker containers have an ephemeral filesystem, meaning changes inside the container do not persist after restart.Step 2: Apply this to file creation
Files created inside the container are lost when the container stops or restarts unless saved externally.Final Answer:
The files are deleted because the container filesystem is ephemeral. -> Option AQuick Check:
Container filesystem ephemeral = files lost on restart [OK]
- Thinking files inside container persist after restart
- Assuming Docker automatically saves files externally
- Confusing container filesystem with host filesystem
/data to a container directory /app/data to persist data?Solution
Step 1: Identify volume mount syntax
The correct syntax to mount a host directory is using-v host_path:container_path.Step 2: Match the correct option
docker run -v /data:/app/data myimage uses-v /data:/app/data, which is the correct way to mount the host directory.Final Answer:
docker run -v /data:/app/data myimage -> Option DQuick Check:
Volume mount uses -v host:container [OK]
- Using -p instead of -v for volumes
- Confusing --link or --env with volume mounts
- Forgetting to specify both host and container paths
FROM alpine RUN echo "Hello" > /greeting.txt CMD cat /greeting.txt
If you run this container, then modify
/greeting.txt inside the running container to "Hi", then restart the container, what will cat /greeting.txt output?Solution
Step 1: Understand Dockerfile build vs container runtime
The file/greeting.txtis created during image build with content "Hello".Step 2: Recognize ephemeral container filesystem
Changes made inside a running container (changing to "Hi") are lost after restart because the container filesystem resets.Final Answer:
Hello -> Option BQuick Check:
Container restart resets filesystem, original file restored [OK]
- Assuming changes inside container persist after restart
- Confusing image build files with runtime changes
- Expecting file to be missing after restart
Solution
Step 1: Identify cause of file loss
Files inside container are lost on restart because filesystem is ephemeral.Step 2: Choose method to persist data
Using a Docker volume stores data outside container, preserving files across restarts.Final Answer:
Use a Docker volume to store the file outside the container. -> Option CQuick Check:
Volumes keep data persistent beyond container life [OK]
- Thinking restart flags keep files
- Confusing memory limits with filesystem persistence
- Using --rm removes container on exit, losing files
/var/log/app.log inside the container. You want these logs to persist even if the container is removed or recreated. Which Docker run command achieves this?Solution
Step 1: Understand volume mount for specific file
To persist a single log file, mount the host file to the container file path.Step 2: Analyze options
docker run -v /host/logs/app.log:/var/log/app.log myapp mounts/host/logs/app.logto/var/log/app.loginside container, persisting logs exactly.Step 3: Why other options fail
docker run -v /host/logs/app.log:/var/log myapp tries to mount file to directory path (invalid). docker run -v /host/logs:/var/log/app.log myapp tries to mount directory to file path (invalid). docker run --rm myapp removes container on exit, losing logs.Final Answer:
docker run -v /host/logs/app.log:/var/log/app.log myapp -> Option AQuick Check:
Mount host file to container file for persistent logs [OK]
- Mounting directory to file path (invalid)
- Using --rm which deletes container and files
- Mounting directory when only file persistence needed
Ready to prove it?
Pass the test and this pattern moves unknown
