ruby-on-railsdockerubuntudockerfiledevops

i just want my dockerfile to do bundle install only if there are changes in Gemfile and Gemfile.lock ........ ruby on rails ,docker


here is my Dockerfile

just want my dockerfile to do bundle install only if there are changes in Gemfile and Gemfile.lock otherwise it should use the cache for bundle install


FROM ruby:3.0.2
RUN curl https://deb.nodesource.com/setup_18.x | bash
RUN curl https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
RUN echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
RUN apt-get update -qq && apt-get install -y --fix-missing nodejs postgresql-client yarn
#ENV BUNDLER_VERSION=2.1.4
RUN gem install bundler
# ENV RAILS_ENV production
RUN mkdir /myapp
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
COPY . /myapp
RUN npm install
RUN yarn add postcss
RUN yarn install
RUN bundle install
RUN RAILS_ENV=development bin/rails assets:precompile

#RUN bundle exec rails webpacker:install
#RUN bundle exec rake assets:clobber
#RUN bundle exec rake assets:precompile
EXPOSE 3000

i just want a solution someone helpme please

with this solution we can reduce the time and load right?


Solution

  • Docker uses multiple layers during build (https://docs.docker.com/get-started/docker-concepts/building-images/understanding-image-layers/), like an onion.

    If you install your bundle after only copying the Gemfile, Docker will be able to cache the bundle install if the Gemfile (and Gemfile.lock) did not change.

    Because you also copied your application code before running bundle install, it was not able to cache the gems - since docker does not know whether changes to your app code result in changes to the bundle install command.

    Simply move up your bundle install command like below. You can also do that for your JS files (package.json or so).

    (Also note, that there are many more best practices that can be applied to your Dockerfile, that newer Rails releases include. Or you could try https://github.com/fly-apps/dockerfile-rails for older rails versions.)

    FROM ruby:3.0.2
    RUN curl https://deb.nodesource.com/setup_18.x | bash
    RUN curl https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
    RUN echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
    RUN apt-get update -qq && apt-get install -y --fix-missing nodejs postgresql-client yarn
    #ENV BUNDLER_VERSION=2.1.4
    RUN gem install bundler
    # ENV RAILS_ENV production
    RUN mkdir /myapp
    WORKDIR /myapp
    COPY Gemfile /myapp/Gemfile
    COPY Gemfile.lock /myapp/Gemfile.lock
    RUN bundle install # <-- move that here --------
    COPY . /myapp
    RUN npm install
    RUN yarn add postcss
    RUN yarn install
    RUN RAILS_ENV=development bin/rails assets:precompile
    
    #RUN bundle exec rails webpacker:install
    #RUN bundle exec rake assets:clobber
    #RUN bundle exec rake assets:precompile
    EXPOSE 3000