🎨
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

Was this helpful?

  1. docker-compose.yaml文件中常用指令
  2. 服务配置参考

Variable substitution

配置选项可以包含环境变量。 Compose使用运行docker-compose的shell环境中的变量值。例如,假设shell包含POSTGRES_VERSION=9.3,并且我们提供了以下配置:

db:
  image: "postgres:${POSTGRES_VERSION}"

当使用此配置运行docker-compose时,Compose在shell程序中查找POSTGRES_VERSION环境变量并替换其值。对于本示例,Compose在运行配置之前将映像镜像解析为postgres: 9.3。

如果未设置环境变量,则Compose替换为空字符串。在上面的示例中,如果未设置POSTGRES_VERSION,则image选项的值为postgres:。

我们可以使用.env文件设置环境变量的默认值,该文件会自动在项目目录(Compose文件的父文件夹)中查找。在shell环境中设置的值将覆盖.env文件中设置的值。

.env文件功能仅在使用docker-compose up命令时有效,而不适用于docker stack deploy。

$VARIABLE和${VARIABLE}语法均受支持。此外,当使用2.1文件格式时,可以使用Shell语法提供内联默认值:

  • 如果在当前环境中,VARIABLE的值没有设置或者为空值,${VARIABLE:-default}被计算为default。

  • 只有当VARIABLE的值在当前环境中没有设置时,${VARIABLE-default}会被计算为default。

同样,以下语法允许我们指定必要变量:

  • 如果在当前环境中,VARIABLE的值没有设置或者为空,则${VARIABLE:?err}会以指定的err错误消息退出。

  • 如果当前环境中,VARIABLE的值没有设置,则${VARIABLE?err}会以指定的err错误消息退出。

其他扩展的shell风格特性, 例如像${VARIABLE/foo/bar}并不支持。

当我们的配置需要一个字面量的$符号时,可以使用一个$$符号。这种写法也可以防止Compose内插一个值,因此,$$允许我们引用一个不想被Compose处理的环境变量。

web:
  build: .
  command: "$$VAR_NOT_INTERPOLATED_BY_COMPOSE"

如果没有使用$$符号,而是使用了$符号,Compose会将该其作为环境变量进行解释,然后发出一个警告:

The VAR_NOT_INTERPOLATED_BY_COMPOSE is not set. Substituting an empty string.
Previoussecrets configuration referenceNextExtension fields

Last updated 4 years ago

Was this helpful?