0
0
Dockerdevops~5 mins

Container filesystem is ephemeral in Docker - Commands & Configuration

Choose your learning style9 modes available
Introduction
Containers have their own filesystem that disappears when the container stops. This means any changes made inside the container are lost unless saved outside.
When you want to test an application without worrying about leftover files after stopping the container
When running short-lived tasks that do not need to save data permanently
When you want to keep your host system clean from temporary files created by containers
When you want to understand why data disappears after restarting a container
When you need to persist data beyond the life of a container using volumes
Commands
This command runs a temporary Alpine Linux container, writes 'hello' to a file inside it, then reads the file to show the content.
Terminal
docker run --name temp-container alpine sh -c "echo hello > /hello.txt && cat /hello.txt"
Expected OutputExpected
hello
--name - Assigns a name to the container for easier reference
Stops the running container named temp-container, ending its lifecycle but does not remove the container or its filesystem.
Terminal
docker stop temp-container
Expected OutputExpected
temp-container
Removes the stopped container to clean up resources and deletes its writable filesystem.
Terminal
docker rm temp-container
Expected OutputExpected
temp-container
Runs a new Alpine container and tries to read the file /hello.txt which was created in the previous container. This shows the file does not exist because the previous container's filesystem was ephemeral.
Terminal
docker run --rm alpine cat /hello.txt
Expected OutputExpected
cat: /hello.txt: No such file or directory
--rm - Automatically removes the container after it exits
Key Concept

If you remember nothing else from this pattern, remember: any changes inside a container are lost when it stops unless you save them outside.

Common Mistakes
Expecting files created inside a container to persist after the container stops
Containers have isolated, temporary filesystems that are deleted when the container is removed
Use Docker volumes or bind mounts to save data outside the container
Not removing stopped containers and expecting their data to be available later
Removing containers deletes their writable layer and all changes made inside
Keep containers running or use volumes to persist important data
Summary
Run a container and create a file inside it to see how data is stored temporarily.
Stop and remove the container to simulate container lifecycle end.
Run a new container and try to access the old file to observe that it no longer exists.
Understand that container filesystems are ephemeral and data must be saved externally to persist.