When should the content in docker-compose.yml be quoted?

A Docker Compose file is a YAML file in which the quoting rules follow the YAML syntax rules. Here are a few scenarios where quotes are used in YAML:

  1. Quotes are required when a string contains special characters. These special characters include, but are not limited to: colon :, pound sign #, asterisk *, question mark ?, dash -, >, |etc. For example:

    environment:
      - "SOME_VAR=value:value"
    
  2. Quotes are required when strings contain YAML reserved words (such as true, false, null, yes, no, on, off) and you want them to be treated as strings rather than boolean or null values. For example:

    environment:
      - "SOME_VAR=true"
    
  3. Quotes are recommended when string values ​​start with numbers to avoid them being misinterpreted as other types. For example:

    environment:
      - "SOME_VAR=123abc"
    
  4. When the value of the string is JSON or other complex structures, in order to avoid parsing errors, it also needs to be wrapped in quotation marks. For example:

    labels:
      - "traefik.http.routers.my-container.rule=Host(`my-container.docker.localhost`)"
    

Note that both single and 'double quotes can be used in YAML ", but they behave differently. Single quotes preserve all special characters in the string, while double quotes allow escape sequences (such as \nfor newlines).

In general, although quotation marks are fine in many cases, in order to avoid possible parsing errors, it is recommended to always use quotation marks to wrap string values ​​when writing YAML files.

Guess you like

Origin blog.csdn.net/m0_57236802/article/details/131621349