本文介绍了如何在 Dockerfile 中编写多行命令,同时保留新行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Dockerfile 中编写以下 RUN 命令.但是,docker 并没有保留新行.

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?

推荐答案

您可以使用所谓的ANSI-C 引用"与 $'...'.它最初是 ksh93 功能,但现在可以在 bash、zsh、mksh、FreeBSD shbusybox 的 ash 中使用(但仅当它使用 ENABLE_ASH_BASH_COMPAT 编译时).

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

作为 RUN 使用 /bin/sh 作为外壳默认你需要先使用SHELL指令切换到bash之类的东西.

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

这篇关于如何在 Dockerfile 中编写多行命令,同时保留新行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 00:53