Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

docker - How to write commands with multiple lines in Dockerfile while preserving the new lines?

I want to write the following RUN command in the Dockerfile. But, docker is not preserving the new lines.

RUN echo "[repo] 
name            = YUM Repository 
baseurl         = https://example.com/packages/ 
enabled         = 1 
gpgcheck        = 0" > /etc/yum.repos.d/Repo.repoxyz

I know that at the end of each line escapes the new line. But, is there any way that I can write multiple lines preserving the new line?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use what is called "ANSI-C quoting" with $'...'. It was originally a ksh93 feature but it is now available in bash, zsh, mksh, FreeBSD sh and in busybox's ash (but only when it is compiled with ENABLE_ASH_BASH_COMPAT).

As RUN uses /bin/sh as shell by default you are required to switch to something like bash first by using the SHELL instruction.

Start your command with $', end it with ' and use for newlines, like this:

SHELL ["/bin/bash", "-c"]

RUN echo $'[repo] 

name            = YUM Repository 

baseurl         = https://example.com/packages/ 

enabled         = 1 

gpgcheck        = 0' > /etc/yum.repos.d/Repo.repoxyz

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...