Skip to content

Interview PrepDocker

Storage, Limits and Isolation ​

90. What happens to a container's writable filesystem when the container is removed? ​

The short answer

It is deleted, permanently. Everything the container wrote outside of volumes and mounts lives in its writable layer, and docker rm throws that layer away. Stopping a container does not delete it. Only removing it does. Named volumes survive removal on purpose, so data that must outlive a container belongs in a volume.

Picture the writable layer as a whiteboard bolted to one container. You can stop the container and start it again, and the whiteboard keeps its writing. Remove the container, and the whiteboard goes into the bin with it.

bash
docker run --name demo alpine sh -c 'echo hello > /note.txt'   # (1)
docker start -a demo                                           # (2)
docker cp demo:/note.txt -                                     # (3)
docker rm demo                                                 # (4)
  1. The container writes /note.txt into its writable layer, then exits.
  2. Starting it again reuses the same container. The file is still there.
  3. Copying the file out proves it survived the stop.
  4. Removal deletes the writable layer. The file is gone forever.

What survives docker rm:

Data written toSurvives docker rm?
The writable layerNo
A named volumeYes
A bind mount, a host folderYes, it is on the host
An anonymous volumeOnly without --rm or -v
A tmpfs mountNo, and not even a stop

An anonymous volume is a volume Docker created with a random name, usually because the image has a VOLUME line (question 467). docker run --rm and docker rm -v delete a container's anonymous volumes along with it. Named volumes are never deleted that way.

The lesson for production: treat a container's own filesystem as temporary. Logs go to standard output, data goes to a volume or a database, and the container itself can be replaced at any moment without losing anything.

91. What is the difference between a bind mount, a named volume, and the container writable layer? ​

The short answer

The writable layer is the container's own temporary scratch space on top of the image, deleted with the container. A bind mount places an existing folder or file from the host directly into the container, so both see the same files. A named volume is storage that Docker creates and manages itself, kept in Docker's own area of the host, which survives containers and can be backed by plug-in drivers for network or cloud storage.

container's view of its filesystem/tmp/cachewritable layer/srcbind mount/var/lib/postgresql/datanamed volumethe host's diskoverlay2 upper folderdeleted with container/home/dev/shop/srcyour own folder/var/lib/docker/volumesmanaged by Docker
Three places a container can write. All three look like ordinary folders from inside. On the host they are a temporary layer (red), a folder you own (amber), and storage Docker manages for you (purple).
bash
# (1)
docker run -v /home/dev/shop/src:/src node:20-slim ls /src
# (2)
docker run --mount type=bind,source=/home/dev/shop/src,target=/src,readonly node:20-slim ls /src
# (3)
docker volume create pgdata
docker run -d -v pgdata:/var/lib/postgresql/data postgres:16
  1. A bind mount with the short -v syntax. A path that starts with / means "this host folder".
  2. The same bind mount with the longer --mount syntax, which is clearer and adds readonly. One difference: if the host folder does not exist, -v silently creates an empty one, while --mount fails with an error, which is usually what you want.
  3. A named volume. A plain name instead of a path means "a Docker volume called this".
Writable layerBind mountNamed volume
Who manages itDocker, per containerYouDocker
Where on the hostoverlay2 foldersAny path you choose/var/lib/docker/volumes
Survives the containerNoYesYes
Speed for heavy writesSlowestNativeNative
Can use remote storageNoOnly what the host has mountedYes, with a volume driver
Typical useTemporary filesSource code in developmentDatabase data

Why volumes are faster than the writable layer. The writable layer goes through OverlayFS, with copy-ups and lookups across many folders (question 463). A volume or a bind mount is a plain folder mounted straight in, so reads and writes go directly to the host's filesystem.

92. When would you use a named volume rather than a bind mount? ​

The short answer

Use a named volume for data the app owns, such as a database's files. Docker manages its location, so it does not depend on any folder existing on the host. It is easy to back up and move, and it can use remote storage drivers. Use a bind mount when the host and the container must share specific files, such as source code during development or a configuration file from the host.

