🎨
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. Dockerfile指令
  2. Parser directives

escape

# escape=\ (backslash)

或者

# escape=` (backtick)

escape指令设置用于对Dockerfile中的字符进行转义的字符。如果未指定,则默认转义字符为\

转义字符用于转义一行中的字符和转义换行符。这允许Dockerfile指令跨越多行。请注意,无论Dockerfile中是否包含转义解析器指令,都不会在RUN命令中执行转义,除非在行末。

在Windows上将转义符设置成其他字符特别有用,其中\是目录路径分隔符 , Windows PowerShell一致。

考虑以下示例,该示例在Windows上将以非显而易见的方式失败。第二行末尾的第二个\将被解释为换行符的转义符,而不是第一行\的转义目标。同样,假设第三行末尾的\实际上是作为指令处理的,则将其视为行的延续。该dockerfile的结果是第二和第三行被视为一条指令:

FROM microsoft/nanoserver
COPY testfile.txt c:\ #这里的\会被当成行连续符
RUN dir c:\

结果:

PS C:\John> docker build -t cmd .
Sending build context to Docker daemon 3.072 kB
Step 1/2 : FROM microsoft/nanoserver
 ---> 22738ff49c6d
Step 2/2 : COPY testfile.txt c:\RUN dir c:
GetFileAttributesEx c:RUN: The system cannot find the file specified.
PS C:\John>

上述解决方案之一是将/用作COPY指令和dir的目标路径。但是,此语法容器让人产生困惑,因为Windows上的路径并不自然,而最糟糕的是,由于Windows上的所有命令都不支持/作为路径分隔符,因此容易出错。

通过添加escape解析器指令,以下Dockerfile会通过在Windows上使用平台指定的语义(也就是\)来做为文件路径分隔符使用而成功执行:

PS C:\John> docker build -t succeeds --no-cache=true .
Sending build context to Docker daemon 3.072 kB
Step 1/3 : FROM microsoft/nanoserver
 ---> 22738ff49c6d
Step 2/3 : COPY testfile.txt c:\
 ---> 96655de338de
Removing intermediate container 4db9acbb1682
Step 3/3 : RUN dir c:\
 ---> Running in a2c157f842f5
 Volume in drive C has no label.
 Volume Serial Number is 7E6D-E0F7

 Directory of c:\

10/05/2016  05:04 PM             1,894 License.txt
10/05/2016  02:22 PM    <DIR>          Program Files
10/05/2016  02:14 PM    <DIR>          Program Files (x86)
10/28/2016  11:18 AM                62 testfile.txt
10/28/2016  11:20 AM    <DIR>          Users
10/28/2016  11:20 AM    <DIR>          Windows
           2 File(s)          1,956 bytes
           4 Dir(s)  21,259,096,064 bytes free
 ---> 01c7f3bef04f
Removing intermediate container a2c157f842f5
Successfully built 01c7f3bef04f
PS C:\John>
PrevioussyntaxNextEnvironment replacement

Last updated 4 years ago

Was this helpful?