Pipe RUN's output to ENV in Dockerfile

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.

  • How to forbid attempts to use inter-container communication via ingress network in docker swarm?
  • jenkins 'execute script' build step Error: /bin/docker: Permission denied
  • Elastic Beanstalk deployment of Docker failing and reverting to old application version
  • Mount “named volume” as non-root in Docker
  • How to change location of “.docker” directory
  • Docker stop responding under load
  • 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
    

  • Using git clone in a Dockerfile Produces empty working directory
  • Docker: Mixing distros - any disadvantage?
  • How to build, test and deploy using Jhipster, Docker, Gitlab and Heroku
  • can't run a pull queue handler inside a custom vm
  • Running docker containers inside of docker + mesos + marathon
  • Docker file size problems
  • Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.