Docker basics-use tmpfs mounts to manage application data

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: tmpfsmount. When you create with tmpfsthe container mount, the container can create files writable layer outside the container.

Unlike volumes and bind mounts, tmpfsmounts are temporary and only remain in the host's memory. When the container stops, the tmpfsmount will be deleted, and the files written there will not be persisted.

docker-types-of-mounts-tmpfs

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 tmpfsmounted.
  • This feature is only available when Docker is running on Linux.

Select --tmpfsor --mountmark

Initially, --tmpfstags were used for stand-alone containers, and --mounttags were used for cluster services. But from Docker 17.06, you can also be --mountused in conjunction with a separate container. Usually, the --mountmark expression is more clear and verbose. The biggest difference is that the --tmpfstag does not support any configurable options.

  • --tmpfs: Set the tmpfsMount 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. --mountThe syntax than the --tmpfsmore verbose:
    • The type of mounting ( type), which can be either bind, volumeor tmpfs. This topic is discussed tmpfs, so the type ( type) is always tmpfs.
    • Target ( destination), the vessel tmpfspath is provided to mount as its value. It can be used destination, dstor targetspecify.
    • tmpfs-sizeAnd tmpfs-modeoptions. See below a specified tmpfs options .

The following example shows as simultaneously as possible --mountand the --tmpfstwo syntaxes, and the first show --mount.

--tmpfsAnd --mountthe difference between the behavior

  • --tmpfs The tag does not allow any configurable options to be specified.
  • --tmpfsThe 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 tmpfsmount, use the --tmpfsmark, or with the use type=tmpfsand destinationoptions --mountmark. Not used for tmpfssource mount ( source).

The following example Nginx container /appto create a tmpfsmount. The first example uses a --mountmark, using a second --tmpfsmarker.

--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 tmptestto verify that the mount is tmpfsmounted to view the Mountssection:

"Tmpfs": {
    
    
    "/app": ""
},

Delete the container:

$ docker container stop tmptest

$ docker container rm tmptest

Specify tmpfs options

tmpfsMounting allows two configuration options, neither of which is required. If you need to specify these options, you must use the --mountmark, because the --tmpfsmark 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, 700or 0770. Default 1777or writable.

The following example tmpfs-modeis 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

Guess you like

Origin blog.csdn.net/weixin_47498376/article/details/107754163