skip to Main Content

I use ruby:2.7.1-alpine for my project
it installs Node version 12, I need version >=16 (I think 18 is desirable) to work with react.

how can I update the version in my Dockerfile ?

FROM ruby:2.7.1-alpine


ARG RAILS_ROOT=/task_manager
ARG PACKAGES="vim openssl-dev postgresql-dev build-base curl nodejs-current yarn less tzdata git postgresql-client bash screen gcompat"

RUN apk update 
    && apk upgrade 
    && apk add --update --no-cache $PACKAGES

RUN gem install bundler:2.1.4

RUN mkdir $RAILS_ROOT
WORKDIR $RAILS_ROOT
COPY Gemfile Gemfile.lock  ./
RUN bundle install --jobs 5

COPY package.json yarn.lock ./
RUN yarn install --frozen-lockfile

ADD . $RAILS_ROOT
ENV PATH=$RAILS_ROOT/bin:${PATH}

EXPOSE 3000
CMD bundle exec rails s -b '0.0.0.0' -p 3000

currently in this build the following options are available:

bash-5.0#  apk search nodejs
nodejs-12.22.12-r0
npm-12.22.12-r0
nodejs-current-doc-14.5.0-r0
nodejs-current-14.5.0-r0
nodejs-dev-12.22.12-r0
nodejs-current-dev-14.5.0-r0
nodejs-doc-12.22.12-r0

I can’t figure it out.

2

Answers


  1. Chosen as BEST ANSWER

    I just change FROM ruby:2.7.1-alpine to FROM ruby:3.0-alpine and build new image


  2. I see 2 options to be somewhat independent of your original image:

    • Use node version management this tool helps keep multipliemanage installation of node in the system. There are some caveats using it with alpine but it is solvable. Check out their documentation.

    • Second option (less pretty in my opinion) is to use copy from another pre-build node alpine image, it can be done like this:

    FROM ruby:2.7.1-alpine
    
    RUN apk update 
        && apk upgrade
    
    # copying node binaries, and yarn stuff
    COPY --from=node:18-alpine /usr/local/bin/node /usr/local/bin/node
    COPY --from=node:18-alpine /usr/local/lib/node_modules/ /usr/local/lib/node_modules/
    COPY --from=node:18-alpine /opt/yarn-v1.22.19 /opt/yarn-v1.22.19
    
    # create proper symlinks
    RUN 
        ln -s /usr/local/lib/node_modules/corepack/dist/corepack.js /usr/local/bin/corepack 
        && ln -s /usr/local/lib/node_modules/npm/bin/npm-cli.js /usr/local/bin/npm 
        && ln -s /usr/local/lib/node_modules/npm/bin/npx-cli.js /usr/local/bin/npx 
        && ln -s /opt/yarn-v1.22.19/bin/yarn /usr/local/bin/yarn 
        && ln -s /opt/yarn-v1.22.19/bin/yarnpkg /usr/local/bin/yarnpkg
    
    # here you can you yarnnpmnpx and node
    
    

    To adjust node version, just change node-XX:alpine image version, everything should continue to work (except for yarn, you’ll have to adjust its path)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search