Docker Data Storage - "tmpfs" Mounts

Q

How to create a new "tmpfs" mount to a Docker container?

✍: FYIcenter.com

A

"tmpfs" mounts are stored in the host system’s memory only, and are never written to the host system’s filesystem.

You have two options "--tmpfs" and "--mount" on the "docker container create/run" command to create "tmpfs" mounts.

"--tmpfs" Option - Allows you to mount a "tmpfs" file system to the new Docker container with default configuration.

"--mount type=tmpfs,..." Option - Allows you to mount a "tmpfs" file system to the new Docker container with specific configurations.

For example, the following command "docker run --tmpfs" runs a new container with a new "tmpfs" filesystem mounted at /app.

fyicenter# docker run --tmpfs /app --tty --interactive --detach --name fyi_test alpine

fyicenter# docker exec --tty --interactive fyi_test /bin/sh

/ # df -h 
Filesystem                Size      Used Available Use% Mounted on
overlay                  50.0G     22.3G     27.7G  45% /
tmpfs                    64.0M         0     64.0M   0% /dev
tmpfs                     3.7G         0      3.7G   0% /sys/fs/cgroup
shm                      64.0M         0     64.0M   0% /dev/shm
tmpfs                     3.7G         0      3.7G   0% /app
/dev/mapper/cl-root      50.0G     22.3G     27.7G  45% /etc/resolv.conf
/dev/mapper/cl-root      50.0G     22.3G     27.7G  45% /etc/hostname
/dev/mapper/cl-root      50.0G     22.3G     27.7G  45% /etc/hosts
tmpfs                     3.7G         0      3.7G   0% /proc/asound
tmpfs                     3.7G         0      3.7G   0% /proc/acpi
tmpfs                    64.0M         0     64.0M   0% /proc/kcore
tmpfs                    64.0M         0     64.0M   0% /proc/keys
tmpfs                    64.0M         0     64.0M   0% /proc/timer_list
tmpfs                    64.0M         0     64.0M   0% /proc/sched_debug
tmpfs                     3.7G         0      3.7G   0% /proc/scsi
tmpfs                     3.7G         0      3.7G   0% /sys/firmware

/ # exit

fyicenter# docker container stop fyi_test
  fyi_test

fyicenter# docker container rm fyi_test
  fyi_test

As you can see, the "docker run --tmpfs /app" created a new "tmpfs" filesystem with 3.7G space. The "alpine" Docker has many other "tmpfs" filesystems running the Docker container.

If you want to create new "tmpfs" filesystem with less space, you need to use the "docker run --mount type=tmpfs,destination=..." option as shown below.

fyicenter# docker run --mount type=tmpfs,destination=/app,tmpfs-size=640m \
  -tid --name fyi_test alpine

fyicenter# docker exec -it fyi_test /bin/sh

/ # df -h
Filesystem                Size      Used Available Use% Mounted on
overlay                  50.0G     22.3G     27.7G  45% /
tmpfs                    64.0M         0     64.0M   0% /dev
tmpfs                     3.7G         0      3.7G   0% /sys/fs/cgroup
shm                      64.0M         0     64.0M   0% /dev/shm

tmpfs                   640.0M         0    640.0M   0% /app
...

/ # exit

fyicenter# docker container stop fyi_test
  fyi_test

fyicenter# docker container rm fyi_test
  fyi_test

Note that data stored in a "tmpfs" filesystem stays with the Docker's container only. When the container is removed, data will removed too.

If you want to keep data files in a "tmpfs" filesystem, you have two options:

  • Copy data files out from the container with the "docker cp" command.
  • Commit the container into a new Docker image. The "tmpfs" will stay in the new Docker image.

 

Docker Data Storage - Volume Mounts

Types of Docker Data Storage

Managing Data Storage in Docker

⇑⇑ Docker Container Platform - Tutorials

2021-08-15, 194👍, 0💬