bash - 如何将 “escape hell”传递给docker run

标签 bash powershell docker command-line cmd

我正在尝试从powershell \ cmd将几个命令传递到 Alpine 容器。问题是:命令具有引号\单引号\转义引号。

示例命令,实际命令为一行,我将其拆分,以便于查看:

docker run neilpang/acme.sh bin/sh -c --%
  "acme.sh --issue --dns dns_azure --dnssleep 10 --force -d "domain";
  openssl pkcs12 -inkey /acme.sh/domain/domain.key -in /acme.sh/domain/domain.cer -export -out pfx -password pass:password;
  curl -X POST -d "{\"certName\":\"certName1\",\"certData\":\"$(tail -n +2 /acme.sh/domain/domain.cer > b64; head -n -1 b64)\"}" url -H Content-Type:application/json;
  curl -X POST -d "{\"certName\":\"certName2\",\"certData\":\"$(openssl base64 -in pfx -out b64; cat b64)\"}" url -H Content-Type:application/json"

我尝试过的
  • 在整个表达式中使用单引号(在-%之后)返回类似于引号的字符串,但未关闭
  • 在json周围使用单引号可以很好地工作,但是$()不会得到解释,所以我没有得到正确的输出
  • jsont_li在json之前\之后转义" json之前/之后没有"
  • ,会导致出现垃圾
  • 在没有--%的情况下,我永远无法使它正常工作,但是即使使用--%
  • 时,ps似乎也会进行一些字符串操作
  • 用cmd做了一些实验,也没有运气。

  • 我愿意以任何方式使这项工作:)

    对于那些请求错误的示例,这里是一个示例:
    -n: line 1: syntax error: unexpected end of file (expecting ")") # quotes around expression
    -p: line 1: syntax error: unterminated quoted string # single quotes around expression
    no error, but certData empty # no quotes around json
    no quotes in json >> not valid json >> doesnt get parsed # escaped quotes around json
    

    执行我试图在容器内部传递的字符串会导致有效的“解决方案”,而尝试通过docker run传递字符串会导致上述各种缺陷。我不确定这些是哪里发生的,来自powershell(尽管我坚信--%在这里起作用,并且powershell正确传递了所有内容),docker或bash本身。我最接近工作的是'版本,但它不评估$()内部的表达式,因此出现了问题

    最佳答案

    如果将很长很复杂的命令行转换为Shell脚本,将其打包为自定义镜像,然后运行该脚本,就会发现引用问题变得更加容易。

    Shell脚本,我们称它为run_acme.sh:

    #!/bin/sh
    acme.sh --issue --dns dns_azure --dnssleep 10 --force -d "domain"
    openssl pkcs12 \
      -inkey /acme.sh/domain/domain.key \
      -in /acme.sh/domain/domain.cer \
      -export \
      -out pfx \
      -password pass:password
    CER_B64=$(tail -n +2 /acme.sh/domain/domain.cer | head -n -1)
    curl -X POST \
      --data-binary "{\"certName\":\"certName1\",\"certData\":\"$CER_B64\"}" \
      -H 'Content-Type: application/json'
      url
    PFX_B64=$(openssl base64 -in pfx)
    curl -X POST \
      --data-binary "{\"certName\":\"certName1\",\"certData\":\"$PFX_B64\"}" \
      -H 'Content-Type: application/json'
      url
    

    Dockerfile:
    FROM neilpang/acme.sh
    COPY run_acme.sh /bin
    CMD ["/bin/run_acme.sh"]
    

    然后,您可以像其他任何自定义图像一样对它进行docker build编码,并对其进行docker run编码,非常长的命令行将被烘焙到图像的shell脚本中。

    关于bash - 如何将 “escape hell”传递给docker run,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51848657/

    相关文章:

    docker - Docker Swarm创建真实集群:已注册和已删除

    bash - FFmpeg:将段传送到 s3

    bash - 在 Fish Shell 中设置导出

    bash - 如何终止所有子shell进程?

    .net - Powershell 中的点网表单未以 3D 形式显示

    tomcat - COPY 和 ADD 在 Dockerfile 中不起作用

    docker - Nginx docker compose - 卷添加 nginx.conf(反向代理)

    bash - 在 bash 中逐个字符地读取用户给定的文件

    java - 如何使用 Powershell 退出代码重新启动我关闭的客户端/服务器对?

    powershell - 将get-childitem传递到powershell中的选择字符串中