Search code examples
bashdockerfileheredoc

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";

With Bash it works, as using,

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. 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

To note that cat or echo is not an option, also adding those 3 line in a script is also not preferable.


Solution

  • If you only have one line to append then I wouldn't use a heredoc. It's simpler to use 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:

    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:

    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.