Choose a named volume when:

  1. The data belongs to the app. Database files, uploaded files, a queue's storage. You care that the data persists, not where it sits on the host.
  2. You want the setup to work on any host. A Compose file that uses pgdata:/var/lib/postgresql/data works on every machine. A bind mount to /home/dev/pgdata only works where that folder exists.
  3. You run Docker Desktop on Mac or Windows. Bind mounts there cross from the host into Docker's Linux VM through a file-sharing layer, which can be much slower. Named volumes live inside the VM at full speed.
  4. You want the image's starting files. When an empty named volume is first mounted on a folder that has files in the image, Docker copies those files into the volume. A bind mount never does this: it hides whatever the image had at that path.
  5. You need remote or special storage. Volume drivers can put a volume on NFS, on cloud disks, or on other storage systems.

Choose a bind mount when:

  1. Live code editing in development. Mount your source folder so the app sees each change instantly, often with a file-watcher that reloads it.
  2. Supplying a host file, such as a configuration file or a TLS certificate, especially read-only.
  3. Reaching host resources, such as a Unix socket. Mounting the Docker socket is the dangerous example (question 140).

Not great for either: production secrets. A plain bind mount of a secret file works, but in production a secret store and the orchestrator's secret mechanism are better (question 144).

93. How do you persist a database's data across container recreation? ​

The short answer

Mount a named volume on the folder where the database keeps its data files, such as /var/lib/postgresql/data for PostgreSQL. When you remove and recreate the container, for example to upgrade the image, the new container mounts the same volume and finds all the data. Never let a database write into the container's writable layer.

bash
docker volume create pgdata                                   # (1)
docker run -d --name db \
  -e POSTGRES_PASSWORD_FILE=/run/secrets/pg_password \
  -v pgdata:/var/lib/postgresql/data \
  -v "$PWD/pg_password.txt:/run/secrets/pg_password:ro" \
  postgres:16.4                                               # (2)
docker rm -f db                                               # (3)
docker run -d --name db -v pgdata:/var/lib/postgresql/data \
  -v "$PWD/pg_password.txt:/run/secrets/pg_password:ro" \
  -e POSTGRES_PASSWORD_FILE=/run/secrets/pg_password postgres:16.6   # (4)
  1. Creates the volume once.
  2. Starts PostgreSQL with its data folder on the volume. The password is read from a mounted file rather than a plain environment variable, so it does not show up in docker inspect.
  3. Removes the container completely. The volume is untouched.
  4. Starts a new container with a newer minor version, using the same volume. All tables and rows are still there.

Things that still go wrong, and are worth mentioning:

  1. Major version upgrades. PostgreSQL 16 cannot read PostgreSQL 15's data files directly. A major upgrade needs pg_upgrade or a dump and restore. Simply changing the image tag fails at startup with a clear error.
  2. Mounting the wrong folder. If the volume is mounted at a path the database does not use, data goes into the writable layer and vanishes on recreation. Check the image's documentation for its data path.
  3. Two containers on one volume. Two database processes writing the same files will corrupt them. A volume is not a sharing mechanism for databases.
  4. A volume is not a backup. Deleting the volume, or a bug in the app, destroys the only copy. Question 95 covers backups.

In production, many teams avoid running databases in containers on a single host at all, and use a managed database service instead, because backups, failover and patching are then handled for them. Question 169 discusses the trade-offs.

94. How do file ownership and permissions behave when a host directory is bind-mounted into a container? ​

The short answer

The kernel checks permissions by user number, not by user name, and a bind mount does not translate numbers. A file owned by user 1000 on the host is owned by user 1000 inside the container, whoever that is there. So an app running as a different user number cannot write to the folder, and files the container creates as root appear on the host owned by root. You fix it by matching the numbers, using a named volume, or using user namespaces.

