Volumes (volumes) and bind mounts (bind mounts) allows you to share files between the host and the container, so that even after the container can also stop persistent storage of data.
If you are running Docker on Linux, there is a third option: tmpfs
mount. When you create with tmpfs
the container mount, the container can create files writable layer outside the container.
Unlike volumes and bind mounts, tmpfs
mounts are temporary and only remain in the host's memory. When the container stops, the tmpfs
mount will be deleted, and the files written there will not be persisted.
This is useful for temporarily storing sensitive files that you don't want to store persistently in the host or container writable layer.
Limitations of tmpfs mount
- Unlike volume and bind mounts, can not be shared between the containers
tmpfs
mounted. - This feature is only available when Docker is running on Linux.
Select --tmpfs
or --mount
mark
Initially, --tmpfs
tags were used for stand-alone containers, and --mount
tags were used for cluster services. But from Docker 17.06, you can also be --mount
used in conjunction with a separate container. Usually, the --mount
mark expression is more clear and verbose. The biggest difference is that the --tmpfs
tag does not support any configurable options.
--tmpfs
: Set thetmpfs
Mount does not allow you to specify any configuration options, and can only be used with a separate container.--mount
: A plurality of keys - each key value pairs ,, - value pair by one<key>=<value>
of tuples.--mount
The syntax than the--tmpfs
more verbose:- The type of mounting (
type
), which can be eitherbind
,volume
ortmpfs
. This topic is discussedtmpfs
, so the type (type
) is alwaystmpfs
. - Target (
destination
), the vesseltmpfs
path is provided to mount as its value. It can be useddestination
,dst
ortarget
specify. tmpfs-size
Andtmpfs-mode
options. See below a specified tmpfs options .
- The type of mounting (
The following example shows as simultaneously as possible --mount
and the --tmpfs
two syntaxes, and the first show --mount
.
--tmpfs
And --mount
the difference between the behavior
--tmpfs
The tag does not allow any configurable options to be specified.--tmpfs
The tag cannot be used for cluster services. For cluster services, you must use--mount
.
Mount using tmpfs in the container
To be used in the container tmpfs
mount, use the --tmpfs
mark, or with the use type=tmpfs
and destination
options --mount
mark. Not used for tmpfs
source mount ( source
).
The following example Nginx container /app
to create a tmpfs
mount. The first example uses a --mount
mark, using a second --tmpfs
marker.
--mount
:
$ docker run -d \
-it \
--name tmptest \
--mount type=tmpfs,destination=/app \
nginx:latest
--tmpfs
:
$ docker run -d \
-it \
--name tmptest \
--tmpfs /app \
nginx:latest
By running docker container inspect tmptest
to verify that the mount is tmpfs
mounted to view the Mounts
section:
"Tmpfs": {
"/app": ""
},
Delete the container:
$ docker container stop tmptest
$ docker container rm tmptest
Specify tmpfs options
tmpfs
Mounting allows two configuration options, neither of which is required. If you need to specify these options, you must use the --mount
mark, because the --tmpfs
mark is not supported.
Options | description |
---|---|
tmpfs-size |
The size of the tmpfs mount (in bytes). There is no limit by default. |
tmpfs-mode |
The octal file mode of tmpfs. For example, 700 or 0770 . Default 1777 or writable. |
The following example tmpfs-mode
is provided to 1770
, and therefore in the container it is not global readable.
docker run -d \
-it \
--name tmptest \
--mount type=tmpfs,destination=/app,tmpfs-mode=1770 \
nginx:latest
Author: Docker's official website
translator: Technical Zemin
Publisher: Technical Verses
links: English text