命令运行正常...但是在 docker RUN 内部“传输”到 grep 时出现错误?

命令运行正常...但是在 docker RUN 内部“传输”到 grep 时出现错误?

抱歉,标题我实在无法解释得更好。我正在学习 shell 编程。

我正在尝试检测作曲家有一个名为“post-install-cmd”的脚本。要执行此操作,可以调用composer run-script --list文档)因此我将该命令通过管道传输到grep -q条件语句内部。

当我在 Docker 层(基于 Debian 的发行版)中运行此程序时,只有在以下情况下才会出现错误:composer run-script --list条件内调用

RUN set -eux; \
    if [ -f composer.json ]; then \
        composer dump-autoload --no-dev --classmap-authoritative; \
        # This is working fine, a list is returned (see log)
        composer run-script --list; \
        # This gives me an error (Unable to write output)
        if composer run-script --list | grep -q post-install-cmd; then \
            echo "Script was found!"; \
        fi; \
    fi

日志:

#41 [myproject php_prod  9/10] RUN set -eux;     if [ -f composer.json ]; then         composer dump-autoload --no-dev --classmap-authoritative;         composer run-script --list;         if composer run-script --list | grep -q post-install-cmd; then             echo "Script was found!";         fi;     fi
#41 0.441 + [ -f composer.json ]
#41 0.445 + composer dump-autoload --no-dev --classmap-authoritative
#41 0.835 Generating optimized autoload files (authoritative)
#41 0.878 Generated optimized autoload files (authoritative) containing 40 classes
#41 0.892 + composer run-script --list
#41 1.164 scripts:
#41 1.168   auto-scripts      Runs the auto-scripts script as defined in composer.json.  
#41 1.169   post-install-cmd                                                             
#41 1.169   post-update-cmd                                                              
#41 1.183 + grep -q post-install-cmd
#41 1.188 + composer run-script --list
#41 1.468 scripts:
#41 1.471 
#41 1.479                                                           
#41 1.480   [Symfony\Component\Console\Exception\RuntimeException]  
#41 1.481   Unable to write output.                                 
#41 1.481                                                           
#41 1.481 
#41 1.482 run-script [--timeout TIMEOUT] [--dev] [--no-dev] [-l|--list] [--] [<script>] [<args>]...
#41 1.482 
#41 1.495 + echo Script was found!
#41 1.497 Script was found!
#41 DONE 1.6s

答案1

--no-ansi您可以尝试通过在 composer run-script 命令中添加选项来禁用 ANSI 转义码的输出格式--list。这有时可以修复不支持输出的终端环境中的输出相关问题。

RUN set -eux; \
    if [ -f composer.json ]; then \
        composer dump-autoload --no-dev --classmap-authoritative; \
        composer run-script --list; \
        if composer run-script --list --no-ansi | grep -q post-install-cmd; then \
            echo "Script was found!"; \
        fi; \
    fi

或者:

docker run -t my_image

答案2

我还通过运行以下命令在构建步骤中运行:

echo "$(composer show)" | grep myvendor/mypackage

引号"是为了避免字段分裂(将命令全部放在一行,这不是您想要的)。 图片来源:https://unix.stackexchange.com/a/170726/177624

更多信息请参见字段拆分@seehttps://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_05

相关内容