Linux stores a file's owner as a number: the UID (user ID) and GID (group ID). Names like dev or node are just labels from /etc/passwd, and the container has its own /etc/passwd. So UID 1000 might be dev on the host and node inside the container. The kernel only sees "1000".

Two problems this causes:

  1. Permission denied. Your laptop folder is owned by UID 1000. The container's app runs as UID 10001. It cannot write there:
bash
docker run --rm -u 10001 -v "$PWD/uploads:/app/uploads" shop/api touch /app/uploads/x
# touch: cannot touch '/app/uploads/x': Permission denied
  1. Root-owned files on the host. A container running as root writes into a bind-mounted folder. On the host, the new files belong to root, and your normal user cannot delete them without sudo.

The fixes:

  1. Run the container as your own UID in development: docker run -u "$(id -u):$(id -g)" .... Files then belong to you on both sides.
  2. Give the folder to the container's UID: sudo chown -R 10001:10001 uploads.
  3. Use a named volume instead, and let Docker manage it. When an empty volume is first mounted, Docker copies the image's files and their ownership into it (question 92), so the app's user already owns it.
  4. User namespace remapping or rootless Docker (question 142). Container UIDs are shifted to a high range on the host, so root inside the container is an unprivileged UID outside. This protects the host, but makes bind-mounted files look like they have strange owners, which needs its own planning.
  5. SELinux hosts, such as Fedora and Red Hat systems, add a second check. Even with the right UID, access is denied unless the folder carries the right security label. Adding :z (shared) or :Z (private to this container) to the mount tells Docker to relabel it.

95. How would you back up and restore data stored in a Docker volume? ​

The short answer

Start a temporary container that mounts both the volume and a host folder, and use tar to copy the volume's contents into an archive in the host folder. Restore by doing the same thing in reverse. For a database, a file copy taken while it is running may be inconsistent, so either stop the database first, or better, use the database's own backup tool, such as pg_dump. Then test the restore regularly.

A file-level backup:

bash
docker stop db                                                                 # (1)
docker run --rm -v pgdata:/data:ro -v "$PWD:/backup" alpine \
  tar -czf /backup/pgdata-2026-10-27.tgz -C /data .                            # (2)
docker start db
  1. Stops the database so its files are in a consistent state on disk.
  2. A throwaway alpine container mounts the volume read-only at /data and the current host folder at /backup. tar -czf packs everything in /data into a compressed archive in the host folder. --rm deletes the helper afterwards.

Restore it into a new volume:

bash
docker volume create pgdata-restored
docker run --rm -v pgdata-restored:/data -v "$PWD:/backup" alpine \
  tar -xzf /backup/pgdata-2026-10-27.tgz -C /data

For a database, a logical backup is usually better:

bash
docker exec db pg_dump -U postgres -Fc shop > shop-2026-10-27.dump              # (1)
docker exec -i db pg_restore -U postgres -d shop --clean < shop-2026-10-27.dump # (2)
  1. pg_dump asks PostgreSQL itself for a consistent snapshot of the shop database while it keeps running, and writes it in PostgreSQL's custom compressed format.
  2. Restores it into a running database.

Why a running file copy is risky. A database writes to several files at once and keeps recent changes in memory. Copying its files mid-write can capture a mix of old and new data that it later refuses to open. Stop it first, or use a tool that understands the database.

The rule that matters most: a backup you have never restored is a hope, not a backup. Schedule a regular test restore into a scratch environment, and check that the data is really there. Question 307 covers testing recovery properly.

96. What is the difference between a container's read-only filesystem and writable mounted paths? ​

The short answer

--read-only makes the container's whole root filesystem read-only, including its writable layer, so nothing can write anywhere except where you explicitly allow it. Mounted paths, such as a volume or a tmpfs at /tmp, stay writable. The result is a container that can only change the few folders you chose, which blocks many attacks and makes it obvious where an app writes.

bash
docker run -d --read-only \
  --tmpfs /tmp:size=64m \
  -v uploads:/app/uploads \
  shop/api:1.4

