Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to append multi-lines to file in a dockerfile?

I have a dockerfile and can't seem to be able to embed the nginx configuration file to 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 config 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

like image 243
mailman Avatar asked Apr 29 '19 15:04

mailman


People also ask

Can we add multiple from in Dockerfile?

With multi-stage builds, you use multiple FROM statements in your Dockerfile. Each FROM instruction can use a different base, and each of them begins a new stage of the build. You can selectively copy artifacts from one stage to another, leaving behind everything you don't want in the final image.

What is && in Dockerfile?

Each RUN command in a Dockerfile creates a new layer to the Docker image. In general, each layer should try to do one job and the fewer layers in an image the easier it is compress. This is why you see all these '&& 's in the RUN command, so that all the shell commands will take place in a single layer.

How do I comment multiple lines in Dockerfile?

Dockerfile does not provide district multiline comments. The single-line comment sign, hash mark, can be used to create multiline comments in Dockerfile. Just put multiple lines of comments one after the others. This creates a multiline comment.

Can one Dockerfile build multiple images?

One approach to keeping Docker images small is using multistage builds. A multistage build allows you to use multiple images to build a final product. In a multistage build, you have a single Dockerfile, but can define multiple images inside it to help build the final image.


2 Answers

That should do the trick:

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

Basically it's wrapped in a $'' and uses \n\ for new lines.

like image 173
Thomas Baier Avatar answered Oct 30 '22 13:10

Thomas Baier


I was looking to create & append lines to my .npmrc to install private packages. The only syntax that worked for me was:

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
like image 31
Nikhil Nanjappa Avatar answered Oct 30 '22 13:10

Nikhil Nanjappa