'RUN echo -e "deb http ... " prepares a wrong contents of the target file

Host with Ubuntu 16.04.2.

Docker version 17.06.0-ce.

Dockerfile

RUN echo -e "deb http://nginx.org/packages/mainline/ubuntu/ xenial nginx\ndeb-src http://nginx.org/packages/mainline/ubuntu/ xenial nginx" | tee /etc/apt/sources.list.d/nginx.list

This results in in /etc/apt/sources.list.d/nginx.list:

-e deb http://nginx.org/packages/mainline/ubuntu/ xenial nginx
deb-src http://nginx.org/packages/mainline/ubuntu/ xenial nginx

Well, as if interpretation of backslash escapes works. But this "-e" has occurred in the file.

And then such image can't be built.

Could you give me a kick here?



Solution 1:[1]

As Samuel Toh commented, this is a problem because of Docker using sh for executing the commands. The problem specifically happens because sh (shell) has a builtin 'echo' command which doesn't accept any flag, not even -h/--help. So it simply prints anything that comes it's way.

You can wrap your call around as suggested with a call to bash, or explicitly ask for the correct version of bash to be used:

RUN /bin/echo -e "deb http://nginx.org/packages/mainline/ubuntu/ xenial nginx\ndeb-src http://nginx.org/packages/mainline/ubuntu/ xenial nginx" | tee /etc/apt/sources.list.d/nginx.list

The 'echo' binary may be installed at a different place in your docker image, so make sure to check where it is (/bin/, /usr/bin/, etc.) with the help of 'which' and replace it in the command above accordingly.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tomas Staig