* Fixed syntax error (#8732) Last edit to the REPLICA_ID command introduced a syntax error by adding an extra ')'. Removed it. * Fix replica ID setting examples - Accept suggestion from @thajeztah based on product testing - Apply change to page examples - Remove NFS backup example based on the following errors: tar: /var/lib/docker/volumes/dtr-registry-nfs-36e6bf87816d: Cannot stat: No such file or directory tar: Exiting with failure status due to previous errors * Update header for example tar * Fixed link title * Added new example and deprecation info (#8773) * Updated multi-stage build doc (#8769) Changed the 'as' keyword to 'AS' to match the Dockerfile reference docs here: https://docs.docker.com/engine/reference/builder/#from * Fix typo (#8766) * Fixed a sentence (#8728) * Minor edit * Update configure-tls.md (#8719) * Update upgrade.md (#8718) * Update index.md (#8717) * Update configure-tls.md (#8716) * Add TOC entry for Hub page title change (#8777) * Update upgrade.md * Fix left navigation TOC * Update get-started.md (#8713) * Update tmpfs.md (#8711) * Add an indentation in compose-gettingstarted.md (#8487) * Fix messaging on service dependencies
4.9 KiB
description, title, keywords, redirect_from
| description | title | keywords | redirect_from | |
|---|---|---|---|---|
| Using tmpfs mounts | Use tmpfs mounts | storage, persistence, data persistence, tmpfs |
|
Volumes and bind mounts let you share files between the host machine and container so that you can persist data even after the container is stopped.
If you're running Docker on Linux, you have a third option: tmpfs mounts.
When you create a container with a tmpfs mount, the container can create
files outside the container's writable layer.
As opposed to volumes and bind mounts, a tmpfs mount is temporary, and only
persisted in the host memory. When the container stops, the tmpfs mount is
removed, and files written there won't be persisted.
This is useful to temporarily store sensitive files that you don't want to persist in either the host or the container writable layer.
Limitations of tmpfs mounts
- Unlike volumes and bind mounts, you can't share
tmpfsmounts between containers. - This functionality is only available if you're running Docker on Linux.
Choose the --tmpfs or --mount flag
Originally, the --tmpfs flag was used for standalone containers and
the --mount flag was used for swarm services. However, starting with Docker
17.06, you can also use --mount with standalone containers. In general,
--mount is more explicit and verbose. The biggest difference is that the
--tmpfs flag does not support any configurable options.
-
--tmpfs: Mounts atmpfsmount without allowing you to specify any configurable options, and can only be used with standalone containers. -
--mount: Consists of multiple key-value pairs, separated by commas and each consisting of a<key>=<value>tuple. The--mountsyntax is more verbose than--tmpfs:- The
typeof the mount, which can bebind,volume, ortmpfs. This topic discussestmpfs, so the type is alwaystmpfs. - The
destinationtakes as its value the path where thetmpfsmount is mounted in the container. May be specified asdestination,dst, ortarget. - The
tmpfs-typeandtmpfs-modeoptions. See tmpfs options.
- The
The examples below show both the --mount and --tmpfs syntax where possible,
and --mount is presented first.
Differences between --tmpfs and --mount behavior
- The
--tmpfsflag does not allow you to specify any configurable options. - The
--tmpfsflag cannot be used with swarm services. You must use--mount.
Use a tmpfs mount in a container
To use a tmpfs mount in a container, use the --tmpfs flag, or use the
--mount flag with type=tmpfs and destination options. There is no
source for tmpfs mounts. The following example creates a tmpfs mount at
/app in a Nginx container. The first example uses the --mount flag and the
second uses the --tmpfs flag.
--mount--tmpfs
$ docker run -d \
-it \
--name tmptest \
--mount type=tmpfs,destination=/app \
nginx:latest
$ docker run -d \
-it \
--name tmptest \
--tmpfs /app \
nginx:latest
Verify that the mount is a tmpfs mount by running docker container inspect tmptest and looking for the Mounts section:
"Tmpfs": {
"/app": ""
},
Remove the container:
$ docker container stop tmptest
$ docker container rm tmptest
Specify tmpfs options
tmpfs mounts allow for two configuration options, neither of which is
required. If you need to specify these options, you must use the --mount flag,
as the --tmpfs flag does not support them.
| Option | Description |
|---|---|
tmpfs-size |
Size of the tmpfs mount in bytes. Unlimited by default. |
tmpfs-mode |
File mode of the tmpfs in octal. For instance, 700 or 0770. Defaults to 1777 or world-writable. |
The following example sets the tmpfs-mode to 1770, so that it is not
world-readable within the container.
docker run -d \
-it \
--name tmptest \
--mount type=tmpfs,destination=/app,tmpfs-mode=1770 \
nginx:latest
Next steps
- Learn about volumes
- Learn about bind mounts
- Learn about storage drivers
