I have the following command in my Dockerfile:
RUN echo "\
export NODE_VERSION=$(\
curl -sL https://nodejs.org/dist/latest/ |\
tac |\
tac |\
grep -oPa -m 1 '(?<=node-v)(.*?)(?=-linux-x64\.tar\.xz)' |\
head -1\
)" >> /etc/bash.bashrc
RUN source /etc/bash.bashrc
The following command should store export NODE_VERSION=6.2.2 in /etc/bash.bashrc, but it's not storing anything.
This works however when I'm inside an image with bash and manually entering the following commands.
Update:
I changed back the shell from bash to the Debian/Ubuntu default dash, which is POSIX standard. I removed this line:
RUN ln -sf /bin/bash /bin/sh && ln -sf /bin/bash /bin/sh.distrib
Than I tried to add to the environment variables with export:
RUN export NODE_VERSION=$(\
curl -sL https://nodejs.org/dist/latest/ |\
tac |\
tac |\
grep -oPa -m 1 '(?<=node-v)(.*?)(?=-linux-x64\.tar\.xz)' |\
head -1\
)
But again, the output is missing at image creation, but works when I running the image with $ docker run --rm -it debian /bin/sh. Why?
Update 2:
Looks like the final solution should be something like this:
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
ENV NODE_VERSION $NODE_VERSION
echo $NODE_VERSION returning 6.2.2 as it should at the execution of the Dockerfile also, but ENV NODE_VERSION $NODE_VERSION cannot read this. Is there a way to define variables globally or how can I pass the RUN's output to ENV?
Solution:
I ended up putting the node.js installation part under the same RUN 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
https://nodejs.org/dist/latest/node-v$NODE_VERSION-linux-x64.tar.xzwhich is interpreted tohttps://nodejs.org/dist/latest/node-v6.2.2-linux-x64.tar.xzfor the node.js installation. Unfortunately Node.js repo not offeringhttps://nodejs.org/dist/latest/node-latest-linux-x64.tar.xzarchive which will make my question unnecessary.COPY ./node-version.sh /root/ RUN chmod +x $HOME/node-version.sh; /root/node-version.sh