Docker Community Forums

Share and learn in the Docker community.

Dockerfile to append multi-lines config file

Hello everyone,

I have a simple dockerfile to automate nginx deployment, and I can’t seem to be able to embed the nginx configuration file into it, so that it can be appended to /etc/nginx/nginx.conf.

I tried the following formats:

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

and

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

However with either of the two examples I get the following error, which kinda looks like docker is trying to treat the nginx configuration file as its own variables:

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

Docker version is 1.13.1, build 07f3374/1.13.1 and the distro I am using is CentOS Atomic Host 7.1902, while docker base image is alpinelinux.

Thanks

You can write multiline strings as

RUN echo "line 1\n"\
  "line 2"\
  >> target

which is quite tedious. So why don’t use COPY to either copy the complete config to /etc/nginx/nginx.conf or to copy a part to /tmp and then append this to the config with RUN?

Thanks a lot :slight_smile:

This suggestions don’t seem to work any longer. Tried both variants without success

This is the way!

The Dockerfile spec does not cover here-docs. Commands wrapping the line ending need to use the \ chraracter, which breaks here-docs.