如何在Dockerfile中将多行追加到文件中?

13

我有一个dockerfile,但似乎无法将nginx配置文件嵌入其中,以便将其附加到/etc/nginx/nginx.conf。

我尝试了以下格式:

RUN cat <<EOT >> /etc/nginx/nginx.conf
user                            www;
worker_processes                auto; # it will be determinate automatically by the number of core

error_log                       /var/log/nginx/error.log warn;
pid                             /var/run/nginx.pid; # it permit you to use /etc/init.d/nginx reload|restart|stop|start

events {
    worker_connections          1024;
}

http {
    include                     /etc/nginx/mime.types;
    default_type                application/octet-stream;
    sendfile                    on;
    access_log                  /var/log/nginx/access.log;
    keepalive_timeout           3000;
    server {
        listen                  80;
        root                    /usr/local/www;
        index                   index.html index.htm;
        server_name             localhost;
        client_max_body_size    32m;
        error_page              500 502 503 504  /50x.html;
        location = /50x.html {
              root              /var/lib/nginx/html;
        }
    }
}
EOT

RUN echo $
'user                            www; \n
worker_processes                auto; # it will be determinate automatically by the number of core \n

error_log                       /var/log/nginx/error.log warn; \n
pid                             /var/run/nginx.pid; # it permit you to use /etc/init.d/nginx reload|restart|stop|start \n

events { \n
    worker_connections          1024; \n
} \n

http { \n
    include                     /etc/nginx/mime.types; \n
    default_type                application/octet-stream; \n
    sendfile                    on; \n
    access_log                  /var/log/nginx/access.log; \n
    keepalive_timeout           3000; \n
    server { \n
        listen                  80; \n
        root                    /usr/local/www; \n
        index                   index.html index.htm; \n
        server_name             localhost; \n
        client_max_body_size    32m; \n
        error_page              500 502 503 504  /50x.html; \n
        location = /50x.html { \n
              root              /var/lib/nginx/html; \n
        } \n
    } \n
}' 
> /etc/nginx/nginx.conf

然而,使用这两个示例之一时,我会收到以下错误,看起来像是Docker试图将nginx配置文件视为其自己的变量:

Sending build context to Docker daemon 33.28 kB
Error response from daemon: Unknown instruction: WORKER_PROCESSES

Docker版本为1.13.1,构建版本为07f3374/1.13.1,我使用的发行版是CentOS Atomic Host 7.1902,Docker基础镜像为AlpineLinux。

谢谢


4
为什么不直接把这个放进一个文件里然后用COPY命令呢? - DannyB
你尝试过不同的行结尾吗?我记得在Windows机器上遇到了这个问题。 - AirLancer
但为什么呢?也许你正在将 Dockerfile 粘贴到 Gist 中,而你并没有多个文件的奢侈条件。或者你正在一个项目中工作,在这个项目中 Dockerfile 是一个辅助功能,并且添加更多文件会污染根目录或需要创建一个新目录来存放这些文件。你可以使用 wget 下载一个 Dockerfile,但是你不能使用 wget 下载一个 Dockerfile 和它所有的文件(除非你有额外的知识)。也许你想在文档中提供一个说明最小化解决方案的示例;需要多个文件会显著复杂化描述。 - Jason R. Coombs
2个回答

18

那应该就可以了:

RUN echo $'first line \n\
second line \n\
third line' > /etc/nginx/nginx.conf

基本上,它被包裹在$''中,并使用\n\进行换行。


我很高兴能够帮助! - Thomas Baier

1

我想创建并追加行到我的.npmrc文件中,以安装私有包。唯一对我有效的语法是:

RUN echo @myscope:registry=https://gitlab.com/api/v4/packages/npm/ > .npmrc \
  && echo //gitlab.com/api/v4/packages/npm/:_authToken=${MY_TOKEN} >> .npmrc \
  && echo strict-ssl=false >> .npmrc

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接