Search code examples
dockerdockerfile

Dockerfile write multiple string with variable to file - string not interpolated


Trying the following script in Dockerfile to write multiple lines with variable to file:

ARG LIBSPARKODBC_PATH
RUN LIBSPARKODBC_PATH=$(find / -name libsparkodbc_sb64.so) && echo '[Simba Spark]'
Driver=$LIBSPARKODBC_PATH' >> /etc/odbc.ini

But this results to the following content in /etc/odbc.ini file:

[Simba Spark]
Driver=$LIBSPARKODBC_PATH

How to fix this?


Solution

  • In order to insert the content of a variable in the output of an echo command you have to use double quotes " instead of single quote '.

    For printing multiple lines with a single echo command you can insert line feed character \n.

    Your sample code would then looks like:

    ARG LIBSPARKODBC_PATH
    RUN LIBSPARKODBC_PATH=$(find / -name libsparkodbc_sb64.so) && \
        echo "[Simba Spark]\nDriver=$LIBSPARKODBC_PATH" >> /etc/odbc.ini
    

    Which, in the target file, will render as:

    [Simba Spark]
    Driver=...
    

    NOTE that the >> will append to the file. Thus, running multiple time your script will most-likely result in having multiple times that same line in the file ..