Home > Mobile >  How to Update dockerfile to set ruby version using rvm
How to Update dockerfile to set ruby version using rvm

Time:03-31

I am new to docker and I am working on a project where we are running application locally in docker container. There is a dockerfile that is pulling base image from amazonlinux. I am using docker-compose for the workflow. I have step to install ruby 2.7.5 using rvm but when I try to run tests using docker-compose docker-compose run test bundle exec bin/rspec I get error Your Ruby version is 3.0.3, but your Gemfile specified 2.7.5. You can see it is picking up the ruby version from amazon-linux-extras and not rvm I looked at bunch of threads but no luck so far. Is there any way I can set the ruby version to 2.7.5 ?

CodePudding user response:

What if you installed the version of Ruby that you want using amazon-linux-extras?

So instead of: RUN amazon-linux-extras enable python3.8 ruby3.0 \

use: RUN amazon-linux-extras enable python3.8 ruby2.7 \

CodePudding user response:

I think this should solve the problem:

Solution

Instead of using this line:

RUN rvm use $RUBY_VERSION

You should use this:

RUN rvm --default use $RUBY_VERSION

Explaination

Because this command will only set the correct RUBY_VERSION in the current shell:

RUN rvm use $RUBY_VERSION

So when you execute this line,

docker-compose run --rm --service-ports --use-aliases -e RACK_ENV=test test bundle exec bin/rspec

Another new shell will be create, and it's gonna use the default ruby version which automatically be set as 3.0.3 (when you install it with RUN amazon-linux-extras enable python3.8 ruby3.0)

That's the reason why the error is:

Your Ruby version is 3.0.3, but your Gemfile specified 2.7.5

Hope this helps

  • Related