🎨
Docker
  • Docker入门
  • Docker overview
    • 隔离与限制
    • copy-on-write(CoW: 写时复制)
    • docker中的联合文件系统
    • Docker存储
      • tmpfs mounts
      • volume
      • bind mounts
    • Docker网络
      • host模式
      • bridge模式
        • default bridge模式
        • User-defined bridge模式
      • none模式
      • container模式
  • 容器监控方式
    • prometheus + grafana+cAdvisor监控容器
  • Dockerfile指令
    • Usage
    • BuildKit
    • Format
    • Parser directives
      • syntax
      • escape
    • Environment replacement
    • FROM
      • 了解ARG和FROM之间的交互方式
    • .dockerignore file
    • RUN
    • CMD
    • COPY
    • ADD
    • ENV
    • WORKDIR
    • ARG
      • Impact on build caching(对构建缓存的影响)
      • Default(默认值)
      • Scope(作用域)
      • Using ARG Variables(使用ARG变量)
      • Predefined ARGS(预先定义好的ARG变量)
      • Automatic platform ARGs in the global scope(全局作用域中自动化平台的ARGS)
    • STOPSIGNAL
    • HEALTHCHECK
    • SHELL
    • ONBUILD
    • Dockerfile示例
    • LABEL
    • MAINTAINER (deprecated)
    • EXPOSE
    • ENTRYPOINT
      • Shell形式的ENTRYPOINT示例
      • 理解CMD和ENTRYPOINT是如何交互的
    • VOLUME
    • USER
  • docker-compose.yaml文件中常用指令
    • compose文件结构和示例
    • 服务配置参考
      • build
        • dockerfile
        • context
        • shm_size
        • network
        • labels
        • cache_from
        • args
        • target
      • cap_add, cap_drop
      • cgroup_parent
      • command
      • configs
        • 短语法形式
        • 长语法形式
      • container_name
      • credential_spec
        • EXAMPLE GMSA CONFIGURATION
      • depends_on
      • deploy
        • endpoint_mode
        • labels
        • mode
        • placement
        • max_replicas_per_node
        • replicas
        • resource
        • restart_policy
        • rollback_config
        • Update_config
        • Not supported for docker stack deploy
      • devices
      • dns
      • dns_search
      • entrypoint
      • env_file
      • environment
      • expose
      • external_links
      • extra_hosts
      • healthcheck
      • image
      • init
      • isolation
      • labels
      • links
      • logging
      • network_mode
      • networks
        • alias
        • IPV4_ADDRESS, IPV6_ADDRESS
      • pid
      • ports
        • 长语法
        • 短语法
      • profiles
      • restart
      • secrets
        • 长语法
        • 短语法
      • security_opt
      • stop_grace_period
      • stop_signal
      • sysctls
      • tmpfs
      • ulimits
      • userns_mode
      • volumes
        • 长语法
        • 短语法
        • VOLUMES FOR SERVICES, SWARMS, AND STACK FILES
      • domainname, hostname, ipc, mac_address, privileged, read_only, shm_size, stdin_open, tty, user, work
      • Specifying durations
      • Specifying byte values
      • Volume configuration reference
        • driver
        • driver_opts
        • external
        • labels
        • name
      • Network configuration reference
        • name
        • external
        • labels
        • internal
        • ipam
        • enable_ipv6
        • driver
          • overlay
          • bridge
          • host或者none
        • driver_opts
        • attachable
      • configs configuration reference
      • secrets configuration reference
      • Variable substitution
      • Extension fields
  • docker-compose示例
    • WordPress
    • PostgreSQL
    • Django和PostgreSQL
    • Rails和PostgreSQL
  • Dockerfile最佳实践
Powered by GitBook
On this page
  • tmpfs挂载的限制
  • 选择--tmpfs还是--mount
  • --tmpfs和--mount行为的不同
  • 在一个容器中使用tmpfs挂载
  • 指定tmpfs选项

Was this helpful?

  1. Docker overview
  2. Docker存储

tmpfs mounts

PreviousDocker存储Nextvolume

Last updated 4 years ago

Was this helpful?

卷和bind mounts让我们可以在主机和容器之间共享文件并且可以在容器停止以后持久化数据。

如果我们在Linux主机上运行Docker,那么我们有第三个选项:tmpfs挂载。当我们使用tmpfs挂载创建一个容器,容器可以在容器的可写层外部创建文件。

与卷和bind mounts相反,tmpfs挂载是临时的,它只将文件持临时保存在主机内存中。当容器停止时,tmpfs挂载会被移除,写入的文件不会持久化保存。

这对于临时性存储的文件非常有用,我们既不需要持久化保存它们,也不需要在容器的可写层保存它们。

tmpfs挂载的限制

  • 不像卷和bind mounts那样,我们无法在容器之间共享tmpfs挂载。

  • 这个功能只能在Linux系统主机上使用。

选择--tmpfs还是--mount

通常--mount更加的详细和明确。它们之间最大的不同是,tmpfs不支持指定的多多可配置的选项。

  • --tmpfs:挂载一个tmpfs挂载不允许你指定任何可配置的选项,并且只能配合单独的容器使用。

  • --mount:由多个键值对组成,通过逗号分隔并且以<key>=<value>形式指定内容。

    • 挂载的类型可以是bind, volume或tmpfs。

    • destination的值是容器中需要被挂载到tmfps上的路径。可以通过destination,dst或target来指定。

    • 可以使用tmpfs-size和tmpfs-mode选项。

--tmpfs和--mount行为的不同

  • --tmpfs不允许指定任何可配置的选项。

  • --tmpfs无法用于swarm服务。必须使用--mount。

在一个容器中使用tmpfs挂载

为了使用tmpfs挂载,通过--tmpfs或者--mount来指定type=tmpfs以及destination选项。对于tmpfs挂载来说没有source。下面的示例在一个nginx容器的/app路径上创建了一个tmpfs挂载。第一个示例使用--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

通过docker inspect devtest来检查tmpfs挂载信息。查看Mounts部分:

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

移除容器:

$ docker container stop tmptest

$ docker container rm tmptest

指定tmpfs选项

tmpfs挂载允许两个配置选项,这两个选项都不是必须的。如果你需要指定这些选项,我们必须使用--mount,因为--tmpfs不支持它们。

选项

描述

tmpfs-size

字节形式的tmpfs挂载大小。默认没有限制。

tmpfs-mode

八进制形式的tmpfs文件模式。例如700或0700.默认为1777或world-writable。

以下示例设置tmpfs-mode为1770,以便在容器中它不是word-readable的。

docker run -d \
  -it \
  --name tmptest \
  --mount type=tmpfs,destination=/app,tmpfs-mode=1770 \
  nginx:latest