簡體   English   中英

在 docker-compose 命令中使用內聯注釋

[英]Use inline comments in docker-compose command

在我的docker-compose.yml ,我需要使用一個很長的command ,並且我想記錄它。 這不容易做到,但我有一個幾乎可以工作的解決方法。

foo:                                    # valid comment
  image: foo:latest                     # valid comment
  command: >
           printf '
           something                    # explanation for this command
           --arg                        # explanation for this switch
           --a                          # explanation
           --b hello
           -c                           # this does...
           --d spam                     # don't use this when...
           #some notes
           --e ham                      # hmmm
           --eggs                       # explanation
           ' |grep -v ^[[:space:]]*$ |grep -v ^# |cut -d# -f1   # valid comment
  restart: always                       # valid comment

所以每個命令和開關都可以被注釋。

bash:

  • printf '... '將所有內容打印為文本,作為命令運行
  • grep -v ^[[:space:]]*$忽略第一個空行
  • grep -v ^#忽略注釋行
  • cut -d# -f1從每一行中去除內聯注釋

這個技巧在 shell 中完美運行!

然而docker-compose up說:

錯誤:服務“foo”中“command”選項的插值格式無效:“printf ' something...

如果我將$轉義為$$它會說:

錯誤:對於 foo 沒有結束引號

我怎樣才能讓它工作?

這是一個更好的方法。 command文檔沒有顯示它,但我認為它是標准的 YAML,所以它是允許的。

foo:                               # valid comment
  image: foo:latest                # valid comment
  command:
    - something                    # explanation for this command
    - --arg                        # explanation for this switch
    - --a                          # explanation
    - --b hello
    - -c                           # this does...
    - --d spam                     # don't use this when...
    #some notes
    - --e ham                      # hmmm
    - --eggs                       # explanation
  restart: always                  # valid comment

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM