I have a ruby on rails 7 app so I have ruby listed in my gemfile.
app/Gemfile
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
ruby "~> 3.1"
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem "rails", "~> 7.0.0"
But beyond the "standard" Rails app, i'm leveraging a Dockerfile with docker-compose to build a docker container as I'm using Github Codespace. I'm pretty beginner with docker so i more or less used the boilerplate provided by microsoft which starts like this:
# # [Choice] Ruby version (use -bullseye variants on local arm64/Apple Silicon): 3, 3.1, 3.0, 2, 2.7, 2.6, 3-bullseye, 3.1-bullseye, 3.0-bullseye, 2-bullseye, 2.7-bullseye, 2.6-bullseye, 3-buster, 3.1-buster, 3.0-buster, 2-buster, 2.7-buster, 2.6-buster
ARG VARIANT=3.1-bullseye
# Note the images provided by Microsoft have some pre-installed packages
# to make them work out of the box for dev container feature
FROM mcr.microsoft.com/vscode/devcontainers/ruby:0-${VARIANT}
But then I added also to manage versions of ruby and other libraries a Version manager called ASDF so a bit futher down on the same dockerfile, it reads:
# # Environment variables
ARG RUBY_VERSION latest
RUN git clone --depth 1 https://github.com/asdf-vm/asdf.git $HOME/.asdf && \
echo '. $HOME/.asdf/asdf.sh' >> $HOME/.bashrc && \
echo '. $HOME/.asdf/completions/asdf.bash' >> $HOME/.bashrc && \
echo '. $HOME/.asdf/asdf.sh' >> $HOME/.profile
# ASDF steps: install asdf ruby plugin, then a ruby version, then set our machine e.g our container's global Ruby version
RUN bash -c "source $HOME/.bashrc &&
asdf plugin add ruby https://github.com/asdf-vm/asdf-ruby.git &&
asdf install ruby $RUBY_VERSION &&
asdf global ruby $RUBY_VERSION"
So in total that's like 3 places where I think there is Ruby that gets installed. I feel it's too much and not really a good practice. Should I remove ruby from my gemfile and at the toip to only keep ASDF ruby as this is the tool i manage ruby versions with?
Should I keep two of them? What is recommended?
CodePudding user response:
You usually should not use a version manager with Docker (asdf
, rbenv
, rvm
, ...). At a mechanical level, most paths to running a Docker container don't read the .bashrc
or .profile
files you set up, and you need a pretty roundabout path to "activate" the version manager. Style-wise, the Docker image contains only a single application and its runtime, so you'll only ever need a single Ruby version within the context of its container, and the image's FROM
line is enough.
You should keep the ruby
constraint in your Gemfile, which helps ensure you have a compatible version of Ruby in other environments (for example, using asdf
in your non-Docker desktop setup). And you need a Ruby version in your image's FROM
line so Docker knows which base image to use.
In your example, without installing or invoking asdf
, the base image already has Ruby 3.1 available, so you don't need to do any additional work to (re)install it.