Inside this container:

  • Writing to /app/server.js fails with Read-only file system. The app's own code cannot be changed.
  • Writing to /tmp works. It is a tmpfs, a small filesystem in memory (question 470).
  • Writing to /app/uploads works. It is a volume.

Why it is worth doing:

  1. Attackers cannot plant files. A common attack downloads a tool or script into the container and runs it. With a read-only root, there is nowhere to put it, except the few writable paths, which you can also mount with noexec so nothing stored there can be run.
  2. The image cannot drift. Nobody can "quickly fix" a file in a running container.
  3. It documents the app. You must list every place the app writes. Writes you did not expect show up immediately as errors, instead of slowly filling the writable layer.

What usually needs to stay writable: /tmp, a cache folder, a folder for process ID files such as /var/run, and whatever data folder the app really uses. For nginx, that means its cache and run folders.

In Kubernetes, the same setting is readOnlyRootFilesystem: true in the container's security context, with emptyDir volumes for the writable paths (question 419).

97. What happens when a container is started with a memory limit and the process exceeds it? ​

The short answer

The limit is enforced by the kernel through the container's cgroup. As usage nears the limit, the kernel first frees memory it can, such as cached file data. If the process still needs more than the limit, the kernel's out-of-memory killer ends a process in that container with SIGKILL. The container exits with code 137, docker inspect shows OOMKilled: true, and the restart policy may start it again.

bash
docker run -d --name api -m 512m --memory-swap 512m shop/api:1.4   # (1)
docker inspect -f 'exit={{.State.ExitCode}} oom={{.State.OOMKilled}}' api   # (2)
  1. -m 512m sets a hard memory limit of 512 MB. Setting --memory-swap to the same value means no swap may be used on top, so the limit is a real ceiling. Swap is disk space the kernel can use as slow overflow memory.
  2. After a kill, this prints exit=137 oom=true.

What happens, step by step:

  1. The app's memory use grows: its heap, its threads, and file data the kernel cached for it, all counted in its cgroup.
  2. Near the limit, the kernel reclaims what it safely can, mainly that cached file data, which can be read again from disk later.
  3. If the app keeps asking for memory and nothing more can be reclaimed, the kernel runs the OOM killer, out of memory, inside that cgroup only. It picks a process, normally the biggest, which is usually your app, and sends SIGKILL.
  4. The process dies instantly, with no chance to log anything. That is why OOM kills often leave no error message.
  5. The exit code is 128 + 9 = 137. Docker records OOMKilled: true.
  6. If the container has a restart policy, it starts again. If the cause is still there, it is killed again, in a loop.

Why apps hit the limit unexpectedly:

  • Runtimes that do not see the limit. Old Java versions sized their memory from the whole host's RAM, not the container's limit. Modern Java and Node.js read the cgroup limit, but heap settings still need care (question 68).
  • Memory outside the heap. Thread stacks, native libraries, buffers and file caches all count toward the limit.
  • A real leak. Usage grows steadily until the kill. Question 156 shows how to tell a leak from a limit that is too low.

Avoid --oom-kill-disable unless you know exactly why. Without the killer, a container at its limit freezes instead of dying, which is usually worse.

98. How do CPU limits and CPU reservations or shares affect container behavior? ​

The short answer

A CPU limit, such as --cpus=1.5, is a hard ceiling: the kernel lets the container use 1.5 CPUs' worth of time in each short period, then pauses it until the next period. That pause is called throttling. CPU shares, or weights, are not a ceiling: they only decide who gets more CPU when several containers compete, and an idle machine lets any container use everything. --cpuset-cpus pins a container to specific cores.

1. The hard limit: --cpus.

bash
docker run -d --cpus=1.5 shop/api:1.4

The kernel's scheduler works in short periods, 100 milliseconds by default. --cpus=1.5 gives the container a quota of 150 ms of CPU time per 100 ms period, across all its threads. With several busy threads, it can use that quota in the first 40 ms of the period. Then it is throttled: frozen until the next period starts. Nothing is killed, but requests wait. Question 392 explains how this hurts latency.

