Bird
Raised Fist0
Dockerdevops~20 mins

Container filesystem is ephemeral in Docker - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Container Filesystem Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding container filesystem persistence

Which statement best describes why data stored inside a Docker container's filesystem is lost after the container stops?

AThe container filesystem is ephemeral and resets to the original image state on restart.
BDocker automatically backs up container files to the host, so data is never lost.
CContainers use a permanent disk partition on the host that preserves all changes.
DContainer filesystems are shared with the host and changes are instantly saved.
Attempts:
2 left
💡 Hint

Think about what happens when you stop and start a container without volumes.

💻 Command Output
intermediate
2:00remaining
Effect of file creation inside a container

What will be the output of the following commands executed on a Docker container?

docker run --rm ubuntu sh -c "echo hello > /tmp/greeting.txt && cat /tmp/greeting.txt"
docker run --rm ubuntu cat /tmp/greeting.txt

Assuming no volumes are used.

ABoth commands output 'hello'.
BFirst command outputs 'hello', second command outputs nothing and returns an error.
CFirst command outputs nothing, second command outputs 'hello'.
DBoth commands output nothing and return errors.
Attempts:
2 left
💡 Hint

Consider what happens to files created inside a container after it exits.

🔀 Workflow
advanced
2:00remaining
Preserving data across container restarts

You want to keep data created by a container even after it stops. Which Docker command option should you use to achieve this?

AUse <code>docker run --network host</code> to share the host network.
BUse <code>docker run --rm</code> to automatically remove the container after exit.
CUse <code>docker run -v /host/path:/container/path</code> to mount a host directory as a volume.
DUse <code>docker run --memory 512m</code> to limit container memory.
Attempts:
2 left
💡 Hint

Think about how to store data outside the container's ephemeral filesystem.

❓ Troubleshoot
advanced
2:00remaining
Data loss despite using volumes

You mounted a volume to persist data, but after restarting the container, the data is missing. What is the most likely cause?

AThe volume was mounted to the wrong container path, so data was saved elsewhere.
BDocker volumes automatically delete data on container restart.
CThe container image does not support volumes.
DDocker does not allow writing to volumes by default.
Attempts:
2 left
💡 Hint

Check the paths used in the volume mount.

✅ Best Practice
expert
3:00remaining
Ensuring data durability in containerized applications

Which practice best ensures that important application data is not lost when containers are updated or recreated?

AUse ephemeral containers without any data storage to avoid complexity.
BRely on container snapshots to save the container state before updates.
CStore all data inside the container image layers for persistence.
DUse Docker volumes or bind mounts to store data outside the container filesystem.
Attempts:
2 left
💡 Hint

Think about separating data from container lifecycle.

Practice

(1/5)
1. What happens to files created inside a Docker container when the container is stopped and restarted?
easy
A. The files are deleted because the container filesystem is ephemeral.
B. The files are saved permanently inside the container.
C. The files are automatically copied to the host machine.
D. The files are backed up to Docker Hub.

Solution

  1. Step 1: Understand container filesystem behavior

    Docker containers have an ephemeral filesystem, meaning changes inside the container do not persist after restart.
  2. Step 2: Apply this to file creation

    Files created inside the container are lost when the container stops or restarts unless saved externally.
  3. Final Answer:

    The files are deleted because the container filesystem is ephemeral. -> Option A
  4. Quick Check:

    Container filesystem ephemeral = files lost on restart [OK]
Hint: Remember: container files reset on restart unless saved outside [OK]
Common Mistakes:
  • Thinking files inside container persist after restart
  • Assuming Docker automatically saves files externally
  • Confusing container filesystem with host filesystem
2. Which Docker command correctly mounts a host directory /data to a container directory /app/data to persist data?
easy
A. docker run --env /data:/app/data myimage
B. docker run -p /data:/app/data myimage
C. docker run --link /data:/app/data myimage
D. docker run -v /data:/app/data myimage

Solution

  1. Step 1: Identify volume mount syntax

    The correct syntax to mount a host directory is using -v host_path:container_path.
  2. 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.
  3. Final Answer:

    docker run -v /data:/app/data myimage -> Option D
  4. Quick Check:

    Volume mount uses -v host:container [OK]
Hint: Use -v host_path:container_path to mount volumes [OK]
Common Mistakes:
  • Using -p instead of -v for volumes
  • Confusing --link or --env with volume mounts
  • Forgetting to specify both host and container paths
3. Consider this Dockerfile snippet:
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?
medium
A. Hi
B. Hello
C. File not found error
D. Empty output

Solution

  1. Step 1: Understand Dockerfile build vs container runtime

    The file /greeting.txt is created during image build with content "Hello".
  2. Step 2: Recognize ephemeral container filesystem

    Changes made inside a running container (changing to "Hi") are lost after restart because the container filesystem resets.
  3. Final Answer:

    Hello -> Option B
  4. Quick Check:

    Container restart resets filesystem, original file restored [OK]
Hint: Changes inside container lost on restart; image files stay [OK]
Common Mistakes:
  • Assuming changes inside container persist after restart
  • Confusing image build files with runtime changes
  • Expecting file to be missing after restart
4. You ran a container and created a file inside it. After restarting, the file disappeared. You want to keep the file persistent. Which of these fixes the problem?
medium
A. Restart the container with --restart=always flag.
B. Increase container memory limit.
C. Use a Docker volume to store the file outside the container.
D. Run the container with --rm option.

Solution

  1. Step 1: Identify cause of file loss

    Files inside container are lost on restart because filesystem is ephemeral.
  2. Step 2: Choose method to persist data

    Using a Docker volume stores data outside container, preserving files across restarts.
  3. Final Answer:

    Use a Docker volume to store the file outside the container. -> Option C
  4. Quick Check:

    Volumes keep data persistent beyond container life [OK]
Hint: Persist files with volumes, not restart flags or memory [OK]
Common Mistakes:
  • Thinking restart flags keep files
  • Confusing memory limits with filesystem persistence
  • Using --rm removes container on exit, losing files
5. You want to run a container that writes logs to /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?
hard
A. docker run -v /host/logs/app.log:/var/log/app.log myapp
B. docker run --rm myapp
C. docker run -v /host/logs:/var/log/app.log myapp
D. docker run -v /host/logs/app.log:/var/log myapp

Solution

  1. Step 1: Understand volume mount for specific file

    To persist a single log file, mount the host file to the container file path.
  2. Step 2: Analyze options

    docker run -v /host/logs/app.log:/var/log/app.log myapp mounts /host/logs/app.log to /var/log/app.log inside container, persisting logs exactly.
  3. 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.
  4. Final Answer:

    docker run -v /host/logs/app.log:/var/log/app.log myapp -> Option A
  5. Quick Check:

    Mount host file to container file for persistent logs [OK]
Hint: Mount host file to container file path to persist logs [OK]
Common Mistakes:
  • Mounting directory to file path (invalid)
  • Using --rm which deletes container and files
  • Mounting directory when only file persistence needed
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard