dockerdockerfile

Cannot access downloaded repository in my Docker file from additional RUN commands


I'm setting up a docker image, I've managed to get it working, but only by running one long RUN command with everything in it.
I want to break it into smaller RUN commands for ease of maintenance and debugging, etc.

My first RUN command looks like this:

 RUN apt-get update --quiet && \
  apt-get install --quiet --yes --no-install-recommends \
  build-essential \
  libssl-dev \
  zlib1g-dev \
  libreadline-dev \
  libffi-dev \
  libyaml-dev && \
  git clone --depth 1 https://github.com/rbenv/rbenv.git ~/.rbenv && \
  git clone --depth 1 https://github.com/rbenv/ruby-build.git  && \
  echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc && \
  echo 'eval "$(rbenv init -)"' >> ~/.bashrc && \
  /bin/bash -c "source ~/.bashrc"

When I try call the rbenv install script in the next RUN command docker can't find it. Why is this? I've tried adding the path in the new RUN command, but it doesn't seem to make a difference.
What am I doing wrong?


Solution

  • The issue here with the PATH env variable. Each RUN instruction in a Dockerfile executes in a new shell session, and environment changes (like modifications to PATH in .bashrc) do not persist across these sessions.

    You need to explicitly set PATH via Docker ENV instruction, like this:

    ENV PATH="$HOME/.rbenv/bin:$PATH" 
    

    You can find more info in this stackoverflow answer, and in this Baeldung article.