ZZzzZZzz
ZZzzZZzz

Reputation: 1844

Dockerfile String interpolation dosent work

I have been trying to run Dockerfile with the below command.

RUN NODE_VERSION=$( \
    curl -sL https://nodejs.org/dist/latest/ | \
    tac | \
    tac | \
    grep -oPa -m 1 '(?<=node-v)(.*?)(?=-linux-x64\.tar\.xz)' | \
    head -1 \
) \
&& echo $NODE_VERSION \
&& curl -SLO "https://nodejs.org/dist/latest/node-v$NODE_VERSION-linux-x64.tar.xz" -o "node-v$NODE_VERSION-linux-x64.tar.xz" \
&& curl -SLO "https://nodejs.org/dist/latest/SHASUMS256.txt.asc" \
&& gpg --batch --decrypt --output SHASUMS256.txt SHASUMS256.txt.asc \
&& grep " node-v$NODE_VERSION-linux-x64.tar.xz\$" SHASUMS256.txt | sha256sum -c - \
&& tar -xJf "node-v$NODE_VERSION-linux-x64.tar.xz" -C /usr/local --strip-components=1 \
&& rm "node-v$NODE_VERSION-linux-x64.tar.xz" SHASUMS256.txt.asc SHASUMS256.txt

However, for some reason, I see the echo $NODE_VERSION outputs the version details, but, the NODE_VERSION details are not available in the subsequent curl command. What could be going wrong?

Upvotes: 1

Views: 276

Answers (1)

GracefulRestart
GracefulRestart

Reputation: 781

It would seem that your output assigned to $NODE_VERSION contains a newline which will cause most of your commands to error out.

You would want to strip the newlines from the output. Something similar to the following:

NODE_VERSION=$( \
    curl -sL https://nodejs.org/dist/latest/ | \
    grep -oPa -m 1 '(?<=node-v)(.*?)(?=-linux-x64\.tar\.xz)' | \
    head -1 | \ 
    tr -d '\r\n' \
) 

That should now get your output without any newlines. I removed the tac | tac as that seems redundant.

Upvotes: 2

Related Questions