2. The relative weight: --cpu-shares.

bash
docker run -d --cpu-shares=2048 --name important shop/api:1.4
docker run -d --cpu-shares=1024 --name batch shop/batch:1.0

The default weight is 1024. If both containers want all the CPU at once, important gets about two thirds and batch one third. If batch is idle, important can use the whole machine. Shares only matter when there is a fight for CPU. On cgroup v2 systems these values are converted to a setting called cpu.weight, but the idea is the same.

3. Pinning: --cpuset-cpus.

bash
docker run -d --cpuset-cpus=0,1 shop/latency-critical:1.0

The container may only run on cores 0 and 1. This is used for workloads that must not be disturbed, or to keep a noisy job away from important ones.

SettingKindEffect when the machine is idleEffect under contention
--cpusHard capStill cappedCapped
--cpu-sharesWeightNo effectDecides the split
--cpuset-cpusPlacementOnly those coresOnly those cores

How Kubernetes maps onto this. A container's CPU request becomes its weight, and its CPU limit becomes its quota. That is why many teams set CPU requests but no CPU limits: the requests protect everyone's fair share, and no container is throttled while the machine has spare CPU. Question 389 covers requests and limits.

99. What are Linux namespaces, and which kinds of isolation do they provide? ​

The short answer

A namespace is a kernel feature that gives a process its own private copy of one kind of system resource. A process in its own network namespace sees only its own network interfaces. A process in its own PID namespace sees only its own processes. Linux has eight kinds: mount, PID, network, UTS, IPC, user, cgroup and time. A container is a process placed in a new namespace of most of these kinds.

Think of namespaces as one-way glass partitions. Each process sees only what is inside its own partition, and believes that is the whole world.

NamespaceWhat the process gets its own copy of
MountThe list of mounted filesystems, so its own /
PIDProcess numbers, so it can be PID 1 and see only its own processes
NetworkInterfaces, IP addresses, ports, routing table, firewall rules
UTSHostname and domain name
IPCShared memory and message queues between processes
UserUser and group numbers, so root inside can be unprivileged outside
CgroupIts view of the cgroup tree, so it sees its own limits as the root
TimeOffsets for some system clocks

Seeing them for yourself:

bash
docker run -d --name ns-demo nginx:1.27
pid=$(docker inspect -f '{{.State.Pid}}' ns-demo)    # (1)
sudo ls -l /proc/$pid/ns                             # (2)
sudo nsenter -t $pid -n ip addr                      # (3)
  1. Finds the container's main process number as the host sees it, for example 48211. Inside the container, the same process believes it is number 1.
  2. Lists its namespaces. Each is a link with an ID, such as net:[4026532609]. A host process shows different IDs.
  3. nsenter runs a command inside another process's namespaces. Here -n enters only the network namespace, and ip addr shows the container's network interfaces, using the host's own ip tool.

What Docker uses by default: new mount, PID, network, UTS, IPC and cgroup namespaces for every container. It does not use a user namespace by default, so root in a container is root on the host, held back only by the other protections in question 101. Question 142 covers turning user namespaces on.

What namespaces do not do: they hide things, but they do not limit how much a process can use. A process in its own namespaces can still use every CPU and all memory. That is the job of cgroups, question 100.

100. What are Linux control groups (cgroups), and how are they used to account for or limit resources? ​

The short answer

Cgroups are a kernel feature that groups processes and then measures and limits what the group uses: CPU time, memory, disk input and output, and the number of processes. Docker puts each container in its own cgroup. The kernel counts every byte and every millisecond the container uses, which is where docker stats gets its numbers, and enforces the limits you set with flags like -m and --cpus.

If namespaces decide what a process can see, cgroups decide how much it can use.

Each cgroup is a folder in a special filesystem at /sys/fs/cgroup. The files in the folder are both the readings and the controls. Modern systems use cgroup v2, where everything is in one tree:

