简体   繁体   English

Dockerfile 中带有 sudo 的多行 heredoc

[英]Multiline heredoc with sudo in Dockerfile

We use our local repo in sources.list, and for 20.04 it required to add in apt.conf.d Acquire::https::local_repo.local_host.com::Verify-Peer "false";我们在 sources.list 中使用我们的本地仓库,对于 20.04,它需要添加apt.conf.d Acquire::https::local_repo.local_host.com::Verify-Peer "false";

With Bash it works, as using,使用 Bash 它可以使用,

sudo tee -a /etc/apt/apt.conf.d/80ssl-exceptions > /dev/null <<'EOF'
Acquire::https::local_repo.local_host.com::Verify-Peer "false";
EOF

But I don't find a solution to do it for Dockerfile.但我没有为 Dockerfile 找到解决方案。 I've tried it with different escape character/new line and so on, but always unsuccessful.我已经尝试过使用不同的转义字符/换行符等,但总是不成功。 For example,例如,

sudo tee -a /etc/apt/apt.conf.d/80ssl-exceptions > /dev/null <<'EOF' \
Acquire::https::local_repo.local_host.com::Verify-Peer "false"; \
EOF

Results - /bin/sh: 1: EOF: not found结果 - /bin/sh: 1: EOF: not found

To note that cat or echo is not an option, also adding those 3 line in a script is also not preferable.要注意catecho不是一个选项,在脚本中添加这 3 行也不可取。

If you only have one line to append then I wouldn't use a heredoc.如果你只有一行 append 那么我不会使用heredoc。 It's simpler to use echo :使用echo更简单:

RUN echo 'Acquire::https::local_repo.local_host.com::Verify-Peer "false";' | \
        sudo tee -a /etc/apt/apt.conf.d/80ssl-exceptions > /dev/null

Or cat :cat

RUN cat <<< 'Acquire::https::local_repo.local_host.com::Verify-Peer "false";' | \
        sudo tee -a /etc/apt/apt.conf.d/80ssl-exceptions > /dev/null

Or send the string directly to sudo tee :或将字符串直接发送到sudo tee

RUN sudo tee -a /etc/apt/apt.conf.d/80ssl-exceptions > /dev/null \
        <<< 'Acquire::https::local_repo.local_host.com::Verify-Peer "false";'

Note that the latter two options may require you to also set SHELL /bin/bash since <<< is a bash-ism not available in plain sh .请注意,后两个选项可能还需要您设置SHELL /bin/bash因为<<<是普通sh中不可用的 bash-ism。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM