Last active
July 31, 2024 06:26
-
-
Save abn/a16e9d799312fb492861 to your computer and use it in GitHub Desktop.
Dockerfile alternatives for heredoc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#printf | |
RUN printf '#!/bin/bash\n\ | |
echo hello world from line 1\n\ | |
echo hello world from line 2'\ | |
>> /tmp/hello | |
#echo | |
RUN echo -e '#!/bin/bash\n\ | |
echo hello world from line 1\n\ | |
echo hello world from line 2'\ | |
>> /tmp/hello |
FROM debian
RUN <<EOT bash
set -eux
to_install=(
vim
)
apt-get update
failing_command # set -e exit with non-zero status
apt-get install -y "\${to_install[@]}"
EOT
# execute list shell
RUN <<EOF
apt-get update
apt-get upgrade -y
apt-get install -y ...
EOF
# run python
RUN <<EOF
#!/usr/bin/env python3
with open("/hello", "w") as f:
print("Hello", file=f)
print("World", file=f)
EOF
# create index.html
COPY <<EOF /usr/share/nginx/html/index.html
<html>hello, index</html>
EOF
# create /etc/mysql/my.cnf
RUN <<-EOF > /etc/mysql/my.cnf
[mysqld]
user=root
datadir=/app/mysql
port=3306
log-bin=/app/mysql/mysql-bin
EOF
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
kudos @maxx27! I was banging my head why in 2024 it still has to be so difficult to use heredocs in Dockerfiles in a reasonable way. That last snippet of yours is the way to go! Thanks!