如何在保留新行的同时在Dockerfile中编写多行命令? [英] How to write commands with multiple lines in Dockerfile while preserving the new lines?

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

问题描述

我想在Dockerfile中编写以下RUN命令。但是,docker不会保留新行。

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

我知道每行末尾的\ 会换行。但是,有什么方法可以写多行来保存新行?

解决方案

您可以使用所谓的 ANSI -C用 $'...'引用。 它最初是 ksh93 功能,但现在可以在 bash,zsh,mksh,FreeBSD sh busybox的灰中使用(但只有在使用ENABLE_ASH_BASH_COMPAT编译时才可以。)



默认情况下,RUN使用 / bin / sh 作为外壳程序,您需要先使用SHELL指令切换到bash之类的方式。 / p>

$'开始命令,以'并使用 \n\ 换行,例如:

  SHELL [ / bin / bash, -c] 

RUN echo $'[repo] \n\
名称= YUM存储库\n\
baseurl = https://example.com/packages/已启用
= 1已启用
gpgcheck = 0'> /etc/yum.repos.d/Repo.repoxyz


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?

解决方案

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 \n\ for newlines, like this:

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

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