What if your app's files could never be changed by mistake or attack while running?
Why Read-only filesystem containers in Docker? - Purpose & Use Cases
Imagine you run a web app on a server where anyone can accidentally or maliciously change files. You try to keep the app safe by manually checking and fixing files after every change.
This manual checking is slow and tiring. You might miss changes, causing bugs or security holes. Fixing these problems after they happen wastes time and can break your app.
Using read-only filesystem containers means the app's files cannot be changed while running. This stops accidental or harmful changes instantly, making your app safer and more reliable without extra work.
docker run myapp
# App files can be changed inside containerdocker run --read-only myapp
# Filesystem is locked, no changes allowedYou can run apps confidently knowing their files stay safe and unchanged during execution.
A company runs a payment service in containers with read-only filesystems to prevent hackers from altering critical files during attacks.
Manual file protection is slow and error-prone.
Read-only containers lock files to prevent changes.
This improves security and stability effortlessly.
Practice
--read-only flag do when starting a Docker container?Solution
Step 1: Understand the
The--read-onlyflag purpose--read-onlyflag makes the container's filesystem read-only, so no files can be changed or created inside the container.Step 2: Compare other options
Running with root privileges, deleting files, or allowing writes to the host filesystem are unrelated or incorrect behaviors for this flag.Final Answer:
Makes the container's filesystem read-only, preventing any changes inside it. -> Option CQuick Check:
--read-only= read-only filesystem [OK]
--read-only locks container files from changes [OK]- Thinking it deletes files instead of locking them
- Confusing with volume mounts
- Assuming it grants root access
Solution
Step 1: Check the correct flag syntax
The correct flag to make the filesystem read-only is--read-onlywith a hyphen.Step 2: Validate options
docker run --read-only alpine uses the correct flag--read-only. The short flag-rodoesn't exist,--readonlylacks the hyphen, and--readwriteis the opposite meaning.Final Answer:
docker run --read-only alpine -> Option AQuick Check:
Correct flag is--read-only[OK]
--read-only [OK]- Using
--readonlywithout hyphen - Using short flags like
-rowhich don't exist - Confusing with
--readwrite
docker run --read-only -v /data alpine sh -c "echo hello > /data/file.txt && cat /data/file.txt"What will be the output?
Solution
Step 1: Understand
The container filesystem is read-only, but the--read-onlywith volume mount/datadirectory is mounted as a volume, which is writable.Step 2: Analyze the command behavior
The command writes "hello" to/data/file.txtand then reads it back. Since/datais writable, this succeeds and outputs "hello".Final Answer:
hello -> Option AQuick Check:
Writable volume inside read-only container = writable [OK]
- Assuming entire container is fully read-only including volumes
- Expecting permission denied on volume writes
- Confusing volume mount syntax
docker run --read-only alpine sh -c "touch /tmp/testfile"But it fails with a permission error. What is the best fix?
Solution
Step 1: Identify cause of permission error
The container filesystem is read-only, so creating files in/tmpfails.Step 2: Provide writable space with tmpfs
Using--tmpfs /tmpmounts a writable temporary filesystem at/tmp, allowing file creation despite read-only root.Final Answer:
Add a writable tmpfs mount for /tmp using--tmpfs /tmp-> Option BQuick Check:
Writable tmpfs fixes write errors in read-only containers [OK]
--tmpfs to add writable temp folders in read-only containers [OK]- Removing
--read-onlyloses security benefits - Mounting host /tmp may cause permission issues
- Assuming root user can write despite read-only filesystem
/app/logs. Which command correctly achieves this?Solution
Step 1: Understand read-only with writable folder
The container filesystem is read-only, but mounting a host directory as a volume at/app/logsmakes that folder writable.Step 2: Evaluate options
docker run --read-only -v /host/logs:/app/logs alpine uses--read-onlyplus a volume mount for writable logs. docker run --read-only --tmpfs /app/logs alpine uses tmpfs which is temporary and not persistent. docker run -v /host/logs:/app/logs alpine lacks--read-only. docker run --read-only alpine has no writable folder.Final Answer:
docker run --read-only -v /host/logs:/app/logs alpine -> Option DQuick Check:
Volume mount +--read-only= writable folder [OK]
--read-only for writable folders [OK]- Using tmpfs when persistence is needed
- Forgetting
--read-onlyflag - Expecting writable without volume or tmpfs
Ready to prove it?
Pass the test and this pattern moves unknown
