How can I make rvm work from Docker without explicitly running /bin/bash?

71 views Asked by At

I'm trying to install Herkou-20 & Ruby 2.7.5 for my Rails App for my local machine, but I keep having to input /bin/bash before every command in order to get things to work. I've tried playing with the command SHELL ["/bin/bash", "-c"] but it didn't help clean things up.

# Use the official Heroku-20 stack as the base image
FROM heroku/heroku:20

WORKDIR /app

# Set environment variables
ENV RUBY_VERSION 2.7.5

# Install dependencies
RUN apt-get update && \
    apt-get install -y build-essential libpq-dev nodejs curl gpg && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

# Install RVM and Ruby
RUN gpg --keyserver keyserver.ubuntu.com --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB && \
    \curl -sSL https://get.rvm.io | bash -s stable && \
    /bin/bash -l -c "rvm requirements" && \
    /bin/bash -l -c "rvm install $RUBY_VERSION" && \
    /bin/bash -l -c "rvm use $RUBY_VERSION --default"

# Install Bundler
RUN gem install bundler -v '2.3.6' --no-document

COPY Gemfile Gemfile.lock ./

# Copy the rest of the application code into the container
COPY . .

RUN gem list

RUN ls

RUN echo 'source /etc/profile.d/rvm.sh' >> /root/.bashrc
RUN /bin/bash -l -c "rvm use $RUBY_VERSION --default && which ruby && ruby -v"

RUN /bin/bash -l -c "which ruby"

RUN /bin/bash -l -c "rvm list"

RUN /bin/bash -l -c "bundle install"

# Print the shell's name
RUN /bin/bash -l -c "echo Shell: $0"

# Set the default command to run when the container starts
CMD ["/bin/bash", "-l"]
1

There are 1 answers

1
datawookie On

This is the (slightly abridged) output generated when installing RVM:

Installing RVM to /usr/local/rvm/
Installation of RVM in /usr/local/rvm/ is almost complete:

  * First you need to add all users that will be using rvm to 'rvm' group,
    and logout - login again.
  * To start using RVM you need to run `source /etc/profile.d/rvm.sh`
    in all your open shell windows, in rare cases you need to reopen all shell
    windows.
  * Please do NOT forget to add your users to the rvm group.
    The installer no longer auto-adds root or users to the rvm group. Admins must
    do this. Also, please note that group memberships are ONLY evaluated at
    login time. This means that users must log out then back in before group
    membership takes effect!

You don't need to worry about the rvm group. But you do need to source that startup script when you launch a shell. I'd also suggest changing the shell being used from /bin/sh to BASH (this is done via the SHELL command).

FROM heroku/heroku:20

WORKDIR /app

ENV RUBY_VERSION 2.7.5

RUN apt-get update && \
    apt-get install -y build-essential libpq-dev nodejs curl gpg && \
    rm -rf /var/lib/apt/lists/*

SHELL ["/bin/bash", "-lc"]

RUN gpg --keyserver keyserver.ubuntu.com --recv-keys \
        409B6B1796C275462A1703113804BB82D39DC0E3 \
        7D2BAF1CF37B13E2069D6956105BD0E739499BDB && \
    curl -sSL https://get.rvm.io | bash -s stable && \
    echo "source /etc/profile.d/rvm.sh" >> /etc/bash.bashrc

RUN rvm requirements && \
    rvm install $RUBY_VERSION && \
    rvm use $RUBY_VERSION --default

Put the rvm commands into a separate RUN so that you get a fresh BASH instance and the effects of the startup script have been applied.

You can now remove explicit invocation of /bin/bash from all subsequent commands.