bash
cat /sys/fs/cgroup/system.slice/docker-8f3c2a...scope/memory.max      # (1)
cat /sys/fs/cgroup/system.slice/docker-8f3c2a...scope/memory.current  # (2)
cat /sys/fs/cgroup/system.slice/docker-8f3c2a...scope/cpu.max         # (3)
cat /sys/fs/cgroup/system.slice/docker-8f3c2a...scope/pids.max        # (4)
536870912
214003712
150000 100000
512
  1. The memory limit, in bytes: 512 MB, from -m 512m.
  2. Memory in use right now: about 204 MB. This is accounting: the kernel counts all of it.
  3. The CPU limit: 150,000 microseconds of CPU time per 100,000-microsecond period, which is --cpus=1.5.
  4. The most processes the container may have at once, from --pids-limit 512.

The main controllers, each a kind of resource:

ControllerLimits or measures
memoryRAM use, with an OOM kill at the limit
cpuCPU time, as a quota and as a weight
cpusetWhich cores and memory nodes may be used
ioDisk read and write speed
pidsNumber of processes and threads

Why the pids limit matters. A fork bomb is a program that copies itself over and over until the machine cannot create any more processes, freezing everything on it. A per-container pids limit stops it inside that one container.

cgroup v1 and v2. The older v1 had a separate tree for every controller, which made consistent limits hard. v2 has one tree and better memory handling. Current Linux distributions use v2, and recent Kubernetes versions are moving away from v1 support.

101. How do capabilities, seccomp, and namespaces contribute to container isolation? ​

The short answer

They are separate layers of defence. Namespaces hide the rest of the system from the container. Capabilities split root's powers into small pieces, and Docker gives containers only a short list of them, so even root inside cannot, for example, load kernel modules or change the network. Seccomp is a filter on system calls, and Docker's default filter blocks the dangerous ones, such as reboot and some kinds of mount. Security modules such as AppArmor or SELinux add rules about files on top.

The figure shows the layers a container's request passes through before the kernel acts on it.

containerprocess① seccompwhich systemcalls are allowedblocks reboot,kexec, ...② capabilitieswhich rootpowers it holdsno SYS_ADMIN,no NET_ADMIN③ AppArmoror SELinuxrules about filesno writing to/proc/sys, ...④ kernelnamespaces:what it seescgroups:what it usesEach layer can say no. An attacker must get past all of them, not just one.
Layers of defence for every request. A system call must pass the seccomp filter, the capability check and the security module before the kernel does anything, and even then namespaces and cgroups limit its reach.

Namespaces (question 99) hide other processes, other networks and the host's files. They answer "what can this process see?"

Capabilities. Traditionally, Linux had two kinds of user: root, who could do everything, and everyone else. Capabilities split root's power into about forty separate permissions. For example, NET_BIND_SERVICE allows using ports below 1024, NET_ADMIN allows changing network settings, SYS_ADMIN allows a huge range of system operations including mounting filesystems, and SYS_MODULE allows loading code into the kernel.

Docker gives a container's root user only about fourteen of them, including CHOWN, KILL, SETUID, SETGID and NET_BIND_SERVICE. It removes the powerful ones such as SYS_ADMIN, NET_ADMIN, SYS_MODULE and SYS_PTRACE. You can tighten further:

bash
docker run --cap-drop ALL --cap-add NET_BIND_SERVICE nginx:1.27

This drops every capability and adds back only the one nginx needs to listen on port 80.

Seccomp, secure computing mode, is a filter on system calls, the requests a program makes to the kernel. There are more than three hundred of them. Docker's default profile blocks a few dozen that containers rarely need and that have been used in attacks, such as reboot, kexec_load, add_key, and mount for most cases. A blocked call fails with "operation not permitted".

AppArmor or SELinux, Linux security modules, add rules about which files and resources a process may touch. Docker's default AppArmor profile, for example, stops a container writing to sensitive parts of /proc and /sys.

