What if all your work vanished every time you closed your app? Containers do that by default!
Why Container filesystem is ephemeral in Docker? - Purpose & Use Cases
Imagine you run a software inside a container and make some changes to files while it's running. Then, you stop the container and start it again later. You expect your changes to still be there, but they are gone!
Manually managing file changes inside containers is frustrating because containers don't save changes after they stop. This means you lose work, have to redo setups, and can't keep important data easily.
Understanding that the container filesystem is ephemeral helps you plan better. You can use special storage areas called volumes to keep data safe outside the container's temporary space.
docker run myapp # make file changes inside container # stop container # start container again # changes lost
docker run -v mydata:/app/data myapp # changes saved in volume # stop and start container # changes persist
This concept lets you keep important data safe and share it between containers, making your apps reliable and easy to manage.
A developer runs a database inside a container. Without volumes, all data disappears when the container stops. Using volumes, the database keeps all records safe even after restarts.
Containers erase file changes when stopped.
Data must be stored outside containers to persist.
Volumes solve the problem by saving data safely.
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