Why several layers. Each one covers gaps in the others. A kernel bug might bypass one check, and the next layer can still stop the attack. That is defence in depth, the same idea as question 14. And --privileged turns most of these layers off at once, which is why it is so dangerous (question 141).

102. Why is a container not simply a lightweight virtual machine? ​

The short answer

Because a container has no virtual hardware and no kernel of its own. It is an ordinary process on the host, made to look isolated by namespaces and limited by cgroups. It runs on the host's kernel, it shows up in the host's process list, and without a user namespace its root user is the host's root user, held back only by capabilities and filters. A VM, by contrast, is a separate computer with its own kernel.

The phrase "lightweight VM" gives people the wrong idea, and the wrong idea leads to real mistakes. Here is what is actually different:

  1. There is no separate kernel. Run uname -r inside any container and you get the host's kernel version. A container built from an Ubuntu 24.04 image, running on a host with an older kernel, gets that older kernel. Only the files come from Ubuntu.
  2. It is visible from the host. ps aux on the host lists every container's processes, with host process numbers. A VM's processes are invisible to its host.
  3. Root can be real root. Without user namespaces, UID 0 in a container is UID 0 on the host. Only capabilities, seccomp and the security module stand between it and the host. A VM's root controls only its own virtual machine.
  4. It cannot change the kernel. A container cannot load its own kernel modules or use a different kernel version. A VM can run any operating system.
  5. Kernel bugs reach every container. One kernel flaw can be an escape route for every container on the machine. A VM's guest kernel bug stays inside that VM.
  6. It usually runs one program. A VM runs a whole operating system with an init system and many services. A container normally runs one app as process 1, without the system services a full OS would start.

The practical consequences:

  • Do not run untrusted code in ordinary containers. Use a sandboxed runtime or real VMs (question 20).
  • Do not treat a container like a server: no SSH daemon inside, no installing updates in place. Rebuild and replace instead.
  • Expect host-level effects: a container that fills the host's disk or exhausts the kernel's connection tracking table affects every other container.

A better short description: a container is a process with a private view of the system.

470. What are tmpfs mounts, and when would you use one? ​

The short answer

A tmpfs mount is a filesystem that lives entirely in memory. Files written there are fast to read and write, are never stored on disk, and disappear when the container stops. Use it for temporary scratch files, especially together with a read-only root filesystem, and for sensitive files that must never be written to disk. Remember that what you store there counts against the container's memory limit.

bash
# (1)
docker run -d --read-only --tmpfs /tmp:rw,size=64m,mode=1777 shop/api:1.4
# (2)
docker run -d --read-only --mount type=tmpfs,destination=/run/app,tmpfs-size=16777216,tmpfs-mode=0700 shop/api:1.4
  1. Mounts a 64 MB in-memory filesystem at /tmp. mode=1777 is the normal permission for /tmp: everyone can write, but people can only delete their own files.
  2. The same idea with the longer --mount syntax. Here the size is in bytes, 16 MB, and only the owner can enter the folder.

When to use it:

  1. With --read-only. Most apps need somewhere to write temporary files. A tmpfs at /tmp gives them that, without making the rest of the filesystem writable (question 96).
  2. For files that must never touch a disk, such as decrypted keys or temporary copies of secrets. When the container stops, the memory is freed and nothing is left behind for anyone to recover.
  3. For speed, such as a scratch area for heavy temporary work.

The catches:

  • It uses the container's memory. 50 MB in a tmpfs is 50 MB of memory in the container's cgroup. A tmpfs filling up can push the container into an OOM kill (question 97). Always set a size.
  • Nothing persists. Not even a restart keeps the contents.
  • Linux only. Windows containers do not support it.

In Kubernetes, the same thing is an emptyDir volume with medium: Memory.

Combine chapters into one PDF

Pick the chapters you want. They print in the order you pick them.

Interview Prep
Docker, CI/CD, Kubernetes & Cloud
Your project
Docker
CI/CD
Kubernetes
Cloud
Observability
Scenarios
Other topics
Nothing picked yet